List
ArrayList:能够快速的随机访问,更利于查询与修改;
LinkedList:能够快速的插入与删除,具有在List中未包含的额外方法;
相同点:
都是非同步的(异步),所以效率更高;
都是按照被添加的顺序保存对象;
Set
HashSet:最快的获取元素的方式,并且它是无序的(可见下面示例);
TreeSet:按照比较结果的升序保存对象(按照字典顺序排序),不支持null;
LinkedHashSet:按照被添加的顺序保存对象;
相同点:每个值只保存一个对象,即不可重复;
Map
HashMap:与HashSet相同,最快的获取元素的方式,并且是无序的;
HashTable:是同步的,不支持null,直接使用对象的hashCode;
TreeMap:与TreeSet相同,按照比较结果的升序保存对象,并且不支持null;
LinkedHashMap:与LinkedHashSet一样,按照被添加的顺序保存对象,并且它还有HashMap的速度;
示例
package com.hjp.javaSource.ThinkingInJava.HoldingYourObjects;
import java.util.*;
/**
* @author huangjp 2018-03-12 13:41
* java基本容器测试类
**/
public class Test {
private static ArrayList arrayList = new ArrayList();
private static LinkedList linkedList = new LinkedList();
private static HashSet hashSet = new HashSet();
private static TreeSet treeSet = new TreeSet();
private static LinkedHashSet linkedHashSet = new LinkedHashSet();
private static HashMap hashMap = new HashMap();
private static TreeMap treeMap = new TreeMap();
private static LinkedHashMap linkedHashMap = new LinkedHashMap();
private static Hashtable hashtable = new Hashtable();
private static final Object[] arr = {1,5,2,6,0,8,6,2,4,7,100};
public static void main(String[] args) {
add();
addNull();
print();
}
/*
Output : arrayList:
1 5 2 6 0 8 6 2 4 7 100 null
linkedList:
1 5 2 6 0 8 6 2 4 7 100 null
hashSet:
null 0 1 100 2 4 5 6 7 8
treeSet:
0 1 2 4 5 6 7 8 100
linkedHashSet:
1 5 2 6 0 8 4 7 100 null
hashMap:
null 0 1 100 2 4 5 6 7 8
treeMap:
0 1 2 4 5 6 7 8 100
linkedHashMap:
1 5 2 6 0 8 4 7 100 null
hashtable:
100 8 7 6 5 4 2 1 0
*/
private static void add(){
for (Object i : arr){
arrayList.add(i);
linkedList.add(i);
hashSet.add(i);
treeSet.add(i);
linkedHashSet.add(i);
hashMap.put(i,i);
treeMap.put(i, i);
linkedHashMap.put(i, i);
hashtable.put(i, i);
}
}
private static void addNull(){
arrayList.add(null);
linkedList.add(null);
hashSet.add(null);
//treeSet.add(null);
linkedHashSet.add(null);
hashMap.put(null,null);
//treeMap.put(null, null);
linkedHashMap.put(null, null);
//hashtable.put(null, null);
}
private static void print(){
System.out.println("arrayList:");
for (Object array : arrayList)
System.out.print(array + " ");
System.out.println("");
System.out.println("linkedList:");
for (Object linked : linkedList)
System.out.print(linked + " ");
System.out.println("");
System.out.println("hashSet:");
for (Object set : hashSet){
System.out.print(set + " ");
}
System.out.println("");
System.out.println("treeSet:");
for (Object set : treeSet)
System.out.print(set + " ");
System.out.println("");
System.out.println("linkedHashSet:");
for (Object set : linkedHashSet)
System.out.print(set + " ");
System.out.println("");
System.out.println("hashMap:");
for (Object key : hashMap.keySet())
System.out.print(hashMap.get(key) + " ");
System.out.println("");
System.out.println("treeMap:");
for (Object key : treeMap.keySet())
System.out.print(treeMap.get(key) + " ");
System.out.println("");
System.out.println("linkedHashMap:");
for (Object key : linkedHashMap.keySet())
System.out.print(linkedHashMap.get(key) + " ");
System.out.println("");
System.out.println("hashtable:");
for (Object key : hashtable.keySet())
System.out.print(hashtable.get(key) + " ");
}
} |
|