This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashSet will allow null values? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashSet will allow null values?" Watch "HashSet will allow null values?" New topic
Author

HashSet will allow null values?

Sakthivel Sabapathy
Greenhorn

Joined: Oct 20, 2000
Posts: 4
Can anyone explain me whether HashSet will allow null value or not?
Because in Jamie Jaworski book described as
"It does not allow the use of null element".
anrup kris
Greenhorn

Joined: Oct 22, 2000
Posts: 17
I read in the API that HashSet permits null values,where as the class HashTable implements a hashtable and maps keys to values.So it does not permit null values
regards
kris
Sakthivel Sabapathy
Greenhorn

Joined: Oct 20, 2000
Posts: 4
So that means Jamie Jaworskie book that specified statement is wrong. Am i right?
Sarath Lingamarla
Greenhorn

Joined: Jun 12, 2013
Posts: 1
Sakthivel Sabapathy wrote:So that means Jamie Jaworskie book that specified statement is wrong. Am i right?



HashSet allows null value not null values. it allows only 1 null value per 1 HashSet Object

import java.lang.*;
import java.util.*;
public class Set
{
public static void main(String [] args)
{
Set<Integer> s= new HashSet<Integer>();
s.add(new Integer(5));
s.add(null);
s.add(null);
System.out.println(" "+s.size()+"is the set size");
}
}

Output : 2 is the set size
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: HashSet will allow null values?
 
Similar Threads
about Hashset
Question About TreeSet?
Duplicates in Collections-Confused!!
null values of collections
Hashset, Hashtable and Hashmap