Results 1 to 5 of 5
  1. #1
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default Something like HashMap

    Hi all, I need some structure like HashMap, but it don't have to reorganize items. This is my code:
    Java Code:
    public static HashMap<Integer, String> lastTextbook() {
        HashMap<Integer, String> map = new HashMap<Integer, String>();
    
        map.put(-1, "New Headway");
        map.put(1, "Elementary");
        map.put(2, "Pre-intermediate");
        map.put(-2, "New English File");
        map.put(5, "Elementary");
        map.put(6, "Pre-intermediate");
        map.put(-3, "New Matrix");
        map.put(9, "Pre-intermediate");
        map.put(10, "Intermediate");
    
        return map;
    }
    and following code process it
    Java Code:
    Iterator it = lastTextbook().keySet().iterator();
    while (it.hasNext()) {
        Integer value = (Integer)it.next();
        String text = (String)options.get(value);
    
        System.out.println(text);
    }
    This gives me following output
    Java Code:
    Elementary
    Pre-intermediate
    Elementary
    Pre-intermediate
    Pre-intermediate
    Intermediate
    New Matrix
    New English File
    New Headway
    But I need exactly what I put. What Object I should use? Thank you :-)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Consider using a Map but sorting the set of keys to obtain the ordering you want and then iterating over that.

    (It might help to describe the output you want: "I need exactly what I put" is a bit ambiguous. What is supposed to happen if a value is associated with a key and then, much later, another value is associated with the same key?)

    Another approach would be to have two collections: a map and an list of map entries. You would have to keep the two in sych.

  3. #3
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default

    Hi, I'm sorry for ambiguous expression. With this "I need exactly what I put" I meant that:
    I put items into set in this order
    Java Code:
    map.put(-1, "New Headway");
    map.put(1, "Elementary");
    map.put(2, "Pre-intermediate");
    map.put(-2, "New English File");
    map.put(5, "Elementary");
    map.put(6, "Pre-intermediate");
    map.put(-3, "New Matrix");
    map.put(9, "Pre-intermediate");
    map.put(10, "Intermediate");
    so anytime I'll want to go through this map, I'll get back the same order (by keys). In this case I get back following:
    Java Code:
    New Headway
    Elementary
    Pre-intermediate
    New English File
    Elementary
    Pre-intermediate
    New Matrix
    Pre-intermediate
    Intermediate
    Or if I'll change whichever value I'll get something like this:
    Java Code:
    StringAssociatedWithKey-1
    StringAssociatedWithKey1
    StringAssociatedWithKey2
    StringAssociatedWithKey-2
    StringAssociatedWithKey5
    StringAssociatedWithKey6
    StringAssociatedWithKey-3
    StringAssociatedWithKey9
    StringAssociatedWithKey10
    Thank you

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Read the API for the LinkedHashMap class.

    kind regards,

    Jos

  5. #5
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

Similar Threads

  1. Replies: 7
    Last Post: 12-08-2009, 08:17 PM
  2. HashMap Help
    By ScaryJello in forum New To Java
    Replies: 2
    Last Post: 04-15-2009, 10:18 AM
  3. HashMap
    By koolhoney in forum Advanced Java
    Replies: 1
    Last Post: 03-30-2009, 09:08 PM
  4. Hashmap
    By dirtycash in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 03:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •