Results 1 to 5 of 5
Thread: Something like HashMap
- 03-13-2010, 11:23 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 37
- Rep Power
- 0
Something like HashMap
Hi all, I need some structure like HashMap, but it don't have to reorganize items. This is my code:
and following code process itJava 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; }This gives me following outputJava Code:Iterator it = lastTextbook().keySet().iterator(); while (it.hasNext()) { Integer value = (Integer)it.next(); String text = (String)options.get(value); System.out.println(text); }But I need exactly what I put. What Object I should use? Thank you :-)Java Code:Elementary Pre-intermediate Elementary Pre-intermediate Pre-intermediate Intermediate New Matrix New English File New Headway
- 03-14-2010, 12:53 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
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.
- 03-14-2010, 10:13 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 37
- Rep Power
- 0
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 orderso 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: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");
Or if I'll change whichever value I'll get something like this:Java Code:New Headway Elementary Pre-intermediate New English File Elementary Pre-intermediate New Matrix Pre-intermediate Intermediate
Thank youJava Code:StringAssociatedWithKey-1 StringAssociatedWithKey1 StringAssociatedWithKey2 StringAssociatedWithKey-2 StringAssociatedWithKey5 StringAssociatedWithKey6 StringAssociatedWithKey-3 StringAssociatedWithKey9 StringAssociatedWithKey10
- 03-14-2010, 10:15 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
Read the API for the LinkedHashMap class.
kind regards,
Jos
- 03-14-2010, 11:08 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 37
- Rep Power
- 0
Similar Threads
-
How to create a new HashMap from a HashMap entries of other methods
By pandeyalok in forum Advanced JavaReplies: 7Last Post: 12-08-2009, 07:17 PM -
HashMap Help
By ScaryJello in forum New To JavaReplies: 2Last Post: 04-15-2009, 09:18 AM -
HashMap
By koolhoney in forum Advanced JavaReplies: 1Last Post: 03-30-2009, 08:08 PM -
Hashmap
By dirtycash in forum New To JavaReplies: 5Last Post: 12-03-2007, 02:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks