How preserve order of LinkedHashMap while serializing to GWT AutoBean?

2.8k views Asked by At

I've tried using Map, HashMap and LinkedHashMap as a type for AutoBean factory and always after serializing it's changing initial elements order.

I don't want to send additional ArrayList that will hold the order data. Is there a way to force AutoBean to keep order in a Map?

2

There are 2 answers

0
Irfy On

Apparently, and at least in GWT 2.4, LinkedHashMap.clone() in GWT returns a HashMap, in contrast to pure Java behavior. AutoBean probably relies on clone() which messes up the order in the end.

1
Chris Cashwell On

http://docs.oracle.com/javase/6/docs/api/java/util/Map.html

The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

GWT doesn't make any alterations to Map-based classes. Map and many of its subclasses do not keep order of insertion for performance reasons. LinkedHashMap, by design, does keep its insertion order. That is, of course, assuming you're starting with a LinkedHashMap and not constructing it from another type of Map which may not actually preserve its order during the insertion, causing the LinkedHashMap to have a different order than you're expecting.

I'm wondering why you need to keep the initial order anyway, and why are you using Map if that's what you want?