java—如何将字符串值从arraylist< string>存储到对象的arraylist< object>

6mzjoqzu  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(437)

我想知道 listString 并将它们保存到对象列表中 listFeed 然后回来 listFeed . 到目前为止,我用扫描仪来添加日期 feedsFile 到我的数组列表 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());
    }
}
gmxoilav

gmxoilav1#

最简单的方法就是改变你的循环。

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

这样您就可以向listfeed添加一个新的feed对象。
另一种方法是使用streamapi。

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

不过,你的原始循环技术还不错。
一个小提示,你不需要打电话 super() 如果不使用不同版本的super,它将被显式地自动调用。

相关问题