简体   繁体   English

如何使用 Java 8 迭代 Java 对象列表的列表

[英]How to Iterate list of list of Java Objects using Java 8

How to iterate and get through list of list of objects using Java 8 Also need to get the count of distinct customerIds如何使用 Java 8 迭代和通过对象列表的列表还需要获取不同customerIds的计数

final List<CustomerIssues> issues = customerIssues.collectList().block();
    
for (final CustomerIssues e : issues) {
    final List<CustomerEditVO> v = e.getData();
    for (final CustomerEditVO edit : v) {
        System.out.println("Id " + edit.getCustomerId());     
    }
    System.out.println("Comments " + e.getComments());
}   

public class CustomerIssues {
    private List<CustomerEditVO> data; 
    private String comments;
}

public class CustomerEditVO { 
    private String customerId;
    private Integer units;
}

CustomerEditVO and CustomerIssues are POJOs CustomerEditVOCustomerIssues是 POJO

Post Request body --->发布请求正文 --->

{
   "data":[
      {
         "units":"176",
         "CustomerId":"122"
      },
      {
         "units":"400",
         "CustomerId":"1998"
      }
   ],
   "comments" :"Testing"
}

I have created all the given POJO's and created the objects and list for your query.And also overrides the toString() method in order to clearly show the objects in the list output and also extracted and print the list at each operation.我已经创建了所有给定的 POJO,并为您的查询创建了对象和列表。并且还覆盖了 toString() 方法,以便清楚地显示列表 output 中的对象,并在每个操作中提取并打印列表。

NOTE:: 1) Need to override hashcode and equals method to find the distinct() list and count based on customerId.注意:: 1) 需要重写 hashcode 和 equals 方法来查找 distinct() 列表并根据 customerId 计数。 2) You can use flatMap to convert listOfList of objects in a single list of objects 2)您可以使用flatMap将listOfList对象转换为单个对象列表

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {

        CustomerIssues issues1 =
                new CustomerIssues(Collections.singletonList(new CustomerEditVO("customer1", 100)),"comment1");
        CustomerIssues issues2 =
                new CustomerIssues(Collections.singletonList(new CustomerEditVO("customer2", 200)),"comment2");
        CustomerIssues issues3 =
                new CustomerIssues(Collections.singletonList(new CustomerEditVO("customer3", 300)),"comment3");
        CustomerIssues issues4 =
                new CustomerIssues(Collections.singletonList(new CustomerEditVO("customer4", 400)),"comment4");
        CustomerIssues issues5 =
                new CustomerIssues(Collections.singletonList(new CustomerEditVO("customer5", 500)),"comment5");
        CustomerIssues issues6 =
                new CustomerIssues(Collections.singletonList(new CustomerEditVO("customer5", 600)),"comment6");
        CustomerIssues issues7 =
                new CustomerIssues(Collections.singletonList(new CustomerEditVO("customer5", 700)),"comment7");
        CustomerIssues issues8 =
                new CustomerIssues(Collections.singletonList(new CustomerEditVO("customer1", 800)),"comment8");
        CustomerIssues issues9 =
                new CustomerIssues(Collections.singletonList(new CustomerEditVO("customer2", 900)),"comment9");
        CustomerIssues issues10 =
                new CustomerIssues(Collections.singletonList(new CustomerEditVO("customer3", 1000)),"comment10");

        List<CustomerIssues> customerIssuesList =
                Arrays.asList(issues1,issues2,issues3,issues4,issues5,issues6,issues7,issues8,issues9,issues10);
        System.out.println("Total issues:: " + customerIssuesList + "\n");

        List<List<CustomerEditVO>> listOfListCustomerEditVos =
                customerIssuesList.stream().map(CustomerIssues::getData).collect(Collectors.toList());
        System.out.println("Total listOfListCustomerEditVos:: " + listOfListCustomerEditVos + "\n");

        List<CustomerEditVO> listOfCustomerEditVos =
                listOfListCustomerEditVos.stream().flatMap(Collection::stream).collect(Collectors.toList());
        System.out.println("Total listOfCustomerEditVos:: " + listOfCustomerEditVos + "\n");

        List<String> listOfDistinctCustomerIds = listOfCustomerEditVos.stream().map(CustomerEditVO::getCustomerId)
                .distinct().collect(Collectors.toList());

        System.out.println("List of distinct customer Ids:: " + listOfDistinctCustomerIds + "\n");
        System.out.println("Distinct customer Ids count:: " + listOfDistinctCustomerIds.size() + "\n");

    }
}
import java.util.List;

public class CustomerIssues {

    private List<CustomerEditVO> data;
    private String comments;

    public CustomerIssues(List<CustomerEditVO> data, String comments) {
        this.data = data;
        this.comments = comments;
    }

    public List<CustomerEditVO> getData() {
        return data;
    }

    public void setData(List<CustomerEditVO> data) {
        this.data = data;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    @Override
    public String toString() {
        return "CustomerIssues{" +
                "data=" + data +
                ", comments='" + comments + '\'' +
                '}';
    }
}
import java.util.Objects;

public class CustomerEditVO {

    private String customerId;
    private Integer units;

