简体   繁体   English

根据不同的参数将Collection(使用Java 8流)拆分为较小的组件

[英]Split a Collection(using java 8 streams) into smaller components based on different parameters

This is a continuation of this question Java 8 stream compare two objects and run a function on them 这是这个问题的延续Java 8流比较两个对象并在它们上运行一个函数

Class BigRequest{
    String bId;
    List<Parts> parts;
    //getters and setter here
    }

Class Parts{
String pId;
String partId;
//getters and setter here
}

My question is to split a Collection(using java 8 streams) into smaller components based on some dynamic parameters. 我的问题是根据一些动态参数将Collection(使用Java 8流)拆分为较小的组件。 The Dynamic params can change based on different conditions (here, Ive given example as getPartId() being the same) passed to the method splitParts, whose return type is List of Set of Parts 动态参数可以根据传递给方法splitParts的不同条件(此处给出的示例getPartId()相同)进行更改,该方法的返回类型为“零件集列表”

List<Set<Parts>> splitParts(BigRequest big){}

I've tried the following: 我尝试了以下方法:

 //groupingBy function but that returns a Map 
   Map<String, List<Parts>> parts = big.getParts().stream().collect(Collectors.groupingBy(Parts::getPartId));

//doesn't do it accurately
    big.getParts().stream().filter(p -> p.getPartId.equals(p.getPartId())).collect();

How do I split a stream based on the dynamic params(maybe by calling a functional interface over reduce method) and then run a method over it? 如何基于动态参数拆分流(也许通过在reduce方法上调用功能接口)然后在其上运行方法?

My function's signature is 我函数的签名是

List<Set<Parts>> splitParts(BigObject b){}

I assume doing something like this might help to keep the splitting condition dynamic, but Im not sure how to do it? 我认为做这样的事情可能有助于保持分裂条件的动态,但是我不确定该怎么做?

 big.getParts().stream().reduce(MyFuncInterface::divide);

and then using forEach(), I think i can iterate over the List and call the abc() method. 然后使用forEach(),我想我可以遍历List并调用abc()方法。

How can I do it better? 我该如何做得更好?

Any help here is greatly appreciated. 非常感谢您的任何帮助。

If you want a List<Set<Parts>> , first change your code to get a Map<String, Set<Parts>> : 如果要使用List<Set<Parts>> ,请首先更改代码以获取Map<String, Set<Parts>>

Map<String, Set<Parts>> parts = big.getParts().stream().collect(
        Collectors.groupingBy(Parts::getPartId, Collectors.toSet()));

Calling parts.values() will return a Collection<Set<Parts>> , so convert the Collection to a List : 调用parts.values()将返回Collection<Set<Parts>> ,因此将Collection转换为List

List<Set<Parts>> list = new ArrayList<>(parts.values());

Or you could just re-stream it, so you can do it in a single call chain: 或者,您可以只重播它,因此可以在单个调用链中完成它:

List<Set<Parts>> list = big.getParts()
                           .stream()
                           .collect(Collectors.groupingBy(Parts::getPartId,
                                                          Collectors.toSet()))
                           .values()
                           .stream()
                           .collect(Collectors.toList());

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

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