1

I have searched for a solution for how to remove duplicates from a list with Stream API

found only this question How to remove duplicates from list of objects by id

I have a list of Person i need to filter by the person name, tried with the below snippet but it doesn't filter by the name

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);

}
4
  • 1
    What are the expected input and outputs? It is not clear what you mean by filtering on name Commented Apr 12, 2021 at 5:52
  • Do yo really need a Map or just a List as result? Commented Apr 12, 2021 at 14:01
  • @alex87 List is good also Commented Apr 13, 2021 at 6:39
  • I have posted a possible solution for you. Commented Apr 13, 2021 at 8:48

3 Answers 3

1

You were very close to the solution:

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;
    }
}

Output:

[Person[name=a, id=3], Person[name=b, id=1]]
Sign up to request clarification or add additional context in comments.

Comments

0
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();
    }

1 Comment

You can simplify (id1, id2) -> { if (id2.getId() > id1.getId()) return id2; else return id1; } to (id1, id2) -> id2.getId() > id1.getId()? id2: id1
0

Assuming you have a Person class which has fields id and name and the field id is the field you want to filter by you just need to override equals and hashCode methods:

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);
    }

Then you can just create a List:

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);

And to eliminate duplicates just write:

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

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

That will produce the output 2.

1 Comment

If you want to filter by name you just need to change equals and hashCode implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.