Java流-查找唯一元素

Java流-查找唯一元素,java,java-8,java-stream,Java,Java 8,Java Stream,我有List persons=new ArrayList()并且我想列出所有唯一的名称。我的意思是,如果有“约翰”、“马克斯”、“约翰”、“格雷格”,那么我只想列出“马克斯”和“格雷格”。有什么方法可以用Java stream实现吗?这将删除所有重复的元素 List<String> persons = new ArrayList<>(); persons.add("John"); persons.add("John");

我有
List persons=new ArrayList()并且我想列出所有唯一的名称。我的意思是,如果有“约翰”、“马克斯”、“约翰”、“格雷格”,那么我只想列出“马克斯”和“格雷格”。有什么方法可以用Java stream实现吗?

这将删除所有重复的元素

List<String> persons = new ArrayList<>();

        persons.add("John");
        persons.add("John");
        persons.add("MAX");
        persons.add("Greg");

        Set<String> set = new HashSet<String>();

        Set<String> duplicateSet = new HashSet<String>();

        for (String p : persons) {

            if (!set.add(p)) {
                duplicateSet.add(p);
            }
        }

        System.out.println(duplicateSet.toString());
        set.removeAll(duplicateSet);
        System.out.println(set.toString());
List persons=new ArrayList();
人员。添加(“约翰”);
人员。添加(“约翰”);
添加(“最大值”);
人员。添加(“格雷格”);
Set=newhashset();
Set duplicateSet=新HashSet();
用于(字符串p:人){
如果(!set.add(p)){
添加(p);
}
}
System.out.println(duplicateSet.toString());
set.removeAll(重复集);
System.out.println(set.toString());

这将删除所有重复的元素

List<String> persons = new ArrayList<>();

        persons.add("John");
        persons.add("John");
        persons.add("MAX");
        persons.add("Greg");

        Set<String> set = new HashSet<String>();

        Set<String> duplicateSet = new HashSet<String>();

        for (String p : persons) {

            if (!set.add(p)) {
                duplicateSet.add(p);
            }
        }

        System.out.println(duplicateSet.toString());
        set.removeAll(duplicateSet);
        System.out.println(set.toString());
List persons=new ArrayList();
人员。添加(“约翰”);
人员。添加(“约翰”);
添加(“最大值”);
人员。添加(“格雷格”);
Set=newhashset();
Set duplicateSet=新HashSet();
用于(字符串p:人){
如果(!set.add(p)){
添加(p);
}
}
System.out.println(duplicateSet.toString());
set.removeAll(重复集);
System.out.println(set.toString());

您只需使用
Collections.frequency
检查列表中的元素出现情况,如下所示以过滤重复项:

List<String> listInputs = new ArrayList<>();
//add your users
List<String> listOutputs = new ArrayList<>();
for(String value : listInputs) {
     if(Collections.frequency(listInputs, value) ==1) {
         listOutputs.add(value);
     }
}
System.out.println(listOutputs);
List listInputs=new ArrayList();
//添加您的用户
List listOutputs=新建ArrayList();
for(字符串值:listInputs){
if(Collections.frequency(listInputs,value)==1){
列表输出。添加(值);
}
}
系统输出打印项次(列表输出);

您只需使用
Collections.frequency
检查列表中的元素出现情况,如下所示以过滤重复项:

List<String> listInputs = new ArrayList<>();
//add your users
List<String> listOutputs = new ArrayList<>();
for(String value : listInputs) {
     if(Collections.frequency(listInputs, value) ==1) {
         listOutputs.add(value);
     }
}
System.out.println(listOutputs);
List listInputs=new ArrayList();
//添加您的用户
List listOutputs=新建ArrayList();
for(字符串值:listInputs){
if(Collections.frequency(listInputs,value)==1){
列表输出。添加(值);
}
}
系统输出打印项次(列表输出);

