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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

HashSet

 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
many many sorry......................
in hurry by mistake i type that..........
it should be




i will edit my post too
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

phil sohar wrote:why the answer of second print statement is 2..........


I dont get this when I run your program

<edit>
I assume you mean this *set<Integer> set1=new set<Integer>; * to java.util.Set<Integer> set = new java.util.HashSet<Integer>();
</edit>

no? then it depends on your own set implementation
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator




can i compare two double values with this method......
for example if i do like this..............

Test t=new Test();
int x=(int)t.findLarger(new Double(123),new Double(456));
or

can i compare 1 int and 1 double value............
int x=t.findLarger(123,new Double(456));
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi Phil ,
here is the code that it should look like

what is happening is that the the HashSet overrides the hashcode() and equals() method ,so if you try to put duplicate elements in a HashSet it would be considered as a single value and only one of them will exist ,therefore when we delete 1 there is only one element left and that is 2 and if we remove that also we get size of set as zero,it is one of the reasons why set is different from lists which can contain duplicate elements.



 
Abhinav Yadav
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hope you got it
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Btw when you comment the hashCode() method (use the default method) then all your objects will be in the same bucket .Now compile and run it .Then you will see the expected answer.
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    Bookmark Topic Watch Topic
  • New Topic