放一张书上的简单的容器分类图,其中点线框表示接口,实线框表示普通的(具体的)类,带有空心箭头的点线表示一个特定的类实现了一个接口,空心箭头表示某个类可以生成箭头所指向类的对象。
从上图看,实现对象按需存取的基本接口是List、Set、Queue和Map,抽象共性Collection是其根接口;从接口再到具体的类方式有点多,那么我只管遍历保存的对象,却不想管你是哪种接口(类)的,就用个迭代器,接口为Iterator,帮助你遍历并选择保存的对象。最后以一个遍历各个常用容器类的代码结尾,说明遍历顺序:
package com.test.myjava; import java.util.*; class Content implements Comparable<Content> { public int id; public String content; public Content(int i, String string) { id = i; content = string; } public static void asCollection(int count, Content[] con, Collection<Content> coll) { for (int j = 0; j < count; j++) coll.add(con[j]); } public int compareTo(Content arg0) { // TODO Auto-generated method stub return (-this.id - arg0.id);//this.id - arg0.id 影响TreeSet顺序 } } public class JavaExample { public static void display(Collection<Content> it) { for (Content t : it) System.out.print(t.content + " "); System.out.println(); } public static void main(String args[]) { final int contentCount = 5; Content[] contentArray = new Content[contentCount]; ArrayList<Content> coll = new ArrayList<Content>(); ArrayList<Integer> collInt = new ArrayList<Integer>(); // 内容赋值 for (int i = 0; i < contentCount; i++) { contentArray[i] = new Content(i, "Content" + i); collInt.add(i); } // 转为Collection Content.asCollection(contentCount, contentArray, coll); Collection<Content> arrayListContent = new ArrayList<Content>(coll); System.out.println("ArrayList:"); JavaExample.display(arrayListContent); Collection<Content> linkedListContent = new LinkedList<Content>(coll); System.out.println("LinkedList:"); JavaExample.display(linkedListContent); Collection<Content> hashSetContent = new HashSet<Content>(coll); System.out.println("HashSet:"); JavaExample.display(hashSetContent); Collection<Content> treeSetContent = new TreeSet<Content>(coll); System.out.println("TreeSet:"); JavaExample.display(treeSetContent); Collection<Content> priorityQueueContent = new PriorityQueue<Content>( coll); System.out.println("PriorityQueue:"); JavaExample.display(priorityQueueContent); Map<Integer, Content> hashMapContent = new HashMap<Integer, Content>(); for (int i = 0; i < contentCount; i++) hashMapContent.put(collInt.get(i), coll.get(i)); System.out.println("HashMap:"); JavaExample.display(hashMapContent.values()); //利用hashMapContent填充 Map<Integer, Content> linkedHashMapContent0 = new LinkedHashMap<Integer, Content>(hashMapContent); System.out.println("LinkedHashMap0:"); JavaExample.display(linkedHashMapContent0.values()); //顺序填充 Map<Integer, Content> linkedHashMapContent = new LinkedHashMap<Integer, Content>(); for (int i = 0; i < contentCount; i++) linkedHashMapContent.put(collInt.get(i), coll.get(i)); System.out.println("LinkedHashMap:"); JavaExample.display(linkedHashMapContent.values()); } } /* * 输出: * ArrayList: * Content0 Content1 Content2 Content3 Content4 * LinkedList: * Content0 Content1 Content2 Content3 Content4 * HashSet: * Content2 Content1 Content4 Content0 Content3 * TreeSet: * Content4 Content3 Content2 Content1 Content0 * PriorityQueue: * Content0 Content1 Content2 Content3 Content4 * HashMap: * Content2 Content4 Content1 Content3 Content0 * LinkedHashMap0: * Content2 Content4 Content1 Content3 Content0 * LinkedHashMap: * Content0 Content1 Content2 Content3 Content4 */