0

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.

5
  • This is very unclear. What do BigObject and Parts look like, and what should your splitParts method do. Be as precise as the javadoc on such a method should be. Commented Apr 4, 2017 at 6:05
  • What should your splitParts method do? Be as precise as the javadoc on such a method should be. Commented Apr 4, 2017 at 6:11
  • I agree with @JBNizet that your question is very unclear. What do you mean by "dynamic params", "change based on input type", "calling a functional interface over reduce method"? None of these phrases make a lot of sense. You will need to reword your question to be less ambiguous if you want some effective help. Commented Apr 4, 2017 at 6:21
  • what would be the order of the List of Sets? If it's orderless, then you want the result to be a Collection of Sets Commented Apr 4, 2017 at 6:33
  • @f.khantsis I want List of Set coz I want to index the elements later on. Commented Apr 4, 2017 at 6:57

1 Answer 1

4

If you want a List<Set<Parts>>, first change your code to get a 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:

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()); 
Sign up to request clarification or add additional context in comments.

8 Comments

Dynamic in what way? Collectors.groupingBy() requires a Function<Parts,String> as the first parameter. You can declare a variable of that type and assign any compatible method reference, lambda expression, or anonymous implementation to it.
Why do you "assume the complexity will be very high"? The entire operation is O(n).
@user2340345: can you stop using the word “dynamic” and just describe what you want to do?
@user2340345 You can partition any way you want, as long as you can come up with a String value that defines the partition. All Parts that map to the same String value will be in the same partition, as mapped by the Function<Parts,String> you supply to groupingBy.
Actually, using the single chain variant, there isn’t even a dependency to the group criterion type, so any Function<Parts,?> will do; there’s no requirement to produce a String anymore. In the two step variant, you have to change the map declaration to Map<?, Set<Parts>> parts to be independent of the group criterion type. Then, it can also accept any Function<Parts,?>.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.