如何使用集合维护插入顺序 [英] How to Maintain order of insertion using collections

查看:143
本文介绍了如何使用集合维护插入顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个键,值对添加到hashtable(或任何其他集合),但必须维护插入顺序。



像我将加1作为键,1作为值,2作为键,2作为值。 b
$ b

输出应按如下排序:

  1:one 
2:两个


解决方案

href =http://java.sun.com/javase/6/docs/api/java/util/Map.html =nofollow noreferrer> 地图 实施:





$ c> LinkedHashMap 是这种情况下你需要的。



这里是一个片断来说明差异;它还显示了对 Map 的所有条目进行迭代的常用方法,以及如何使用接口引用对象,可以极大地灵活选择实现。

  import java.util。*; 
public class MapExample {
public static void main(String [] args){
populateThenDump(new HashMap< String,Integer>());
populateThenDump(new TreeMap< String,Integer>());
populateThenDump(new LinkedHashMap< String,Integer>());
}
static void populateThenDump(Map< String,Integer> map){
System.out.println(map.getClass()。getName());

map.put(Zero,0);
map.put(One,1);
map.put(Two,2);
map.put(Three,3);
map.put(Four,4);

for(Map.Entry< String,Integer> entry:map.entrySet()){
System.out.println(entry.getKey()+=>+ getValue());
}
}
}

上述代码片段的输出是(在ideone.com 上显示):

  java.util.HashMap //无序,结果可能不同
Three => 3
Zero => 0
One => 1
Four => 4
二=> 2
java.util.TreeMap //按字符串键排序lexicographically
Four => 4
One => 1
Three => 3
二=> 2
Zero => 0
java.util.LinkedHashMap //插入顺序
Zero => 0
One => 1
二=> 2
Three => 3
Four => 4



相关问题





类似问题




I want to add a key,value pair into a hashtable (or any other collection) but have to maintain insertion order. How can I do this?

Like I'll add 1 as key "one" as value, 2 as key and "two" as value.

The output should be ordered as:

1:one
2:two

解决方案

Here are the characteristic differences of some important Map implementations:

  • LinkedHashMap: "with predictable iteration order [...] which is normally the order in which keys were inserted into the map (insertion-order)."
  • HashMap: "makes no guarantees as to the order of the map"
  • TreeMap: "is sorted according to the natural ordering of its keys, or by a Comparator"

So it looks like LinkedHashMap is what you need in this case.

Here's a snippet to illustrate the differences; it also shows a common way to iterate over all entries of a Map, and how using an interface to refer to objects allow great flexibility of choice of implementation.

import java.util.*;
public class MapExample {
    public static void main(String[] args) {
        populateThenDump(new HashMap<String,Integer>());
        populateThenDump(new TreeMap<String,Integer>());
        populateThenDump(new LinkedHashMap<String,Integer>());
    }
    static void populateThenDump(Map<String,Integer> map) {
        System.out.println(map.getClass().getName());

        map.put("Zero",  0);
        map.put("One",   1);
        map.put("Two",   2);
        map.put("Three", 3);
        map.put("Four",  4);

        for (Map.Entry<String,Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " => " + entry.getValue());
        }
    }
}

The output of the above snippet is (as seen on ideone.com):

java.util.HashMap          // unordered, results may vary
Three => 3
Zero => 0
One => 1
Four => 4
Two => 2
java.util.TreeMap          // ordered by String keys lexicographically
Four => 4
One => 1
Three => 3
Two => 2
Zero => 0
java.util.LinkedHashMap    // insertion order
Zero => 0
One => 1
Two => 2
Three => 3
Four => 4

Related questions

Similar questions

这篇关于如何使用集合维护插入顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