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

Java Collections

 
Dorothy Taylor
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me with the below given problem:


Assume you are writing an application working with Person objects. The application uses Person objects in Collections, placing them into Collection implementations and querying if a Person is in a Collection using the Collection.contains() method. The application also uses Person objects as keys in Maps to associate other objects with Persons and to efficiently look up those objects based on Person. Given these needs, will the following Person implementation work in our application? If not, please fix it so that it will.


 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dorothy Taylor wrote: Given these needs, will the following Person implementation work in our application?


Is it working currently?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dorothy Taylor wrote:The application also uses Person objects as keys in Maps to associate other objects with Persons and to efficiently look up those objects based on Person.

If you want to use the Person object as keys then override equals() and the hashCode() methods of the Object class.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akhilesh Trivedi wrote:
Dorothy Taylor wrote: Given these needs, will the following Person implementation work in our application?


Is it working currently?

Yes Dorothy - first check and let us know if it's working currently...
 
Himanshu Mall
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If not, please fix it so that it will.

 
Tomasz Sochanski
Ranch Hand
Posts: 47
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can't work now. Collection.contains(Object o) implementations use o.equals() method to find element in collection. You don't override equals(), so Object.equals() will be used. Two Person objects created with the same constructor arguments will not be equal. You need to override Object.equals() in your class. If you want to use this class as key in map you will also need to override Object.hashCode() to keep equals() and hashCode() contract. Take a look here if you need more information how to implement missing methods: http://bytes.com/topic/java/insights/723476-overriding-equals-hashcode-methods
 
Dorothy Taylor
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that was a very useful explanation of the concept. I have one more doubt. So if we need to sort these Person objects based on the natural ordering of each person’s complete name, then is it ok to just extend Comparator interface and implement the compare() method?
 
Tomasz Sochanski
Ranch Hand
Posts: 47
Eclipse IDE Java Oracle
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dorothy Taylor wrote:So if we need to sort these Person objects based on the natural ordering of each person’s complete name, then is it ok to just extend Comparator interface and implement the compare() method?


Dorothy,

You cannot extend interface in class, you can implement it. If you tried your idea (which I strongly suggest) then you already noticed it won't work. Not every Map implementation cares about key ordering, you have to choose the one which does (ie. TreeMap).

TreeMap javadoc stands:

The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

So you can either implement Comparable (not Comparator) interface in Person class, or create implementation of Comparator and provide it during initialization of map. You would also want to read about Collator if you have to care about national characters in names, like 'é', 'ą', 'ö' etc.
 
Dorothy Taylor
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic