3

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?

Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
denu
  • 2,170
  • 2
  • 23
  • 28

2 Answers2

1

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?

Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
  • 1
    Hi, GWT isn't reordering my LinkedHashMap. But AutoBean does, while serializing map to json. I'm using it to store column_name = row_value pairs, which are taken from grid model and should be imported into DynamicJasper xls report factory as a List of Map . I don't want to change that structure for legacy reasons. My current workaround is passing column order as another List. Thanks God, AutoBean serializes list with right order. – denu Dec 06 '11 at 08:26
1

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.

Irfy
  • 9,323
  • 1
  • 45
  • 67