[RESOLVED] Java Collections
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] Java Collections

  1. #1
    Join Date
    Dec 2011
    Posts
    4

    [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;
    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Apr 2007
    Posts
    442

    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?

  4. #4
    Join Date
    Dec 2011
    Posts
    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.

  5. #5
    Join Date
    Apr 2007
    Posts
    442

    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.

  6. #6
    Join Date
    Dec 2011
    Posts
    4

    Re: Java Collections

    Quote Originally Posted by Londbrok View Post
    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!!

  7. #7
    Join Date
    Dec 2011
    Posts
    4

    Re: Java Collections

    Quote Originally Posted by Londbrok View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center