/** * This test demonstrates the known problem of creating streams from mutable structures. * * <p>Some of the ways streams created in this way can fail is: - weak stream references getting * garbage collected - underlying mutable data structure changes - iterator gets updated (e.g. * iterator used to create 2 different streams). */ @Test(expected = ConcurrentModificationException.class) public void iterableStreamWithStructureUpdate() { java.util.List<Integer> list = List.list(1, 2, 3).toJavaList(); Stream<Integer> s1 = Stream.fromIterable(list); int x = s1.head(); list.remove(1); Stream<Integer> s2 = s1.tail()._1(); x = s2.head(); }
/** * Calculates the minimum of this elements according to their natural order. * * @return {@code Some(minimum)} of this elements or {@code None} if this is empty or this * elements are not comparable */ @SuppressWarnings("unchecked") default Option<T> min() { final Stream<T> stream = Stream.ofAll(iterator()); if (isEmpty() || !(stream.head() instanceof Comparable)) { return None.instance(); } else { return stream.minBy((o1, o2) -> ((Comparable<T>) o1).compareTo(o2)); } }