如何将字符串值从ArrayList <String>存储到对象的ArrayList <Object>

TotalNoob:

我正在尝试获取的值listString并将其保存到对象列表中listFeed并返回listFeed到目前为止,我已使用Scanner将日期从添加feedsFile到我的ArrayList中,listString但是我不知道如何将这些值存储在Objects的ArrayList中。

这是代码片段

public List<Feed> loadSubscribedFeeds(File feedsFile) throws FileNotFoundException {
    
    Scanner s = new Scanner(feedsFile);
    
    List<String> listString = new ArrayList<>();
    List<Feed> listFeed = new ArrayList<>();
    
    while (s.hasNextLine()) {
        listString.add(s.nextLine());
    }
    
    for(int i = 0; i < listString.size(); i++) {
        for(int j = 0; j < listFeed.size(); j++) {
            
        }
    }
    
    return listFeed;
}

这是Feed类:

public class Feed implements Serializable, Comparable<Feed> {

    private static final long serialVersionUID = 1L;

    private String url;
    private String title;
    private String description;
    private String publishedDateString;
    private List<Entry> entries;

    public Feed(String url) {
        super();
        this.url = url;
        this.entries = new ArrayList<Entry>();
        this.title = "";
        this.description = "";
        this.publishedDateString = "";
    }

    /**
     * Creates an instance of a Feed and transfers the feed
     * data form a SyndFeed object to the new instance.
     *
     * @param url        The URL string of this feed
     * @param sourceFeed The SyndFeed object holding the data for this feed instance
     */
    public Feed(String url, SyndFeed sourceFeed) {
        this(url);
        setTitle(sourceFeed.getTitle());
        setDescription(sourceFeed.getDescription());
        if (sourceFeed.getPublishedDate() != null)
            setPublishedDateString(FeaderUtils.DATE_FORMAT.format(sourceFeed.getPublishedDate()));
        for (SyndEntry entryTemp : sourceFeed.getEntries()) {
            Entry entry = new Entry(entryTemp.getTitle());
            entry.setContent(entryTemp.getDescription().getValue());
            entry.setLinkUrl(entryTemp.getLink());
            entry.setParentFeedTitle(getTitle());
            if (entryTemp.getPublishedDate() != null) {
                entry.setPublishedDateString(FeaderUtils.DATE_FORMAT.format(entryTemp.getPublishedDate()));
            }
            addEntry(entry);
        }
    }

    public String getUrl() {
        return url;
    }

    public void setTitle(String title) {
        this.title = title != null ? title : "";
    }

    public String getTitle() {
        return title;
    }

    public void setDescription(String description) {
        this.description = description != null ? description : "";
    }

    public String getDescription() {
        return description;
    }

    public void setPublishedDateString(String publishedDateString) {
        this.publishedDateString = publishedDateString != null ? publishedDateString : "";
    }

    public String getPublishedDateString() {
        return publishedDateString;
    }

    /**
     * Returns a short string containing a combination of meta data for this feed
     *
     * @return info string
     */
    public String getShortFeedInfo() {
        return getTitle() + " [" +
                getEntriesCount() + " entries]: " +
                getDescription() +
                (getPublishedDateString() != null && getPublishedDateString().length() > 0
                        ? " (updated " + getPublishedDateString() + ")"
                        : "");
    }

    public void addEntry(Entry entry) {
        if (entry != null) entries.add(entry);
    }

    public List<Entry> getEntries() {
        return entries;
    }

    public int getEntriesCount() {
        return entries.size();
    }

    @Override
    public boolean equals(Object obj) {
        return (obj instanceof Feed)
                && ((Feed) obj).getUrl().equals(url);
    }

    @Override
    public int hashCode() {
        return url.hashCode();
    }

    @Override
    public String toString() {
        return getTitle();
    }

    @Override
    public int compareTo(Feed o) {
        return getPublishedDateString().compareTo(o.getPublishedDateString());
    }
}
马特:

最简单的方法是更改​​循环。

for(int i = 0; i < listString.size(); i++) {
    listFeed.add( new Feed( listString.get(i) );
}

这样,您可以将新的Feed对象添加到listFeed。

您可以执行的另一种方法是使用流api。

List<Feed> feeds = listString.stream().map( Feed::new ).collect( Collectors.toList() );

不过,您使用循环的原始技术很好。

一个小注释,super()如果您不使用其他版本的super ,则无需显式调用它将自动调用

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我如何将字符串正确地分离到ArrayList中?

在Java 8中,如何从ArrayList中获取String值并将其存储在以逗号分隔的单个字符串中?

如何将ArrayList <Object>转换为ArrayList <String>?

如何将javascript对象数组转换为所需的object属性的字符串数组?

如何将字符串数组更改为数组。String []到ArrayList <string>格式转换

如何将字符串转换为ArrayList?

ArrayList和String [] AND Object []

如何将JSON字符串转换为对象的Arraylist

如何将分隔的字符串拆分()到List <String>

如何将值从NSMutableArray存储到字符串?

如何将Map <String,LinkedList <Object >>数据存储到数据库表中?

如何将vector <pair <string,int >>中的字符串复制到vector <string>?

将字符串转换为ArrayList <String>

将ArrayList <String []>转换为String [] []或Object [] []

如何将StringBuilder值存储在字符串中

如何将字符串转换为ArrayList?

ArrayList <HashMap <String,String >>到JSON对象Java

如何将arraylist <object>转换为String并将其重新转换为arraylist <object>

如何将arrayList <String>转换为json对象

ArrayList <String []>中的字符串

如何将 ArrayList<HashMap> 的值作为字符串获取?

将字符串添加到 arraylist<String>

将字符串添加到 ArrayList<ArrayList<String[]>>

如何将所有 ArrayList<ArrayList<String>> 值存储到 ArrayList<String> 中?

如何将 arrayList 存储到 Map 'string as key 以及如何检索它..!

如何将长值存储为字符串

如何将值模型类对象作为字符串获取到 ArrayList?

将 ArrayList<Object> 转换为字符串

Yup.string.when 将始终给出未定义的结果。我如何将字符串值传递到 .when 方法中