我使用过,LinkedHashMap因为重要的是键在地图中输入的顺序.
但现在我想首先获得key的值(第一个输入的条目)或最后一个.
如果有喜欢的方法first()和last()或类似的东西?
我是否需要一个迭代器才能获得第一个键入口?这就是我使用的原因LinkedHashMap!
谢谢!
我试了几个小时,但没有找到任何最好的方法来以相反的顺序实现hashmap的迭代,这是我的hashmap.
Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
for(Integer key : map.keySet()) {
List<String> value = map.get(key);
List<Map<String,?>> security = new LinkedList<Map<String,?>>();
for(int ixy = 0; ixy < value.size()-1; ixy++){
security.add(createItem(value.get(ixy), value.get(ixy+1)));
}
adapter.addSection(Integer.toString(key), new SimpleAdapter(getApplicationContext(), security, R.layout.list_complex, new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));
}
Run Code Online (Sandbox Code Playgroud)
我也见过TreeMap的例子,
Map<Integer, List<String>> sortedMap = new TreeMap<Integer, List<String>>(map);
Run Code Online (Sandbox Code Playgroud)
但是树形图也按升序给出,我想要的是降序.
可能重复:
以相反的顺序迭代LinkedHashMap
如何以相反的顺序遍历链接的哈希映射?地图中是否有任何预定义方法可以做到这一点?
我正在创建它如下:
LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer,String>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
Run Code Online (Sandbox Code Playgroud)