Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintaining order in HashMap [duplicate]

Tags:

I have a list which I convert to a map to do some work. After that, i convert the map back again to a list, but this time the order is random. I need the same initial order retained in my second list.

the obvious reason is that a HashMap doesn't maintain order. But I need to do something so that it does. I cannot change the Map implementation.How can I do that ?

Consider the given code:

import java.util.*;
public class Dummy {

public static void main(String[] args) {
    System.out.println("Hello world !");
    List<String> list = new ArrayList<String>();
    list.add("A");list.add("B");list.add("C");
    list.add("D");list.add("E");list.add("F");

    Map<String,String> map = new HashMap<String, String>();

    for(int i=0;i<list.size();i=i+2)
        map.put(list.get(i),list.get(i+1));

    // Use map here to do some work

    List<String> l= new ArrayList<String>();
    for (Map.Entry e : map.entrySet()) {
        l.add((String) e.getKey());
        l.add((String) e.getValue());
    }
  }
}

For ex - Initially, when I printed the list elements, it printed out

A B C D E F 

Now, when I print the elements of List l, it printed out

E F A B C D
like image 531
OneMoreError Avatar asked Oct 03 '13 16:10

OneMoreError


People also ask

Can HashMap hold duplicate values?

It does not allow duplicate values. It can contain a single null key and multiple null values. It can contain a single null value. HashMap uses the put() method to add the elements in the HashMap.

Does HashMap maintain the 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.

How do you preserve a map order?

So, if you want to keep the elements in the order that they were inserted, use another implementation of Map. Use LinkedHashMap, which keeps that order.

Why HashMap does not preserve insertion order?

” HashMap does not preserve insertion order “. HashMap is collection of Key and Value but HashMap does not give guaranty that insertion order will preserve. i.e here we are adding data of student result from 1st to 3rd year but when we retrieve its there are possibility to change sequence.


1 Answers

HashMap itself doesn't maintain insertion order - but LinkedHashMap does, so use that instead.

As documented... HashMap:

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.

And LinkedHashMap:

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

like image 129
Jon Skeet Avatar answered Oct 07 '22 11:10

Jon Skeet