I have a HashSet, I need to collect [0..n] elements in one HashSet and [n+1...l] elements in another HashSet using Java 8 streams. Where n >=0 is an arbitrary number, l >=0 is the length of given HashSet.
resultSet1 = set.stream()
.limit(n)
.collect(Collectors.toSet());
The above gives me [0..n] elements.
I could again call stream() and then skip(), but the thing is HashSet in java is unordered, so there is a probability that the skip method skips the element which was not collected above.
resultSet2 = set.stream()
.skip(n)
.collect(Collectors.toSet());
I can use traditional for, but a solution using streams will be great.
E.g.
given set = {1,2,3,4,5}
resultSet1 = {1,2}
resultSet2 = {3,4,5}