1

I have a list of employees as follows:

[Employee{id="1", NID="A123", wages=5000},
 Employee{id="2", NID="B123", wages=1000},
 Employee{id="3", NID="C123", wages=2000},
 Employee{id="4", NID="C123", wages=3000}]

I need to be able to retrieve only the unique objects and where NID is the same, I need to retrieve the one with the max Id. So the final list should be like

[Employee{id="1", NID="A123", wages=5000},
 Employee{id="2", NID="B123", wages=1000},
 Employee{id="4", NID="C123", wages=3000}]

I know it is possible to do it with nested for loops but I want to do it with Java streams. I could find which employee are duplicate in the list by using the group by but I still can't figure how to get the list like above.

 Map<String, List<Employee>> groupByNid = employeeList.stream().collect(Collectors.groupingBy(Employee::getNID));

Thanks to help.

Ashley

0

5 Answers 5

5

I propose the following:

    Collection<Employee> filteredEmployees = employeeList.stream()
            .collect(Collectors.toMap(
                    Employee::getNID, Function.identity(),
                    BinaryOperator.maxBy(Comparator.comparing(Employee::getID))
            )).values();
Sign up to request clarification or add additional context in comments.

1 Comment

Note: Turns out it's nearly identical to Ernest Kiwele answer, only it uses BinaryOperator.maxBy and compares ID instead of NID, as requested by the OP.
3

You can collect to map, using the NID field for keys, and then selecting the highest ID field in the merge function:

List<Employee> employeeList = ...;
Collection<Employee> uniqueEmployees = employeeList.stream()
        .collect(Collectors.toMap(Employee::getNID, 
                Function.identity(), 
                (e1,e2) -> e1.getID().compareTo(e2.getID()) > 0 ? e1: e2)
        ).values();

Comments

0

Try This:

    List<Employee> list = new ArrayList<>();

    list.add(new Employee(1,"A123",5000));
    list.add(new Employee(2,"B123",1000));
    list.add(new Employee(3,"C123",2000));
    list.add(new Employee(4,"C123",2000));


    Set<String> empSet = new HashSet<>();
    list.removeIf(p -> !empSet.add(p.getNid()));
    list.forEach(emp->System.out.println(emp.getId() +" : "+emp.getNid()+" :"+emp.getWage()));

Comments

0

You can do it like:

employeeList.stream().collect(Collectors.toMap(
        Employee::getNID, r -> r, (e1, e2) ->  e1.getId().compareTo(e2.getId()) > 0 ? e1 : e2));

Comments

0
@Test
public void t() {
    Employee e1 = new Employee(1, "A123", 400);
    Employee e2 = new Employee(2, "A123", 400);

    List<Employee> list = Arrays.asList(e1,e2);

    list = list.stream().filter(distinctByNid(Employee::getNid)).collect(Collectors.toList());
    System.out.println(list);
}

// use a thread safe set to dedup
public static <T> Predicate<T> distinctByNid(Function<? super T, ?> keyExtractor) {
    Set<Object> seen = ConcurrentHashMap.newKeySet();
    return t -> seen.add(keyExtractor.apply(t));
}

static class Employee {
    private int id;
    private String nid;
    private int wages;

    public Employee(int id, String nid, int wages) {
        this.id = id;
        this.nid = nid;
        this.wages = wages;
    }

    public int getId() {
        return id;
    }

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

    public String getNid() {
        return nid;
    }

    public void setNid(String nid) {
        this.nid = nid;
    }

    public int getWages() {
        return wages;
    }

    public void setWages(int wages) {
        this.wages = wages;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", nid='" + nid + '\'' +
                ", wages=" + wages +
                '}';
    }
}

Comments

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.