0

I'm trying to get the values of listString and save them to the list of objects listFeed and return listFeed. So far I used a Scanner to add date from feedsFile to my ArrayList listString but I don't know how to store those values in an ArrayList of Objects.

Here's the code snippet

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

Here's the Feed class:

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());
    }
}
3
  • Where (in which field) exactly in the Feed class you want to store the entries of the ArrayList<String>? Commented Sep 3, 2020 at 8:39
  • You don't need to loop over the listFeed items, you can call .add(new Feed(...)) for every item in your listString list. Another way is to call .stream() and then .map(...) with a lambda that takes a String and returns a Feed, then .collect(Collectors.toList()) Commented Sep 3, 2020 at 8:51
  • Thank you. The .add(new Feed(...)) works. Commented Sep 3, 2020 at 9:02

1 Answer 1

1

The simplest way is to change your loop.

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

That way you're adding a new Feed object to the listFeed.

Another way you could do it is to use the stream api.

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

Your original technique with a loop is fine though.

A minor note, you do not need to call super() explicitly it will be called automatically if you don't use a different version of super.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.