File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic


Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

many many sorry......................
in hurry by mistake i type that..........
it should be

i will edit my post too

SCJP6.0,My blog Ranchers from Delhi
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

I dont get this when I run your program

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

no? then it depends on your own set implementation
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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));

can i compare 1 int and 1 double value............
int x=t.findLarger(123,new Double(456));
Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34
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.

No Dream Too Big
Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34
hope you got it
lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47
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.

[Bsc in Computer Science and Engineering] [SCJP 80%][BCS certificate level]
I agree. Here's the link:
subject: HashSet
jQuery in Action, 3rd edition