Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8251333

Wrapped HashSet stream ignores adding elements to set.

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      If stream was created from empty HashSet and wrapped in another stream with concat, and then value added into set, the stream would remain empty and not throw ConcurrentModificationException on collect operation.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create empty HashSet.
      Create concatenated stream of set stream and empty stream.
      Add value "test" to set.
      Collect created stream to some collection.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The resulting collection contains added "test" value.

      Or collect method throws ConcurrentModificationException, like ArrayList do in the same scenario.
      ACTUAL -
      The resulting collection is empty, value "test" added to set is ignored by stream.

      But if HashSet stream is not wrapped into concat stream, the resulting collection would contain added value.

      ---------- BEGIN SOURCE ----------
      import static org.hamcrest.Matchers.contains;
      import static org.junit.Assert.assertThat;

      import java.util.HashSet;
      import java.util.List;
      import java.util.Set;
      import java.util.stream.Collectors;
      import java.util.stream.Stream;

      import org.junit.Test;

      public class ConcatConcurentModificationHashSetStream {
          @Test
          public void testConcatConcurentModificationStream() {
              final Set<String> set = new HashSet<>();
              final Stream<String> stream = Stream.concat(
                      set.stream(),
                      Stream.of());
              set.add("test");
              final List<String> collect = stream.collect(Collectors.toList());
              assertThat(set, contains("test"));
              assertThat(collect, contains("test"));
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Don't concat stream from HashSet with other streams, if collection would be populated after stream variable created. Instead collect it to separate collection.

      FREQUENCY : always


            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: