This week's book giveaway is in the Open Source Projects forum.
We're giving away four copies of Eclipse Collections Categorically: Level up your programming game and have Donald Raab on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Java Collections

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 1609
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dorothy Taylor wrote: Given these needs, will the following Person implementation work in our application?



Is it working currently?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If not, please fix it so that it will.


 
Ranch Hand
Posts: 47
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
You got style baby! More than this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic