首先来看看Collection和Collections的区别,Collection和Collections是完全不同的两个概念。Collections是个java.util下的类,它包含有各种有关集合操作的静态方法。 Collection是个java.util下的接口,它是各种集合结构的父接口。
Collections在java中的关系图:
Collection是各种集合的接口,来看看下边这个层次结构:
还有map的层次结构:
示例代码:
package com.zz.chain;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 代码示例
* Copyright 2015年5月31日
* created by txxs
* all right reserved
*/
public class Test {
public static void main(String[] args) {
List<String> a1 = new ArrayList<String>();
a1.add("Program");
a1.add("Creek");
a1.add("Java");
a1.add("Java");
System.out.print("ArrayList Elements:");
System.out.print(a1 + "\n");
List<String> l1 = new LinkedList<String>();
l1.add("Program");
l1.add("Creek");
l1.add("Java");
l1.add("Java");
System.out.print("LinkedList Elements:");
System.out.print(l1 + "\n");
Set<String> s1 = new HashSet<String>(); // or new TreeSet() will order the elements;
s1.add("Program");
s1.add("Creek");
s1.add("Java");
s1.add("Java");
s1.add("tutorial");
System.out.print("Set Elements:");
System.out.print(s1 + "\n");
Map<String, String> m1 = new HashMap<String, String>(); // or new TreeMap() will order based on keys
m1.put("Windows", "2000");
m1.put("Windows", "XP");
m1.put("Language", "Java");
m1.put("Website", "programcreek.com");
System.out.print("Map Elements:");
System.out.print(m1);
}
}
运行结果:
参考:http://stackoverflow.com/questions/3317381/what-is-the-difference-between-collection-and-list-in-java