• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Duplicates in Collections-Confused!!

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}
 
Vinod Kumar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic