我在嵌套循环中有两个列表,当我在内部匹配一个项目时,我想删除它以便提高性能。
List<String[]> brandList = readCsvFile("/tmp/brand.csv");
List<String[]> themeList = readCsvFile("/tmp/theme.csv");
for (String[] brand : brandList) {
for (String[] theme : themeList) {
if (brand[0].equals(theme[0])) {
themeList.remove(theme);
}
}
}
我有一个java.util.ConcurrentModificationException
错误。如果我改为 CopyOnWriteArrayList,错误如下:
CopyOnWriteArrayList<String[]> themeList = (CopyOnWriteArrayList<String[]>)readCsvFile("/tmp/theme.csv");
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.concurrent.CopyOnWriteArrayList
现在我该怎么办?省略删除?还是别的?
我认为这是我需要的:
List<String[]> brandList = readCsvFile("/tmp/brand.csv");
List<String[]> themeList = readCsvFile("/tmp/theme.csv");
for (String[] brand : brandList) {
List<String[]> toRemove = new ArrayList<String[]>();
for (String[] theme : themeList) {
if (brand[0].equals(theme[0])) {
toRemove.add(theme);
}
}
for (String[] theme : toRemove) {
themeList.removeAll(theme);
}
}