-
December 5th, 2011, 02:18 AM
#1
[RESOLVED] Java Collections
Can anyone help me with teh below mentioned programming 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.
public class Person {
private String firstName;
private String lastName;
private Date dob;
/** Construct a Person given the first name, last name, and birth date. */
public Person(String firstName, String lastName, Date dob) {
if (firstName == null || lastName == null || dob == null) {
throw new IllegalArgumentException();
}
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
}
}
-
December 5th, 2011, 05:07 AM
#2
Re: Java Collections
will the following Person implementation work in our application?
It will work, but not very well.
There's no way to get the data out of the object and as it's being used as a key to a Map it should provide it's own equals() implementation. And of course if you override equals() you must override hashcode().
If not, please fix it so that it will.
No, that's your job
-
December 5th, 2011, 05:19 AM
#3
Re: Java Collections
Fix it so that it will? Are you serious?
Given the problems that you noted and the implementation you provided... I dont see where you have issues. You have provided a Person class, but nothing related to how you implement their usage in terms of collections and maps. Data structures would be used by your business logic, models and such, code for which you did not manage to include. As to the code submitted, there is nothing wrong as such. So... what exactly are you expecting anyone to fix?
-
December 5th, 2011, 05:33 AM
#4
Re: Java Collections
This problem is from some sample exercises on data structures. The Person class needs to be modified in an efficient way for use maybe for example like for sorting Person objects based on the natural ordering of each person’s complete name.
-
December 5th, 2011, 07:56 AM
#5
Re: Java Collections
Given answers still apply.
Take a look at Collection contains method. How does it determine the return value? As Keang already pointed out, having an efficient equals is the crucial part. On top of that, you have sorting based on Comparator instances. That is oftentimes a convinient approach to the question when sorting needs are varied.
The overall point of any training exercise, is in that you do it, find out both the problems and answers.
-
December 7th, 2011, 02:26 AM
#6
Re: Java Collections
Originally Posted by Londbrok
The overall point of any training exercise, is in that you do it, find out both the problems and answers.
If I knew the solution myself, I wouldn't be posting in this forum. Nevertheless, I guess you guys have given me the solution already which is to override the equals and hashcode() methods. So that answers my question..thanks!!
-
December 7th, 2011, 03:28 AM
#7
Re: Java Collections
Originally Posted by Londbrok
You have provided a Person class, but nothing related to how you implement their usage in terms of collections and maps.
The usage has been provided : Collection.contains() method is to be used.
As to the code submitted, there is nothing wrong as such
The contains() method will return false as it will use the Object.equals() method to compare. hence we need to override this
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|