集合类层次结构关系

来源:互联网 发布:免费人事考勤软件 编辑:程序博客网 时间:2024/06/15 13:00
翻译人员: 铁锚
翻译时间: 2013年11月15日
原文链接: The interface and class hierarchy diagram for collections with an example program

1. Collections(工具类) 和 Collection(集合顶层接口) 的区别
首先, “Collection” 和 “Collections” 是两个不同的概念. 从下面几幅图可知,“Collection”是集合继承结构中的顶层接口,而 “Collections” 是提供了对集合进行操作的强大方法的工具类.
图1
2. Collection继承结构
下图展示了集合类的层次结构关系:
图2
3. Map 类层次结构
下图是Map的类层次结构:
图3
4. 相关类汇总

通用实现类
接口哈希表可变数组树链表List哈希表+链表SetHashSet TreeSet LinkedHashSetList ArrayList LinkedList Queue     MapHashMap TreeMap LinkedHashMap

5. 示例代码
下面是说明一些集合类型的简单示例:
[java] view plain copy
  1. import java.util.*;  
  2.    
  3. public class Main {  
  4.    
  5.     public static void main(String[] args) {  
  6.         List<String> a1 = new ArrayList<String>();  
  7.         a1.add("Program");  
  8.         a1.add("Creek");  
  9.         a1.add("Java");  
  10.         a1.add("Java");  
  11.         System.out.println("ArrayList Elements");  
  12.         System.out.print("\t" + a1 + "\n");  
  13.    
  14.         List<String> l1 = new LinkedList<String>();  
  15.         l1.add("Program");  
  16.         l1.add("Creek");  
  17.         l1.add("Java");  
  18.         l1.add("Java");  
  19.         System.out.println("LinkedList Elements");  
  20.         System.out.print("\t" + l1 + "\n");  
  21.    
  22.         Set<String> s1 = new HashSet<String>(); // or new TreeSet() will order the elements;  
  23.         s1.add("Program");  
  24.         s1.add("Creek");  
  25.         s1.add("Java");  
  26.         s1.add("Java");  
  27.         s1.add("tutorial");  
  28.         System.out.println("Set Elements");  
  29.         System.out.print("\t" + s1 + "\n");  
  30.    
  31.         Map<String, String> m1 = new HashMap<String, String>(); // or new TreeMap() will order based on keys  
  32.         m1.put("Windows""2000");  
  33.         m1.put("Windows""XP");  
  34.         m1.put("Language""Java");  
  35.         m1.put("Website""programcreek.com");  
  36.         System.out.println("Map Elements");  
  37.         System.out.print("\t" + m1);  
  38.     }  
  39. }  
输出结果:
[plain] view plain copy
  1. ArrayList Elements  
  2.     [Program, Creek, Java, Java]  
  3. LinkedList Elements  
  4.     [Program, Creek, Java, Java]  
  5. Set Elements  
  6.     [tutorial, Creek, Program, Java]  
  7. Map Elements  
  8.     {Windows=XP, Website=programcreek.com, Language=Java}  

0 0