如何在序列化到 GWT AutoBean 时保留 LinkedHashMap 的顺序?
我尝试过使用 Map、HashMap 和 LinkedHashMap 作为 AutoBean 工厂的类型,并且总是在序列化之后更改初始元素顺序。
我不想发送额外的 ArrayList 来保存订单数据。有没有办法强制 AutoBean 在 Map 中保持顺序?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://docs.oracle.com/javase/6 /docs/api/java/util/Map.html
GWT 不会对基于
Map
的类进行任何更改。出于性能原因,Map
及其许多子类不保留插入顺序。LinkedHashMap
根据设计,确实保持其插入顺序。当然,假设您开始使用LinkedHashMap
并且不是从另一种类型的Map
构建它,而后者实际上可能不会保留其插入期间的顺序,导致LinkedHashMap
的顺序与您预期的顺序不同。我想知道为什么你需要保留初始顺序,如果这是你想要的,为什么要使用
Map
?http://docs.oracle.com/javase/6/docs/api/java/util/Map.html
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 aLinkedHashMap
and not constructing it from another type ofMap
which may not actually preserve its order during the insertion, causing theLinkedHashMap
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?显然,至少在 GWT 2.4 中,GWT 中的
LinkedHashMap.clone()
返回一个HashMap
,这与纯 Java 行为相反。 AutoBean 可能依赖于clone(),这最终打乱了顺序。Apparently, and at least in GWT 2.4,
LinkedHashMap.clone()
in GWT returns aHashMap
, in contrast to pure Java behavior. AutoBean probably relies on clone() which messes up the order in the end.