【问题标题】:How to Iterate list of list of Java Objects using Java 8如何使用 Java 8 迭代 Java 对象列表
【发布时间】:2021-02-27 03:49:45
【问题描述】:

如何使用 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;
}

CustomerEditVOCustomerIssues 是 POJO

发布请求正文 --->

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

【问题讨论】:

    标签: java java-8 java-stream


    【解决方案1】:

    我已经创建了所有给定的 POJO,并为您的查询创建了对象和列表。并且还覆盖了 toString() 方法,以便在列表输出中清楚地显示对象,并在每个操作中提取和打印列表。

    注意:: 1) 需要重写 hashcode 和 equals 方法来查找 distinct() 列表并根据 customerId 计数。 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 +
                    '}';
        }
    }
    

    输出

    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
    

    我希望这能解决你的问题。

    谢谢 格达尔

    【讨论】:

    • 如何获得 cmets?
    • List&lt;String&gt; listOfComments = customerIssuesList.stream() .map(CustomerIssues::getComments) .collect(Collectors.toList()); System.out.println("Total comments:: " + listOfComments + "\n"); 输出: 总 cmets:: [comment1、comment2、comment3、comment4、comment5、comment6、comment7、comment8、comment9、comment10]我>
    • 评论不是列表。
    • 实际上我已经获取了列表中所有对象的总 cmets
    【解决方案2】:

    试试这个:

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

    还向您的客户 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(); 
        }
    }
    

    【讨论】:

      【解决方案3】:

      如果需要计算CustomerEditVO 中不同customerId 的数量,可以通过使用Stream::flatMap 展平CustomerIssues 列表,然后是Stream::distinctStream::count,以更简单的方式实现:

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

      要使用 Java 8 工具迭代问题列表,可以使用 Collection::streamCollection::parallelStream 以及 Iterable::forEach 来实现。

      【讨论】:

        【解决方案4】:

        你可以这么简单地计算它们:

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

        或者您甚至可以将您的 Flux 调用与 Stream API 调用链接起来(尽管这可能会使代码更难阅读:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-04
          • 1970-01-01
          • 1970-01-01
          • 2015-02-11
          • 2013-09-26
          • 2015-06-11
          相关资源
          最近更新 更多