-3

How does one do an inorder traversal of this Java LinkedHashMap:

LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();

ArrayList<String> fields = new ArrayList<String>();
for (String key: map.________)
  fields.add(key);

Thanks.

4

2 に答える 2

2

ループは必要ありません。単に呼び出すだけです

List<String> fields = new ArrayList<String>(map.keySet());
于 2013-03-07T03:24:21.170 に答える
1

map.keySet() is probably what you're looking for.

From the docs of LinkedHashMap

This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

So YES, it does maintain the order of insertion, even after you enter a MILLION keys.

于 2013-03-07T03:20:14.590 に答える