    public CustomerEditVO(String customerId, Integer units) {
        this.customerId = customerId;
        this.units = units;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CustomerEditVO that = (CustomerEditVO) o;
        return customerId.equals(that.customerId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(customerId);
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    public Integer getUnits() {
        return units;
    }

    public void setUnits(Integer units) {
        this.units = units;
    }

    @Override
    public String toString() {
        return "CustomerEditVO{" +
                "customerId='" + customerId + '\'' +
                ", units=" + units +
                '}';
    }
}

Output Output

Total issues:: [CustomerIssues{data=[CustomerEditVO{customerId='customer1', units=100}], comments='comment1'}, CustomerIssues{data=[CustomerEditVO{customerId='customer2', units=200}], comments='comment2'}, CustomerIssues{data=[CustomerEditVO{customerId='customer3', units=300}], comments='comment3'}, CustomerIssues{data=[CustomerEditVO{customerId='customer4', units=400}], comments='comment4'}, CustomerIssues{data=[CustomerEditVO{customerId='customer5', units=500}], comments='comment5'}, CustomerIssues{data=[CustomerEditVO{customerId='customer5', units=600}], comments='comment6'}, CustomerIssues{data=[CustomerEditVO{customerId='customer5', units=700}], comments='comment7'}, CustomerIssues{data=[CustomerEditVO{customerId='customer1', units=800}], comments='comment8'}, CustomerIssues{data=[CustomerEditVO{customerId='customer2', units=900}], comments='comment9'}, CustomerIssues{data=[CustomerEditVO{customerId='customer3', units=1000}], comments='comment10'}]

Total listOfListCustomerEditVos:: [[CustomerEditVO{customerId='customer1', units=100}], [CustomerEditVO{customerId='customer2', units=200}], [CustomerEditVO{customerId='customer3', units=300}], [CustomerEditVO{customerId='customer4', units=400}], [CustomerEditVO{customerId='customer5', units=500}], [CustomerEditVO{customerId='customer5', units=600}], [CustomerEditVO{customerId='customer5', units=700}], [CustomerEditVO{customerId='customer1', units=800}], [CustomerEditVO{customerId='customer2', units=900}], [CustomerEditVO{customerId='customer3', units=1000}]]

Total listOfCustomerEditVos:: [CustomerEditVO{customerId='customer1', units=100}, CustomerEditVO{customerId='customer2', units=200}, CustomerEditVO{customerId='customer3', units=300}, CustomerEditVO{customerId='customer4', units=400}, CustomerEditVO{customerId='customer5', units=500}, CustomerEditVO{customerId='customer5', units=600}, CustomerEditVO{customerId='customer5', units=700}, CustomerEditVO{customerId='customer1', units=800}, CustomerEditVO{customerId='customer2', units=900}, CustomerEditVO{customerId='customer3', units=1000}]

List of distinct customer Ids:: [customer1, customer2, customer3, customer4, customer5]

Distinct customer Ids count:: 5

I hope this will solve your problem.我希望这能解决你的问题。

Thanks Girdhar谢谢吉达尔

If it is needed to count the number of distinct customerId in CustomerEditVO it may be implemented in a simpler way by flattening the list of CustomerIssues using Stream::flatMap , followed by Stream::distinct and Stream::count : If it is needed to count the number of distinct customerId in CustomerEditVO it may be implemented in a simpler way by flattening the list of CustomerIssues using Stream::flatMap , followed by Stream::distinct and Stream::count :

long distinctCustomerIds = issues.stream() // Stream<CustomerIssues>
        .flatMap(issue -> issue.getData().stream()) // Stream<CustomerEditVO>
        .map(CustomerEditVO::getCustomerId)
        .distinct()
        .count();

To iterate the list of issues using Java 8 facilities may be implemented using Collection::stream or Collection::parallelStream as well as by Iterable::forEach .要使用 Java 迭代问题列表,可以使用Collection::streamCollection::parallelStream以及Iterable::forEach来实现 8 个设施。

You can count them as simple as this:您可以像这样简单地计算它们:

long count = issues.stream()
                   .map(CustomerIssues::getData) 
                   .flatMap(List::stream)
                   .map(CustomerEditVO::getCustomerId)
                   .distinct()
                   .count();

Or you could even chain your Flux calls with Stream API calls (although that might make the code a little harder to read:或者您甚至可以将您的Flux调用与Stream API调用链接起来(尽管这可能会使代码更难阅读:

long count = customerIssues.collectList()
                           .block()
                           .stream()
                           .map(CustomerIssues::getData) 
                           .flatMap(List::stream)
                           .map(CustomerEditVO::getCustomerId)
                           .distinct()
                           .count();

Try this:尝试这个:

long i=issues.stream().peek(e->System.out.println(e.getComments()))
    .map(CustomerIssues::getData)
    .collect(ArrayList<CustomerEditVO>::new, (bl, l) -> {
      l.stream().forEach(cev -> System.out.println("Id " + cev.getCustomerId()));
      ((List<CustomerEditVO>)bl).addAll(l);
      }, List::addAll)
     .stream().distinct().count();

System.out.println("distinct customers: " + i);

Also add equals and hashCode to your customer vo like this:还向您的客户 vo 添加 equals 和 hashCode ,如下所示:

class CustomerEditVO {
    String customerId;
    Integer units;
    
    public CustomerEditVO(String ci, Integer unit) {
        this.customerId = ci;
        this.units = unit;
    }
    public String getCustomerId() {
        return this.customerId;
    }
    @Override
    public boolean equals(Object o) {
        if(o instanceof CustomerEditVO == false) return false;
        return this.customerId.equals(((CustomerEditVO)o).customerId);
    }
    @Override
    public int hashCode() {
        return this.customerId.hashCode(); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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