*
The moose likes Beginning Java and the fly likes doubt on Set and List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt on Set and List" Watch "doubt on Set and List" New topic
Author

doubt on Set and List

janardhan rao B
Greenhorn

Joined: May 27, 2010
Posts: 9

Let us assume a set as below
Set s1 = {1,2,3,4}
Set s2 = {4,3,2,1}.
ok..

Question 1. s1==s2 or not.. if not why ?

Question 2. what the JVM will consider when storing those two sets (hashcode of reference or content) ?

and please let me know in the case of List what will happened if you consider above things...?

Thanks in advance


Success is nothing more than a few simple disciplines, practiced every day.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

s1 == s2 - no
s1.equals(s2) - yes

== only compares if they're exactly the same object. equals() for the Set interface looks at the contents. Specifically "Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set". (quote from the Javadocs).

For question 2, it depends on the implementation of the Set. HashSet uses hashCode() and equals() to do the comparison. Another implementation might only use equals() (although it wouldn't be as efficient).

For Lists, equals() "Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal". In other words, the order matters.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

In short.
remeber two things...

what "==" do is check whether the two refernces points to a same object or not...........how it will do is it will compare the bit pattern of two refernce variable..if the bit pattern is same then it will return true otherwise false........


what equals.do is check whthere the contents of two objects are meaningfully equivalent..........if yes then it will return true............

for example it will also return true for below......

int i=5;
float f=5.0f;
System.out.println(i==f);//return true.....why you check it


SCJP6.0,My blog Ranchers from Delhi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
janardhan bommineni wrote:Set s1 = {1,2,3,4}
Set s2 = {4,3,2,1}
That doesn't look like Java code, but like ordinary mathematical notation. In mathematical notation the == symbol is not commonly used; one would use = instead. But that would give a result of true for those two sets.

Please be careful to distinguish code from mathematics.

The JVM doesn't "consider" anything about those two sets; if they are translated to Java code and executed, the JVM would put them in different memory locations on the heap. It would not work out a hash code or use the equals method until those are required.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Matthew Brown wrote:(complete answer)

Please do not provide complete answers, rather provide hints, resource, etc. JavaRanch is NotACodeMill, and we encourage people to DoYourOwnHomework. The learning process is accelerated when people have to figure things out for themselves, and in some situations, providing a complete answer may encourage sloppy learning at best, outright plagiarism or cheating at worst.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
David Newton wrote: . . . Please do not provide complete answers . . .
Thank you for noticing.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

David Newton wrote:
Matthew Brown wrote:(complete answer)

Please do not provide complete answers, rather provide hints, resource, etc.

Sorry. I considered what I was doing as explaining the concept, rather than providing a complete answer (especially since the bulk of my answer was a direct quote from the Javadocs). I'll try and be more careful in future!
janardhan rao B
Greenhorn

Joined: May 27, 2010
Posts: 9

hi, this is janardhan .. thanks for everything ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt on Set and List
 
Similar Threads
a burning question of reference type
type of reference variable
Inheritence
Kai Notes - http://www.jdiscuss.com/
Collection addAll method Strange Behavior