我们可以使用流和
收集器。groupingBy
计算每个名称出现的次数,然后过滤出现多次的名称:

    List<String> res = persons.stream()
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
            .entrySet()
            .stream()
            .filter(e -> e.getValue() == 1)
            .map(e -> e.getKey())
            .collect(Collectors.toList());

    System.out.println(res); // [Max, Greg]
List res=persons.stream()
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting())
.entrySet()
.stream()
.filter(e->e.getValue()==1)
.map(e->e.getKey())
.collect(Collectors.toList());
System.out.println(res);//[马克斯,格雷格]

我们可以使用流和
收集器。groupingBy
计算每个名称出现的次数,然后过滤出现多次的名称:

    List<String> res = persons.stream()
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
            .entrySet()
            .stream()
            .filter(e -> e.getValue() == 1)
            .map(e -> e.getKey())
            .collect(Collectors.toList());

    System.out.println(res); // [Max, Greg]
List res=persons.stream()
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting())
.entrySet()
.stream()
.filter(e->e.getValue()==1)
.map(e->e.getKey())
.collect(Collectors.toList());
System.out.println(res);//[马克斯,格雷格]
第一猜测解决方案

persons.stream()
       .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
       .entrySet()
       .stream()
       .filter(entry -> entry.getValue() == 1)
       .map(Map.Entry::getKey)
       .collect(Collectors.toList())
第一猜测答案

persons.stream()
       .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
       .entrySet()
       .stream()
       .filter(entry -> entry.getValue() == 1)
       .map(Map.Entry::getKey)
       .collect(Collectors.toList())

这是一篇老文章,但我想提出另一种基于自定义收集器的方法:

public static <T> Collector<T, ?, List<T>> excludingDuplicates() {
    return Collector.<T, Map<T, Boolean>, List<T>>of(
        LinkedHashMap::new,
        (map, elem) -> map.compute(elem, (k, v) -> v == null),
        (left, right) -> {
            right.forEach((k, v) -> left.merge(k, v, (o, n) -> false));
            return left;
        },
        m -> m.keySet().stream().filter(m::get).collect(Collectors.toList()));
}
Map<String, Boolean> duplicates = new LinkedHashMap<>();
people.forEach(elem -> duplicates.compute(elem, (k, v) -> v != null));
duplicates.values().removeIf(v -> v);

Set<String> allUnique = duplicates.keySet();

System.out.println(allUnique); // [Max, Greg]

还有一种方法比使用自定义收集器更简单:

public static <T> Collector<T, ?, List<T>> excludingDuplicates() {
    return Collector.<T, Map<T, Boolean>, List<T>>of(
        LinkedHashMap::new,
        (map, elem) -> map.compute(elem, (k, v) -> v == null),
        (left, right) -> {
            right.forEach((k, v) -> left.merge(k, v, (o, n) -> false));
            return left;
        },
        m -> m.keySet().stream().filter(m::get).collect(Collectors.toList()));
}
Map<String, Boolean> duplicates = new LinkedHashMap<>();
people.forEach(elem -> duplicates.compute(elem, (k, v) -> v != null));
duplicates.values().removeIf(v -> v);

Set<String> allUnique = duplicates.keySet();

System.out.println(allUnique); // [Max, Greg]
Map duplicates=newlinkedhashmap();
people.forEach(elem->duplicates.compute(elem,(k,v)->v!=null));
重复.values().removeIf(v->v);
Set allUnique=duplicates.keySet();
System.out.println(allUnique);//[马克斯,格雷格]

这是一篇老文章,但我想提出另一种基于自定义收集器的方法:

public static <T> Collector<T, ?, List<T>> excludingDuplicates() {
    return Collector.<T, Map<T, Boolean>, List<T>>of(
        LinkedHashMap::new,
        (map, elem) -> map.compute(elem, (k, v) -> v == null),
        (left, right) -> {
            right.forEach((k, v) -> left.merge(k, v, (o, n) -> false));
            return left;
        },
        m -> m.keySet().stream().filter(m::get).collect(Collectors.toList()));
}
Map<String, Boolean> duplicates = new LinkedHashMap<>();
people.forEach(elem -> duplicates.compute(elem, (k, v) -> v != null));
duplicates.values().removeIf(v -> v);

