从代码中看基本类型的容器:

public class PrintingContainers {
    static Collection fill(Collection<String> collection) {
        collection.add("rat");
        collection.add("cat");
        collection.add("dog");
        collection.add("dog");
        return collection;
    }

    static Map fill(Map<String, String> map) {
        map.put("rat", "Fuzzy");
        map.put("cat", "Rags");
        map.put("dog", "Bosco");
        map.put("dog", "Spot");
        return map;
    }


    public static void main(String[] args) {
        print(fill(new ArrayList<String>()));
        print(fill(new LinkedList<String>()));
        print(fill(new HashSet<String>()));
        print(fill(new TreeSet<String>()));
        print(fill(new LinkedHashMap<String, String>()));
        print(fill(new HashMap<String, String>()));
        print(fill(new TreeMap<String, String>()));
        print(fill(new LinkedHashMap<String, String>()));
    }
}
/*
[rat, cat, dog, dog]
[rat, cat, dog, dog]
[cat, dog, rat]
[cat, dog, rat]
{cat=Rags, dog=Spot, rat=Fuzzy}
{cat=Rags, dog=Spot, rat=Fuzzy}
{rat=Fuzzy, cat=Rags, dog=Spot}

 */
  • ArrayList:插入顺序和输出顺序一致,可以重复

  • LinkedList:插入顺序和输出顺序一致,可以重复

  • HashSet:插入顺序和输出顺序不一致,不重复

  • TreeSet:插入顺序和输出顺序不一致,不重复

  • HashMap:键值对存储,键不重复。

  • TreeMap:键值对存储,键不重复。

  • LinkedHashMap:键值对存储,键不重复。


sailfishc
47 声望9 粉丝

喜欢孤独与自由