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

java.util.HashSet add(Object o) when null is added twice it is accepted build75

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.4.0
    • core-libs

      java.util.HashSet add(Object o) when null is added twice it is accepted
      in jdk1.4 build75.
      And returns true.
      As per spec. it is suppose to add once only and second time return false.
      In jdk1.4 build74 program was passing.
      ---------------------------------
      Also after null is added to hashSet returns true.
      and followed by hashSet.contains(null) returns false now(must return true)
      and hashSet.remove(null) returns false now(must return true)
      ---------------------------------------------------------------------
      Following is the test program:
      import java.util.*;
      import java.io.*;
       
      public class Add {
          
          public static void main(String[] args) throws Exception {
              
               AddTest01();
      }
           
        
        public static boolean AddTest01() {

      boolean bReturn = false;
             try {
                  HashSet hashSet = new HashSet();
                  if (!hashSet.add(null)) {
                      throw new Exception("!hashSet.add(null) not ok in AddTest01()");
                  }
                  if (hashSet.size() !=1) {
                     throw new Exception("(hashSet.size() !=1 not ok in AddTest01");
                  }
                  //add second time null must return false and size remain same
                  if (hashSet.add(null)) {
                      throw new Exception("hashSet.add(null) not ok in AddTest01()");
                  }
                  
                  bReturn = true;
                  System.out.println("AddTest01() Pass");
              } catch(Exception e) {
                  bReturn = false;
                  System.out.println("Exception thrown = " + e);
                  System.out.println("AddTest01() Fail");
                  e.printStackTrace();
             }
             return bReturn;
          }

      }

      ------------------------contains() and remove()--------------------------------
      hashSet.add(null) returns true
      hashSet.contains(null) returns false
      hashSet.remove(null) retunrs false.

      ------------------------------------------------------------------------------

            mmcclosksunw Michael Mccloskey (Inactive)
            spandeorcl Shantaram Pande (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: