Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
    Bookmark Topic Watch Topic
  • New Topic

HashSet

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Report post to moderator
many many sorry......................
in hurry by mistake i type that..........
it should be




i will edit my post too
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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));
 
Abhinav Yadav
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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
  • Report post to moderator
hope you got it
 
lakmal padmakumara
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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.
 
    Bookmark Topic Watch Topic
  • New Topic