繁体   English   中英

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

[英]How to store String values from an ArrayList<String> to an ArrayList<Object> of Objects

我正在尝试获取listString的值并将它们保存到对象列表listFeed并返回listFeed 到目前为止,我使用扫描器将feedsFile日期添加到我的 ArrayList listString但我不知道如何将这些值存储在对象的 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) );
}

这样你就向listFeed 添加了一个新的Feed 对象。

另一种方法是使用流api。

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

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

一个小注意事项,如果您不使用不同版本的 super,您不需要显式调用super()它将被自动调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM