aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Duplicates in Collections-Confused!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Duplicates in Collections-Confused!!" Watch "Duplicates in Collections-Confused!!" New topic
Author

Duplicates in Collections-Confused!!

Vinod Kumar
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
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
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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 ]

SCJP2. Please Indent your code using UBB Code
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
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
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
sun ram
Ranch Hand

Joined: Dec 18, 2001
Posts: 61
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
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Duplicates in Collections-Confused!!