Java / Java8 streams
How do I resolve IllegalStateException when Stream is reused?
Create new stream whenever required as shown below.
public class LargestAndSmallest { public static void main(String[] args) { Supplier<Stream<Integer>> myIntStream = () -> Stream.of(1, 2, 3, 44, 556, -1, 0, 2234); System.out.println("Largest = " + myIntStream.get().reduce(Integer::max)); System.out.println("Smallest = " + myIntStream.get().reduce(Integer::min)); } }
Output: Largest = Optional[2234] Smallest = Optional[-1]
More Related questions...