Set<String> allUnique = duplicates.keySet();

System.out.println(allUnique); // [Max, Greg]

还有一种方法比使用自定义收集器更简单:

public static <T> Collector<T, ?, List<T>> excludingDuplicates() {
    return Collector.<T, Map<T, Boolean>, List<T>>of(
        LinkedHashMap::new,
        (map, elem) -> map.compute(elem, (k, v) -> v == null),
        (left, right) -> {
            right.forEach((k, v) -> left.merge(k, v, (o, n) -> false));
            return left;
        },
        m -> m.keySet().stream().filter(m::get).collect(Collectors.toList()));
}
Map<String, Boolean> duplicates = new LinkedHashMap<>();
people.forEach(elem -> duplicates.compute(elem, (k, v) -> v != null));
duplicates.values().removeIf(v -> v);

Set<String> allUnique = duplicates.keySet();

System.out.println(allUnique); // [Max, Greg]
Map duplicates=newlinkedhashmap();
people.forEach(elem->duplicates.compute(elem,(k,v)->v!=null));
重复.values().removeIf(v->v);
Set allUnique=duplicates.keySet();
System.out.println(allUnique);//[马克斯,格雷格]

您可以尝试以下代码

    List<Person> uniquePersons = personList.stream()
            .collect(Collectors.groupingBy(person -> person.getName()))
            .entrySet().stream().filter(stringListEntry -> stringListEntry.getValue().size()==1)
            .map(stringListEntry -> { return stringListEntry.getValue().get(0); })
            .collect(Collectors.toList());
List uniquePersons=personList.stream()
.collect(Collectors.groupingBy(person->person.getName())
.entrySet().stream().filter(stringListEntry->stringListEntry.getValue().size()=1)
.map(stringListEntry->{return stringListEntry.getValue().get(0);})
.collect(Collectors.toList());

您可以尝试以下代码

    List<Person> uniquePersons = personList.stream()
            .collect(Collectors.groupingBy(person -> person.getName()))
            .entrySet().stream().filter(stringListEntry -> stringListEntry.getValue().size()==1)
            .map(stringListEntry -> { return stringListEntry.getValue().get(0); })
            .collect(Collectors.toList());
List uniquePersons=personList.stream()
.collect(Collectors.groupingBy(person->person.getName())
.entrySet().stream().filter(stringListEntry->stringListEntry.getValue().size()=1)
.map(stringListEntry->{return stringListEntry.getValue().get(0);})
.collect(Collectors.toList());
以下是我的解决方案:

List<String> persons = new ArrayList<>();
persons.add("John");
persons.add("John");
persons.add("MAX");
persons.add("Greg");
persons.stream()
          .distinct()
          .sorted()
          .collect(Collectors.toList());
List persons=new ArrayList();
人员。添加(“约翰”);
人员。添加(“约翰”);
添加(“最大值”);
人员。添加(“格雷格”);
人流()
.distinct()
.已排序()
.collect(Collectors.toList());
以下是我的解决方案:

List<String> persons = new ArrayList<>();
persons.add("John");
persons.add("John");
persons.add("MAX");
persons.add("Greg");
persons.stream()
          .distinct()
          .sorted()
          .collect(Collectors.toList());
List persons=new ArrayList();
人员。添加(“约翰”);
人员。添加(“约翰”);
添加(“最大值”);
人员。添加(“格雷格”);
人流()
.distinct()
.已排序()
.collect(Collectors.toList());

首先尝试做一些基础研究:。将其流式收集到一个集合:)事实上,如果你不关心顺序,只需制作一个集合。你这边的任何代码尝试?@9000使用集合都不会有帮助,因为他想让John退出结果,因为它有一个dup@阿尔法辛:那么,distinct也帮不上忙。正确的方法是将其简化为成对的映射(名称、计数),然后过滤计数=1的那些。首先尝试做一些基础研究:。将其流式收集到一个集合:)实际上,如果你不关心