Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: treemap Duplicates

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default treemap Duplicates

    hi!
    i need help with the treemap, the problem is if i store 2 elements with the same key but different values, only the latest entry gets stored for example

            TreeMap map = new TreeMap();
            map.put(1, "alice");
            map.put(1, "bob");
            System.out.println(map.entrySet());
    Displays:
    run:
    [1=bob]
    BUILD SUCCESSFUL (total time: 0 seconds)
    Expected Output
    run:
    [1=alice] [1=bob]
    BUILD SUCCESSFUL (total time: 0 seconds)
    is it possible to achieve the expected output??

    thanks !


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: treemap Duplicates

    The Map interface does not allow duplicate keys...as per the API:
    A map cannot contain duplicate keys; each key can map to at most one value.
    There may be ways to achieve the behavior you want, such as storing Lists in your TreeMap rather than the String

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: treemap Duplicates

    There may be ways to achieve the behavior you want, such as storing Lists in your TreeMap rather than the String
    can you elaborate please? thanks!

    i want to use the treemap class to do something like:
    (sort the names according to height if the height is same sort the names according to weight)

    at the moment i am sorting objects, i.e i created a class with 5 fields, 2 keys for weight and height and 3 for storing the name, height, weight, implemented comparable, added the elements to an arraylist and invoked the sort function. this works fine, but treemap has lot of cool member functions that would help, plus easier to use rather than implementing comparable.

    thanks for the post!

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: treemap Duplicates

    I am not fully sure what you wish to do but I'll guess that you want to store data that represents a person (name, height, weight, etc...). So you could write an object (call it say Person), that contains all those values as variables. From there you could a) add these to a TreeMap or b) add the to an ArrayList (the List would require the extra step up sorting after all is said and done, the TreeMap does this for you). Either way, in doing so you must implement the Comparable interface so you can order based upon height or weight. See Object Ordering (The Java™ Tutorials > Collections > Interfaces)

Similar Threads

  1. Student TreeMap
    By raphytaffy in forum Algorithms & Recursion
    Replies: 6
    Last Post: March 2nd, 2010, 12:11 AM