Java 如何在数据库中按名称筛选位置?

Java 如何在数据库中按名称筛选位置?,java,location,Java,Location,我正在尝试从我的数据库中收集具有相同名称的所有位置,为此,我使用以下方法: public static List<Location> searchRepe(List<Location>ls){ List<Location>res=new ArrayList<Location>(); for(Location l:ls){ ls.remove(l); if(ls.stream().anyMatc

我正在尝试从我的数据库中收集具有相同名称的所有位置,为此,我使用以下方法:

public static List<Location> searchRepe(List<Location>ls){
        List<Location>res=new ArrayList<Location>();
        for(Location l:ls){
            ls.remove(l);
if(ls.stream().anyMatch(x>x.getProvinceName().equals(l.getProvinceName()))){
                res.add(l);}
            ls.add(l);      
        }
        return res;
    }
publicstaticlist searchRepe(Listls){
Listres=newarraylist();
用于(位置l:ls){
ls.移除(l);
如果(ls.stream().anyMatch(x>x.getProvinceName().equals(l.getProvinceName())){
res.add(l);}
ls.加入(l);
}
返回res;
}
错误:线程“main”java.util.ConcurrentModificationException中出现异常

我首先删除列表的位置,然后检查是否有其他同名位置。在检查位置的名称是否在列表中后,我将其添加到我的res列表中。在任何情况下,我将始终保持原始列表不变,因为在检查之后,我将元素读取到列表中。我知道这个错误是因为每次迭代我都删除并添加了元素,但是如果每次迭代我删除并添加了相同的元素,那么列表的大小总是相同的


有人能告诉我一个更好的方法吗?如果可以使用java8

foreach
语句使用
Iterator
检索项,并且
Iterator
不能直接删除项,否则该方法将抛出
ConcurrentModificationException
。因此,您可以通过以下方法从列表中删除项目:

for (int index = 0; index < list.size(); index++) {
    if(condition){
        list.remove(index);
        index--;
    }
}

请注意这一行
locationMap.put(l.getProvinceName(),l)
put
方法的结果取决于
Location
类中的
equals
方法。

如果您绝对想使用stream和java8,我相信这是正确的解决方案。 只需根据
ProvinceName
多个出现次数过滤所有元素

public static List<Location> searchRepe(List<Location> ls) {

    return ls.stream()
             .filter(
                     location -> ls.stream()
                                   .map(Location::getProvinceName)
                                   .filter(n -> Objects.equals(n, location.getProvinceName()))
                                   .count() > 1L
             )
             .collect(Collectors.toList());
}
publicstaticlist searchRepe(列表ls){
返回ls.stream()
.过滤器(
位置->ls.stream()
.map(位置::getProvinceName)
.filter(n->Objects.equals(n,location.getProvinceName()))
.count()>1L
)
.collect(Collectors.toList());
}
或者使用分组方式

public static List<Location> searchRepe(List<Location> ls) {

    return ls.stream()
             .collect(Collectors.groupingBy(Location::getProvinceName))
             .entrySet()
             .stream()
             .map(Map.Entry::getValue)
             .filter(entry -> entry.size() > 1)
             .map(entry -> entry.get(1))
             .collect(toList());
}
publicstaticlist searchRepe(列表ls){
返回ls.stream()
.collect(收集器.groupingBy(位置::getProvinceName))
.entrySet()
.stream()
.map(map.Entry::getValue)
.filter(条目->条目.size()>1)
.map(条目->条目.get(1))
.collect(toList());
}

第一种方法非常有效,前提是我想要一个包含所有具有相同省名的地点的列表。第二种方法仅收集根据其provinceName多次出现的位置。非常感谢你,尼奥奈洛
public static List<Location> searchRepe(List<Location> ls) {

    return ls.stream()
             .collect(Collectors.groupingBy(Location::getProvinceName))
             .entrySet()
             .stream()
             .map(Map.Entry::getValue)
             .filter(entry -> entry.size() > 1)
             .map(entry -> entry.get(1))
             .collect(toList());
}