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

Collection.removeIf fails silently

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.8.0_92"
      Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Darwin 13.4.0 Darwin Kernel Version 13.4.0: Mon Jan 11 18:17:34 PST 2016; root:xnu-2422.115.15~1/RELEASE_X86_64 x86_64 i386 MacBookPro11,2 Darwin


      A DESCRIPTION OF THE PROBLEM :
      Collection.removeIf() returns true even though no element gets deleted from the collection.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
          @Test
          public void testRemoveIf() {
              class A {
                  String s;
                  int i;

                  A(String s, int i) {
                      this.s = s;
                      this.i = i;
                  }

                  @Override
                  public int hashCode() {
                      final int prime = 31;
                      int result = 1;
                      result = prime * result + i;
                      result = prime * result + ((s == null) ? 0 : s.hashCode());
                      return result;
                  }

                  @Override
                  public boolean equals(Object obj) {
                      if (this == obj)
                          return true;
                      if (obj == null)
                          return false;
                      if (getClass() != obj.getClass())
                          return false;
                      A other = (A) obj;
                      if (i != other.i)
                          return false;
                      if (s == null) {
                          if (other.s != null)
                              return false;
                      } else if (!s.equals(other.s))
                          return false;
                      return true;
                  }
              }

              Set<A> set = new HashSet<>(Arrays.asList(new A("one", 1)));
              assertEquals(1, set.size());
              set.iterator().next().s = "two";
              assertTrue(set.removeIf(e -> e.i == 1));
              assertEquals(1, set.size());
          }
      ---------- END SOURCE ----------

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

              Created:
              Updated:
              Resolved: