src/share/classes/java/util/stream/package-info.java

Print this page
rev 7597 : 8015318: Extend Collector with 'finish' operation
Reviewed-by:
Contributed-by: brian.goetz@oracle.com

*** 545,565 **** * <p>For example, consider the following code: * <pre>{@code * List<String> l = new ArrayList(Arrays.asList("one", "two")); * Stream<String> sl = l.stream(); * l.add("three"); ! * String s = sl.collect(toStringJoiner(" ")).toString(); * }</pre> * First a list is created consisting of two strings: "one"; and "two". Then a stream is created from that list. * Next the list is modified by adding a third string: "three". Finally the elements of the stream are collected * and joined together. Since the list was modified before the terminal {@code collect} operation commenced * the result will be a string of "one two three". However, if the list is modified after the terminal operation * commences, as in: * <pre>{@code * List<String> l = new ArrayList(Arrays.asList("one", "two")); * Stream<String> sl = l.stream(); ! * String s = sl.peek(s -> l.add("BAD LAMBDA")).collect(toStringJoiner(" ")).toString(); * }</pre> * then a {@code ConcurrentModificationException} will be thrown since the {@code peek} operation will attempt * to add the string "BAD LAMBDA" to the list after the terminal operation has commenced. */ --- 545,565 ---- * <p>For example, consider the following code: * <pre>{@code * List<String> l = new ArrayList(Arrays.asList("one", "two")); * Stream<String> sl = l.stream(); * l.add("three"); ! * String s = sl.collect(joining(" ")); * }</pre> * First a list is created consisting of two strings: "one"; and "two". Then a stream is created from that list. * Next the list is modified by adding a third string: "three". Finally the elements of the stream are collected * and joined together. Since the list was modified before the terminal {@code collect} operation commenced * the result will be a string of "one two three". However, if the list is modified after the terminal operation * commences, as in: * <pre>{@code * List<String> l = new ArrayList(Arrays.asList("one", "two")); * Stream<String> sl = l.stream(); ! * String s = sl.peek(s -> l.add("BAD LAMBDA")).collect(joining(" ")); * }</pre> * then a {@code ConcurrentModificationException} will be thrown since the {@code peek} operation will attempt * to add the string "BAD LAMBDA" to the list after the terminal operation has commenced. */