This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
import java.util.*; class Sample { public static void main(String[] args) { //TREESET ACCEPTS DUPLICATES TreeSet ts = new TreeSet(); ts.add("one"); ts.add("one"); //HASHSET ACCEPTS DUPLICATES HashSet hs= new HashSet(); hs.add(null); hs.add(null); //HASHMAP ACCEPTS DUPLICATES HashMap hm = new HashMap(); hm.put(null,null); hm.put(null,null); //TREEMAP ACCEPTS DUPLICATES TreeMap tm = new TreeMap(); hm.put("one",null); hm.put("one",null); } } This is the program which I tried out with main classes in util package with nulls and duplicates. In java API and other available sources in the net says Set interface wont allow duplicates,so also Map wont allow duplicate keys but my program is compiling and running without any errors.In the case of Map it is just overriding the previous key.I am using jdk1.3.Please clarify. Thanx in advance, Vinod Pai
In the TreeSet or HashSet the second element is not being added. In TreeMap or HashMap the second addition replaces the first. Use System.out.println(ts) ... to see the elements. Thus no duplicates are hold in the containers. [ February 08, 2002: Message edited by: Jose Botella ]
Hi, Classes inherited from Set interface doesn't set duplicate or show duplicate error. PUT() METHOD JUST RETURNS FALSE(doesn't return error!!!) IF THE INPUT IS DUPLICATED. That's all. Jamal
Vinod Kumar
Ranch Hand
Joined: Jan 18, 2002
Posts: 75
posted
0
Hi all, Yes it was my mistake not checking the final content of those collections.When I checked the size of those then only I realized duplicate entries were omitted/overwritten.I was expecting Exception while running program.Thanx a lot for the replies. Regards, vinod
Hi, TreeMap is not taking null values. see the following program output. Thanks SR class Sample{ public static void main(String[] args) { //TREESET ACCEPTS DUPLICATES TreeSet ts = new TreeSet(); ts.add("one"); ts.add("one"); System.out.println("Tree Set :" + ts); System.out.println("Tree Set sz " + ts.size()); //HASHSET ACCEPTS DUPLICATES HashSet hs= new HashSet(); hs.add(null); hs.add(null); System.out.println("Hash Set :" + hs); System.out.println("Hash Set sz:" + hs.size()); //HASHMAP ACCEPTS DUPLICATES HashMap hm = new HashMap(); hm.put(null,null); hm.put(null,null); System.out.println("Hash map :" + hm); System.out.println("Hash map sz:" + hm.size()); //TREEMAP ACCEPTS DUPLICATES TreeMap tm = new TreeMap(); hm.put("one",null); hm.put("one",null); tm.put("One", "One"); System.out.println("Tree map :" + tm); System.out.println("Tree Map : " + tm.size()); } }
SCSecA,SCNA,SCSA,SCWCD,SCJP
Vinod Kumar
Ranch Hand
Joined: Jan 18, 2002
Posts: 75
posted
0
Hi, Yes very true in your case.I am adding more on this in TreeMap if you have a null key and a non null key NullPointerException will be thrown.Also in TreeSet if you have a null element and a non null element NullPointerException will be thrown.This is because of compareTo method called by these collections for sorting purpose.But if you have ONLY ONE NULL element in both classes the runtime execution will not give any error/exception.So we can add atmost one null in both cases as Java API says. Hope I am correct. Regards, Vinod
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.