This is a continuation of this question Java 8 stream compare two objects and run a function on them
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. 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
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?
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.
How can I do it better?
Any help here is greatly appreciated.
ListofSets? If it's orderless, then you want the result to be aCollectionofSets