Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashMap gives an unordered list of values?

Tags:

java

android

I'm having a weird problem with HashMap in Android. I'm putting values into the hashmap which is of the form

HashMap <String,String> sample = new HashMap<String,String>();

However let's say I'm putting the following values in the following order:

sample.put("ifi1", "video1");
sample.put("ifi2", "video2");
sample.put("ifi3", "video3");
sample.put("ifi4", "video4");
sample.put("ifi5", "video5");
sample.put("ifi6", "video6");
sample.put("ifi7", "video7");
sample.put("ifi8", "video8");
sample.put("ifi9", "video9");

This is just a simple example that is similar to what i have. I only have a bigger list in my actual code. However when I now try to print only the values, I get an unordered list as follows:

VIDEOS: video1
VIDEOS: video3
VIDEOS: video2
VIDEOS: video5
VIDEOS: video4
VIDEOS: video7
VIDEOS: video6
VIDEOS: video9
VIDEOS: video8

where in fact I'm expecting it to produces the following list:

VIDEOS: video1
VIDEOS: video2
VIDEOS: video3
VIDEOS: video4
VIDEOS: video5
VIDEOS: video6
VIDEOS: video7
VIDEOS: video8
VIDEOS: video9

Why is this, any idea?

like image 539
bytebiscuit Avatar asked Oct 17 '11 10:10

bytebiscuit


People also ask

Is HashMap unordered?

HashMap is unordered; you can't and shouldn't assume anything beyond that. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Is HashMap an ordered collection?

Is hashmap an ordered collection. Explanation: Hashmap outputs in the order of hashcode of the keys. So it is unordered but will always have same result for same set of keys.

Does HashMap maintain order?

HashMap does not maintains insertion order in java. Hashtable does not maintains insertion order in java. LinkedHashMap maintains insertion order in java. TreeMap is sorted by natural order of keys in java.

Is a HashMap a list?

The difference between ArrayList and HashMap is that ArrayList is an index-based data-structure supported by array, while the HashMap is a mapped data structure, which works on hashing to retrieve stored values. Although both are used to store objects, they are different in their implementation, function, and usage.


2 Answers

That's right. The HashMap implementation of Map does not guarantee any order during iteration.

If you want ordering based on insertion...

...have a look at LinkedHashMap:

Map<String, String> sample = new LinkedHashMap<String, String>();

sample.put("ifi1", "video1");
sample.put("ifi2", "video2");
sample.put("ifi3", "video3");
sample.put("ifi4", "video4");
sample.put("ifi5", "video5");
sample.put("ifi6", "video6");
sample.put("ifi7", "video7");
sample.put("ifi8", "video8");
sample.put("ifi9", "video9");

for (String video : sample.values())
    System.out.println(video);

// Prints
video1
video2
video3
video4
video5
video6
video7
video8
video9

If you want ordering based on the keys...

Use some SortedMap such as a TreeMap.

like image 181
aioobe Avatar answered Sep 22 '22 09:09

aioobe


You need to use SortedMap instead of HashMap. It will guarantee order.

like image 36
Sasha Goldshtein Avatar answered Sep 22 '22 09:09

Sasha Goldshtein