-1

How to remove duplicates from a list of objects based on "String" property in Java 8? I can find code for Int or long property only but unable to find string comparison when string are difference case and get unique list.

Following is program i am trying to achieve.

public class Diggu {

    class Employee{
        private int id;
        private String name;
        public Employee(int id, String name){
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }



    }

    public void ra(){
        List<Employee> employee = Arrays.asList(new Employee(1, "John"), new Employee(3, "JOHN"), new Employee(2, "BOB"));
        System.out.println(""+ employee.size());
        List<Employee> unique = employee.stream()
                                .collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparing(Employee::getName))),
                                                           ArrayList::new));
        System.out.println(""+ unique.size());
    }
    public static void main(String[] args) {
        new Diggu().ra();
    }
}

Result:

run:
3
3
BUILD SUCCESSFUL (total time: 2 seconds)

Where it should be 3 and 2

fatherazrael
  • 5,511
  • 16
  • 71
  • 155
  • @HadiJ: How to use that in above situation when String are of different cases? – fatherazrael Apr 27 '18 at 08:42
  • @Oleksandr: I tried List unique2 = employee.stream().filter(distinctByKey(Employee::getName)) <- But this goes unreadable in Netbeans. – fatherazrael Apr 27 '18 at 08:43
  • I think `comparing` call the `equals()` method. But you want to call the `equalsIgnoreCase()` method. Change your code to call the right method – vincrichaud Apr 27 '18 at 08:44
  • 1
    [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) The answer, *"A lot. An absurd amount. More than you think you are capable of. After you have reached the end of your rope and the pain of not having the answer exceeds the vast amount of shame received by posting your question, that's when you can go ahead and ask. Because at that point, you will have done whatever research necessary to make it a good question worth asking!"* –  Apr 27 '18 at 08:47
  • 1
    This CASE_INSENSITIVE_ORDER thing is not mentioned in duplicate question for which i was looking. Even another duplicate question not related to Java 8. Question was edited a bit to show motive. Also the program i created is also from research done on stackoverflow. Even i tested duplicate question scenario but not successful. Thanks – fatherazrael Apr 27 '18 at 08:48
  • If the list supports removal, you can use `Set seen = new TreeSet<>( String .CASE_INSENSITIVE_ORDER); employee.removeIf(e -> !seen.add(e.getName()));` – Holger Apr 27 '18 at 09:19

1 Answers1

1

John and JOHN are different Strings, use a case insensitive Comparator

 Comparator.comparing(Employee::getName, String.CASE_INSENSITIVE_ORDER)
Eugene
  • 117,005
  • 15
  • 201
  • 306