简体   繁体   中英

get list of unique objects from an arraylist in java

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

I propose the following:

    Collection<Employee> filteredEmployees = employeeList.stream()
            .collect(Collectors.toMap(
                    Employee::getNID, Function.identity(),
                    BinaryOperator.maxBy(Comparator.comparing(Employee::getID))
            )).values();

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

You can do it like so,

Collection<Employee> emps = employees.stream()
        .collect(Collectors.collectingAndThen(Collectors.toMap(Employee::getNID, Function.identity(),
                (e1, e2) -> e1.getId().compareTo(e2.getId()) > 0 ? e1 : e2), m -> m.values()));

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

You can do it like:

employeeList.stream().collect(Collectors.toMap(
        Employee::getNID, r -> r, (e1, e2) ->  e1.getId().compareTo(e2.getId()) > 0 ? e1 : e2));
@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 +
                '}';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM