有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用流API从java中的对象列表中删除重复项

我已经找到了一个解决方案,可以从列表中删除重复项 使用流API

我只发现了这个问题 How to remove duplicates from list of objects by id

我有一个需要按人名筛选的人的列表,尝试使用下面的代码段,但它不按人名筛选

private static Map<Person, Integer> getUniqueByBiggerId(Collection<Person> persons) {
        return persons.stream().collect(Collectors.toMap(
                persons ->
                        persons,
                Person::getId,
                (id1, id2) -> {
                    if (id2 > id1)
                        return id2;
                    else
                        return id1;
                }
        ));
    }

public  static void main(String args[]){
   //.........
    List<Person> Persons
            = Arrays.asList(Person1, Person2,Person2,Person1,Person2, Person1);

    getUniqueByBiggerId(Persons);

}

共 (3) 个答案

  1. # 1 楼答案

    假设您有一个Person类,该类包含字段idname,并且字段id是您要筛选的字段,您只需要重写equals和hashCode方法:

    public class Person {
    
        private final int id;
    
        private final String name;
    
        public Person(int id, String name) {
            this.id = id;
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
    
        public String getName() {
            return name;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Person person = (Person) o;
            return id == person.id;
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(id);
        }
    

    然后你可以创建一个列表:

    Person personOne = new Person(1, "Name1");
    Person personTwo = new Person(2, "Name2");
    Person personThree = new Person(1, "Name3");
    List<Person> people = Arrays.asList(personOne, personTwo, personThree);
    

    要消除重复,只需写下:

    List<Person> collect = people.stream().distinct().collect(Collectors.toList());

    System.out.println(collect.size());

    这将产生输出2

  2. # 2 楼答案

    您非常接近解决方案:

    public class Test {
        public static void main(String[] args) throws Exception {
            List<Person> persons = Arrays.asList(new Person("a", 2), new Person("b", 1), new Person("a", 3));
            persons = getUniqueByBiggerId(persons);
            System.out.println(persons);
    
        }
    
        private static List<Person> getUniqueByBiggerId(Collection<Person> persons) {
            return new ArrayList<>(
                    persons.stream().collect(Collectors.toMap(Person::getName, Function.identity(), (p1, p2) -> {
                        if (p1.getId() > p2.getId())
                            return p1;
                        else
                            return p2;
                    })).values());
        }
    }
    
    record Person(String name, int id) {
        public String getName() {
            return name;
        }
    
        public int getId() {
            return id;
        }
    }
    

    输出:

    [Person[name=a, id=3], Person[name=b, id=1]]
    
  3. # 3 楼答案

    private static Map<String, Person> getUniqueByBiggerId(Collection<Person> person) {
            return harmonizedDimensions.stream().collect(Collectors.toMap(()
                    person ->
                    person.getName(),
                    Function.identity(),
                    (id1, id2) -> {
                        if (id2.getId() > id1.getId())
                            return id2;
                        else
                            return id1;
                    }
            )).values();
        }