How to Iterate list of list of Java Objects using Java 8

Tags: , ,



How to iterate and get through list of list of objects using Java 8 Also need to get the count of distinct 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

Post Request body —>

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

Answer

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.

NOTE:: 1) Need to override hashcode and equals method to find the distinct() list and count based on customerId. 2) You can use flatMap to convert listOfList of objects in a single list of objects

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

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



Source: stackoverflow