gpt4 book ai didi

java - 将数据保存到 ArrayList 的高效代码

转载 作者:行者123 更新时间:2023-12-02 08:39:58 24 4
gpt4 key购买 nike

我的控制台从这样的 XML 文件输出年份标签

2020
2019
1997
2017
2019
2017 (...)

从这些数据中,我想将每个不同的年份保存在 ArrayList 中,例如:

Years found on file: 2020 , 2019 , 1997 , 2017

我尝试了很多方法,但似乎都不起作用。我正在尝试使用下面的代码找出解决方案:

public class Publications {
public static void main(String[] args) throws IOException {
File file = new File("dblp-2020-04-01.xml");
FileInputStream fileStream = new FileInputStream(file);
InputStreamReader input = new InputStreamReader(fileStream);
BufferedReader reader = new BufferedReader(input);
String line;
ArrayList<String> publicationsList = new ArrayList<String>();
int i = 0;
while ((line = reader.readLine()) != null) {
Publications publ = new Publications();
Pattern pattern = Pattern.compile("<year>(.+?)</year>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
String year = matcher.group(1);
if (publicationsList.size() == 0) {
publicationsList.add(year);
}else{
for(String publications1 : publicationsList){
if(!(publications1.contains(year))){
publicationsList.add(year);
}
}
}
}
}
//READING TEST
for (String publications1 : publicationsList){
System.out.println(publications1);
}
}
}

错误:

Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:996)
at Publications.main(Publications.java:26)

最佳答案

替换ArrayListLinkedHashSet并且重复项将自动被忽略,而插入值的顺序仍然保留。

此外,今年是 2020 年,所以您应该使用 NIO.2 API 和 try-with-resources 语句,这两个语句都是在 2011 年 Java 7 中添加的。这将有助于解决您不关闭文件的问题流。

您的代码应如下所示:

Set<String> publicationYears = new LinkedHashSet<>();
try (BufferedReader reader = Files.newBufferedReader(Paths.get("dblp-2020-04-01.xml"))) {
Pattern pattern = Pattern.compile("<year>(.+?)</year>", Pattern.DOTALL);
for (String line; (line = reader.readLine()) != null; ) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
String year = matcher.group(1);
publicationYears.add(year);
}
}
}
//READING TEST
for (String year : publicationYears){
System.out.println(year);
}
<小时/>

当然,由于您正在读取 XML 文件,因此使用 XML 解析器会更好,例如斯塔克斯:

Set<String> publicationYears = new LinkedHashSet<>();
try (InputStream in = Files.newInputStream(Paths.get("dblp-2020-04-01.xml"))) {
XMLStreamReader xml = XMLInputFactory.newFactory().createXMLStreamReader(in);
while (xml.hasNext()) {
xml.next();
if (xml.getEventType() == XMLStreamConstants.START_ELEMENT) {
if (xml.getLocalName().equals("year")) {
String year = xml.getElementText();
publicationYears.add(year);
}
}
}
}

关于java - 将数据保存到 ArrayList 的高效代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61439295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com