I need to use Maps in Java for an Android Application. But the problem is that the list gets sorted automatically. How do I use Maps to get the data in the same order as I have inserted data.
-
it might be prudent to rename the title to better reflect that fact that you're interested in a map that maintains insertion order – Nadir Muzaffar Mar 04 '12 at 07:17
-
What list gets sorted automatically? Is that a problem? or is it part of your problem *statement?* Question remains unclear. – user207421 Mar 04 '12 at 08:45
5 Answers
You should use LinkedHashMap
for this purpose..Visit Android Docs and Java Docs for more details.

- 7,993
- 6
- 41
- 54
The LinkedHashMap maintains insertion order.

- 7,993
- 6
- 41
- 54

- 4,772
- 2
- 32
- 48
-
1Not only that, it's got an easy hook to build a cache out of it. Very nice. – Argyle Mar 04 '12 at 06:24
A LinkedHashMap will keep the data in the same order as it has been inserted.

- 13,885
- 7
- 36
- 56
As you and I have discovered, LinkedHashMap isn't very helpful. (What is the point of its existence, anyway?)
I have a hashlist (semantically, I think it should have been called hashedlist)
It has an arraylist and a hashmap. The arraylist stores the key.
A hashlist.put(key, value) would perform
- a map.put(key, value)
- as well as a list.add(key)
A hashlist.get(int position) would perform - a map.get(list.get(position))
This is a simplification of HashVector and HashTree classes I wrote back in 2003 when I needed to model javascript and xml objects in Java, retaining their order. However, I did not find the time or necessity to simplify the hashtree for gwt serializability.
On second thoughts, how does GWT implement a hashmap? I think when I have the time, I need to replace the hashmap with faststringmap. Google's faststringmap is not public. It is for GWT compiler private use. So you have to copy its code and change it into a public class: http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/user/client/ui/FastStringMap.java
May be, GWT compiler would have silently used it anyway - should I bother to micromanage the compiler replace hashmap with faststringmap?
BTW,
You could still look for the hashtree by googling "googlecode synthful hashtree".
The Hashtree allows you to create a tree of objects and allows you to retrieve your objects using the an dot-convention xpath like path.
hashtree.get("hello.dolly.how.are.you");
The separator could be respecified so that you could store or get using
hashtree.get("hello/dolly/how/are/you");
hashtree.put("hello/dolly/how/are/you", value);

- 21,058
- 23
- 106
- 176
-
1It does seem you don't understand the purpose of LinkedHashMap, what where you expecting of it? – M Platvoet Mar 04 '12 at 07:13
-
Genuinely curious, what aspect of a LinkedHashMap makes it inadequate for the OP's purpose? – Nadir Muzaffar Mar 04 '12 at 07:16
-
linkedhashmap.insert(6, key, value)? linkedhashmap.remove(7)? linkedhashmap.put(20, key, value)? It does appear I don't understand the purpose of MS windows when I could use Linux. – Blessed Geek Mar 04 '12 at 16:46
-
2@BlessedGeek but that's not needed to answer the question. So LinkedHashMap is perfectly helpful. Usually a bike will suffice, even if you own a proper German car. – M Platvoet Mar 04 '12 at 19:38
use LinkedHashMap
. please follow this link for more details

- 52,533
- 16
- 102
- 136

- 2,001
- 1
- 17
- 22