简体   繁体   中英

How preserve order of LinkedHashMap while serializing to GWT AutoBean?

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 ?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM