开发者

how to get unordered key value pair

The problem is that ;开发者_如何学Go I am retrieving a key value data object pair from database it is like

(select * from xyz ORDER BY letter DESC)

3 z,
2 y,
1 x

And Im putting these to a Long, String HashMap pair. The problem is when I putting these to Map pair, the order is changing. It appears as

1 x,
2 y,
3 z 

How can i preserve data as in first situation.


Use a LinkedHashMap instead! That will keep the order.


You can use LinkedHashMap


use LinkedHashMap instead of HashMap. the order would be preserved.


Use TreeMap instead of HashMap.

Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

  • from http://download.oracle.com/javase/6/docs/api/java/util/Map.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