我正在尝试获取的值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] 删除。
我来说两句