Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on Set and List

 
janardhan rao B
Greenhorn
Posts: 9
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 48953
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48953
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote: . . . Please do not provide complete answers . . .
Thank you for noticing.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, this is janardhan .. thanks for everything ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic