从Java中的arraylist获取唯一对象的列表

时间:2018-06-28 05:43:48

标签: java java-stream

我有一个雇员列表,如下:

[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}]

我需要仅能够检索唯一的对象,并且在NID相同的地方,我需要检索具有最大ID的对象。所以最终列表应该像

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

我知道可以使用嵌套的for循环来实现,但是我想使用Java流来实现。通过使用分组依据,我可以找到列表中重复的员工,但是我仍然不知道如何获得上面的列表。

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

感谢帮助。

阿什利

6 个答案:

答案 0 :(得分:4)

我提出以下建议:

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

答案 1 :(得分:3)

您可以使用NID字段作为键,然后在合并功能中选择最高的ID字段来收集地图:

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

答案 2 :(得分:1)

您可以这样做

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

答案 3 :(得分:0)

尝试一下:

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

答案 4 :(得分:0)

您可以这样做:

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

答案 5 :(得分: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 +
                '}';
    }
}