How to convert/cast HashMap to LinkedHashMap?

17,222

Solution 1

All the answers suggesting that you can create a LinkedHashMap from a HashMap are technically correct, but will not give you the desired results :-(

Of course, you can create a LinkedHashMap from a HashMap, but it isn't guaranteed that the LinkedHashMap will have the same order that your original did.

The problem is that your LinkedHashMap is serialized when it is stored into the persistent storage as a plain unordered Map, which does NOT persist the ordering of the individual items. When you then extract the object from the persistent storage, it is returned as a plain HashMap, and it has lost the "ordering" (which is what you wanted a LinkedHashMap for in the first place). If you then create a LinkedHashMap from the returned HashMap, the ordering will most probably be different from the original.

In order to do this correctly, you should probably convert your LinkedHashMap into an ordered array of objects and store this ordered array in the persistent storage. You can then read the ordered array of objects back from the persistent storage and recreate the LinkedHashMap with the correct order. Basically, you need to serialize and deserialize the LinkedHashMap yourself.

See my answer to this question for more details.

Solution 2

Just create a new LinkedHashMap, since it can take any Map as a constructor argument.

LinkedHashMap<Object> newMap = new LinkedHashMap<>(theHashMapReturnedFromHawk);

Object would be the type you need.

Solution 3

One of LinkedHashMap's constructors accepts a Map. It will return a LinkedHashMap with the same content as HashMap.

Code sample:

LinkedHashMap<T> newMap = new LinkedHashMap<T>(hashmap);

where T is the type of the objects stored in the HashMap

Share:
17,222

Related videos on Youtube

Emil Adz
Author by

Emil Adz

Education: B.S. Computer Science, 2012, HIT. Occupation: Mobile Architect &amp; Co-Founder at Digital Cherry Ever seen a dog chase its tail? Now that's an infinite loop ; ) #SOreadytohelp

Updated on June 19, 2022

Comments

  • Emil Adz
    Emil Adz about 2 years

    I'm using Hawk as a replacement for SharedPreferences in my application.

    I'm trying to store a LinkedHashMap in it, but for some reason when I pull it back from Hawk it returns as a regular HashMap and not a LinkedHashMap. At this point I crash with a ClassCastException as HashMap can't be casted to LinkedHashMap straight forward.

    So the question is how can I convert the returned HashMap to be a LinkedHashMap?

    • David Wasser
      David Wasser almost 8 years
      Thanks for the challenge. I love a non-trivial question that requires a bit of digging.
    • David Wasser
      David Wasser almost 8 years
      I have no idea where the downvotes came from. That surprised me actually.
  • Pankaj Singhal
    Pankaj Singhal almost 8 years
    @DavidWasser I did what was asked. You did what was needed. 👍
  • Adi B
    Adi B over 7 years
    and to accomplish that, I suggest this code sample: stackoverflow.com/a/43142613/1855855