一位國(guó)外的java大神用3張圖表示了Java集合框架的接口和類層次關(guān)系,并配有代碼示例
讓你秒懂他們之間的關(guān)系,廢話不多,直接上圖:
Collection和Collections的區(qū)別
首先要說(shuō)的是,"Collection" 和 "Collections"是兩個(gè)不同的概念;
如下圖所示,"Collection"是集合類(Collection)的頂級(jí)接口,然而”Collections“是一個(gè)提供了一系列靜態(tài)方法的集合工具類;
Collection的類層次結(jié)構(gòu)圖
Map的類層次結(jié)構(gòu)圖
總結(jié)
代碼示例
package simplejava;
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;
public class Q7 {
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.println("ArrayList Elements");
System.out.print("\\t" + a1 + "\\n");
List<String> l1 = new LinkedList<String>();
l1.add("Program");
l1.add("Creek");
l1.add("Java");
l1.add("Java");
System.out.println("LinkedList Elements");
System.out.print("\\t" + 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.println("Set Elements");
System.out.print("\\t" + 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.println("Map Elements");
System.out.print("\\t" + m1);
}
}
結(jié)果打?。?/span>
ArrayList Elements
[Program, Creek, Java, Java]
LinkedList Elements
[Program, Creek, Java, Java]
Set Elements
[tutorial, Creek, Program, Java]
Map Elements
{Windows=XP, Website=programcreek.com, Language=Java}
原文來(lái)自:Linux公社