I'm trying to use this method to get a random element from a stream. Why does it always print "9"?
I know I can use list.get(new Random().nextInt(list.size()))
but I only want to use the Stream API, even though it may not be efficient. How can I do that?
Integer[] array = {1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14};
List<Integer> list = Arrays.asList(array);
Integer anyElement = list.stream()
.parallel()
.findAny()
.get();
System.out.println(anyElement);