943,083 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 267
  • Java RSS
Feb 8th, 2010
0

Add a Map ... <<Beginner Question>>

Expand Post »
I need to append to Map to another Map... and this is what i did ..
Java Syntax (Toggle Plain Text)
  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4.  
  5. public class sampl {
  6.  
  7. public static void main(String[] args) {
  8. Map<String,String> FileMap =new HashMap<String, String>();
  9. Map<String,String> stateMap =new HashMap<String, String>();
  10.  
  11. stateMap.put("1", "value0");
  12. stateMap.put("12", "value1");
  13. stateMap.put("13", "value2");
  14. stateMap.put("14", "value3");
  15.  
  16. FileMap.put("value changed", "key value");
  17. FileMap.putAll(stateMap);
  18.  
  19. Iterator it = FileMap.entrySet().iterator();
  20. while (it.hasNext()) {
  21. Map.Entry pairs = (Map.Entry)it.next();
  22. System.out.println(pairs.getKey() + " = " + pairs.getValue());
  23. }
  24. }
  25. }
The Out put appears as follows

1 = value0
value changed = key value
13 = value2
14 = value3
12 = value1

What i expected here is to print the out put as follows >>

value changed = key value << So this appears first
1 = value0
13 = value2
14 = value3
12 = value1


according to the code.. i am adding FileMap.put("value changed", "key value"); First and FileMap.putAll(stateMap); thereafter. So why has the order changed in the above output.

how can i edit this code to display the output as follows >>>>

value changed = key value << This should appear first , then the map that i added
1 = value0
13 = value2
14 = value3
12 = value1

note:: First of all i need to display value changed = key value , and the display the remaining of the map..

I need to display values in the order of insertion
Help !!!!
Similar Threads
Reputation Points: 1
Solved Threads: 3
Junior Poster
localp is offline Offline
190 posts
since Apr 2008
Feb 8th, 2010
0
Re: Add a Map ... <<Beginner Question>>
A HashMap is unable to do that for you. You could look at TreeMap which is a sorted map which will let you write your own comparator while inserting key values.

Cheers
Reputation Points: 16
Solved Threads: 31
Junior Poster
cale.macdonald is offline Offline
151 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Regex Help
Next Thread in Java Forum Timeline: java features





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC