流 API java-8

问题描述 投票:0回答:1

我编写了代码以从员工列表中删除重复的 ID,并打印唯一的 ID 及其所有详细信息。

所以在这里我可以删除重复项并打印唯一的,但其他员工详细信息不打印

您能帮我解决这个问题,打印员工姓名、工资及其独特的详细信息吗?

这是我的代码片段

员工POJO

包 com.demo.streams;

公开课员工{

Integer empId;
String name;
Double salary;

public Employee(Integer empId, String name, Double salary) {
    this.empId = empId;
    this.name = name;
    this.salary = salary;
}

public Integer getEmpId() {
    return empId;
}

public void setEmpId(Integer empId) {
    this.empId = empId;
}

public String getName() {
    return name;
}

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

public Double getPhone() {
    return salary;
}

public void setCity(Long phone) {
    this.salary = salary;
}

@Override
public String toString() {

    return "Name :" + name + "  Phone :" + salary + "   Emp Id :" + empId;
}

}

我的主课

公共类RemoveDuplicateId {

public static void main(String[] args) {

    List<Employee> listObj = Arrays.asList(new Employee(5, "Raju", 8967452, "Infosys"),
            new Employee(6, "Shayam", 78564589, "Tcs Ltd"), new Employee(3, "Mantu", 677890, "Wipro"),
            new Employee(5, "Shayam", 878965, "Tech-Mahindra"), new Employee(8, "Guudu", 987653, "Cisco"));
    List<Integer> id = listObj.stream().map(e -> e.geteId()).distinct().collect(Collectors.toList());
    for (Integer sorted : id) {
        System.out.println(sorted);
    }

}

}

输出

5 6 3 8

所以在这里我可以删除重复项并打印唯一的,但其他员工详细信息不打印

您能帮我解决这个问题,打印员工姓名、工资及其独特的详细信息吗?

这是我的代码片段

员工POJO

包 com.demo.streams;

公开课员工{

Integer empId;
String name;
Double salary;

public Employee(Integer empId, String name, Double salary) {
    this.empId = empId;
    this.name = name;
    this.salary = salary;
}

public Integer getEmpId() {
    return empId;
}

public void setEmpId(Integer empId) {
    this.empId = empId;
}

public String getName() {
    return name;
}

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

public Double getPhone() {
    return salary;
}

public void setCity(Long phone) {
    this.salary = salary;
}

@Override
public String toString() {

    return "Name :" + name + "  Phone :" + salary + "   Emp Id :" + empId;
}

}

我的主课

公共类RemoveDuplicateId {

public static void main(String[] args) {

    List<Employee> listObj = Arrays.asList(new Employee(5, "Raju", 8967452, "Infosys"),
            new Employee(6, "Shayam", 78564589, "Tcs Ltd"), new Employee(3, "Mantu", 677890, "Wipro"),
            new Employee(5, "Shayam", 878965, "Tech-Mahindra"), new Employee(8, "Guudu", 987653, "Cisco"));
    List<Integer> id = listObj.stream().map(e -> e.geteId()).distinct().collect(Collectors.toList());
    for (Integer sorted : id) {
        System.out.println(sorted);
    }

}

}

输出

5 6 3 8

stream java-stream
1个回答
0
投票

如果您需要其他属性,请不要映射到

id
。例如,您可以使用 id 作为键将 Employee 存储在地图中,如果存在重复的 id,则根据您的要求保留第一个或最后一个看到的 ID,并获取该地图的值

List<Employee> empsDistinctById = new ArrayList<>(
        listObj.stream()
               .collect(Collectors.toMap(Employee::getEmpId, emp -> emp, (first, second) -> first))
               .values());

for (Employee emp : empsDistinctById) {
    System.out.println(emp);
}
© www.soinside.com 2019 - 2024. All rights reserved.