【问题标题】:How to store String values from an ArrayList<String> to an ArrayList<Object> of Objects如何将字符串值从 ArrayList<String> 存储到 ArrayList<Object> 对象
【发布时间】:2020-09-03 08:31:10
【问题描述】:

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

这里是代码sn-p

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());
    }
}

【问题讨论】:

  • 您希望在Feed 类中的哪个位置(在哪个字段中)存储ArrayList&lt;String&gt; 的条目?
  • 您不需要遍历listFeed 项目,您可以为listString 列表中的每个项目调用.add(new Feed(...))。另一种方法是调用.stream(),然后调用.map(...),使用一个接受String并返回Feed的lambda,然后是.collect(Collectors.toList())
  • 谢谢。 .add(new Feed(...)) 有效。

标签: java arraylist


【解决方案1】:

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

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,它会自动调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多