??????????????? Java ????????????Щ???????????Щ????????????????????????? LinkedList??ArrayList??Vector??Stack??Hashtable??HashMap??WeakHashMap ????????Щ???????????????t?????н??????????? WeakHashMap?????????????????????????????????????????????????Щ???
??????????????????л??к????????θ?Ч???????????????????????????????????????????Java ????????????????????????????????????????????????????????? Java ???????JDK ???????????????????????????????????????????????? java.util ????????嵥 1 ????????????????
?????嵥 1.???????????
????Collection
??????List
????????LinkedList
????????ArrayList
????????Vector
??????????Stack
??????Set
????Map
??????Hashtable
??????HashMap
??????WeakHashMap
?????????????????????t????????????????д?????? JDK7??
??????????
????Collection ???

????Collection ???????????????? Collection ??????? Object???? Collection ??????Elements?????Щ Collection ??????????????????????????????Щ???С?JDK ??????????? Collection ????JDK ???????????? Collection ????????? List ?? Set????????? Collection ????????????????????????????????????????????????????? Collection??????? Collection ??????????????????????μ? Collection??????μ? Collection ????? Collection ???????????????????????????????????? Collection??
??????α??? Collection ?е?????????
???????? Collection ???????????Σ??????????? iterator() ????????÷???????????????????????????????????? Collection ????????????????÷????£?
????Iterator it = collection.iterator(); // ????????????
????while(it.hasNext()){
????Object obj = it.next(); // ???????????
????}
????Collection ?????????????????? List ?? Set??
????Collection ????????????????
????boolean add(Object o) ???????????
????boolean remove(Object o) ???????????
????int size() ??????????????????????
????boolean contains(Object o) ??????????????????????
????boolean isEmpty() ?ж???????????
????Iterator iterator() ???????????????
????boolean containsAll(Collection c) ?????????????м??? C ?е?????
????boolean addAll(Collection c) ?????? C ?????е?????????ü????
????void clear() ?????????????????
????void removeAll(Collection c) ?????????? C ????????е?????
????void retainAll(Collection c) ?????????????? C ?в???????????
????List ???
????List ??????? Collection?????????????????????????????λ?á????????????????????? List ?е?λ??????????????±??????? List ?е????????????? Java ?????顣??????????? Set ?????List ???????????????
??????????? Collection ??????? iterator() ??????List ??????? listIterator() ????????????? ListIterator ?????????? Iterator ???????ListIterator ?????Щ add() ???????????????????????趨???????????????????????? List ??????????? LinkedList??ArrayList??Vector ?? Stack ???
????List ????????????????
????void add(int index??Object element) ?????λ??????????????
????boolean addAll(int index??Collection c) ?????? C ??????????????λ???
????Object get(int index) ???? List ?????λ???????
????int indexOf(Object o) ??????????????? O ??λ???
????Object removeint(int index) ??????λ???????
????Object set(int index??Object element) ????? element ???λ?? index ??????? ??????????????
????Map ???
????Map ??м?? Collection ????Map ?? Key ?? Value ???????? Map ?в??????????? Key????? Key ????????? Value??Map ????? 3 ???????????Map ????????????????? Key ???????? Value ???????????? Key-Value ???
????Map ?????????????
????boolean equals(Object o) ??????
????boolean remove(Object o) ??????????
????put(Object key??Object value) ??? key ?? value??
????RandomAccess ???
????RandomAccess ??????????????????????????κη?????????????????? RandomAccess ??????????????????????????????????????????????????Щ???????????????? List ?????κ????????????? List ????????? RaodomAccess ??????????????????????С?????????????????п???????????????????????????????????????????????????????????????????ó???????????????? List ????????????п????????????????????? List ???в???????????????????????
?????????????
????LinkedList ??

????LinkedList ????? List ???????? Null ???????? LinkedList ??????? Get??Remove??Insert ??????? LinkedList ???????β?????????????Щ??????? LinkedList ????????????Stack???????У?Queue?????????У?Deque????????? LinkedList ??????????????????????????????????????????????? List????????????????????????????????????? List ????????????? List????????
????List list = Collections.synchronizedList(new LinkedList(...))??
????ArrayList ??
????ArrayList ????????С?????顣?????????????????? Null??Size??IsEmpty??Get??Set ????????????????????????? Add ?????????????????????? N ???????? O(N) ???????????????????????????
??????? ArrayList ????????????????Capacity????????洢??????????С?????????????????????????????????????????????????????????????????? ensureCapacity ?????????? ArrayList ??????????????Ч????? LinkedList ?????ArrayList ?????????????unsynchronized????
????ArrayList ?????????????
????Boolean add(Object o) ?????????????б???β??
????Boolean add(int index??Object element) ???б??????λ?ü??????????
????Boolean addAll(Collection c) ??????????????б??β??
????Boolean addAll(int index??Collection c) ???б??????λ?ü???????????
????Boolean clear() ????б???????????
????Boolean clone() ??????б???????????????
????Boolean contains(Object o) ?ж??б?????????????
????Boolean ensureCapacity(int m) ?????б??????????????????б???????? m ??????
????Object get(int index) ?????б??????λ???????
????Int indexOf(Object elem) ???б??в???????????±?
????Int size() ???????б??????????
????Vector ??
????Vector ????????? ArrayList???????? Vector ????????????? Vector ?????? Iterator??????? ArrayList ?????? Iterator ???????????????? Vector ????????????? Iterator ??????????????????????????????? Vector ?????????磬??????????Щ????????????? Iterator ??????????? ConcurrentModificationException???????????????
????Stack ??
????Stack ????? Vector??????????????????????Stack ?? 5 ????????????? Vector ??????????????á?????????? Push ?? Pop ?????????? Peek ????????????????Empty ?????????????????Search ??????????????????е?λ?á????Stack ?????????????
????Set ??
????Set ??????????????????? Collection???????????????? e1 ?? e2 ???? e1.equals(e2)=false??Set ??????? null ???????????Set ?????????????????????????? Collection ?????????????????????????????С???????????Mutable Object?????????? Set ?е???????????????????????????Щ????
????Hashtable ??
????Hashtable ??? Map ???????????????? Key-Value ??????????κη???non-null???????????? Key ???? Value???????????? Put(Key??Value)???????????? Get(Key)????????????????????儷?????????
????Hashtable ??? Initial Capacity ?? Load Factor ???????????????????????? Load Factor 0.75 ??????????????????????? Load Factor ???????????????????佫??????????? Get ?? Put ?????????????? Hashtable ?????????? 1??2??3 ???????????? Hashtable ???棬????? Key ?????”one”??”two”??”three”?????????嵥 2 ?????
?????嵥 2 .Hashtable ???
????Hashtable numbers = new Hashtable();
????numbers.put(“one”?? new Integer(1));
????numbers.put(“two”?? new Integer(2));
????numbers.put(“three”?? new Integer(3));
???????????????????????????? 2????????????? key ??????????????嵥 3 ?????
?????嵥 3.?? Hastable ???????
????Integer n = (Integer)numbers.get(“two”);
????System.out.println(“two =”+ n);
??????????? Key ????????????????к???????????????? Value ??λ???????κ???? key ???????????? HashCode ?? Equals ??????HashCode ?? Equals ???????????? Object??????????????????? Key ????????С?????????к???????壬?????????????????? obj1.equals(obj2)=true????????? HashCode ???????????????????????????????? HashCode ????????????????????????? HashCode ??????????????????????????2???????????儷??????????????????? HashCode() ????????????????????
????????????????в???? HashCode?????????????????????????????????? Get ???????? Null???????????????????????д Equals ?????? HashCode ????????????д?????????
????HashMap ??
????HashMap ?? Hashtable ??????????????? HashMap ???????????????????? Null???? Null Value ?? Null Key??????? HashMap ??? Collection ???values() ????????? Collection????????????????儷???? HashMap ????????????????????????????????????????????????? HashMap ????????????ù???????? Load Factor ???????ù????
????WeakHashMap ??
????WeakHashMap ????????? HashMap?????? Key ???“??????”???????? Key ??????????????????? Key ????? GC ?????
?????????????
????ArrayList??Vector??LinkedList ?????? AbstractList ???????? AbstractList ???????? List ??????????? AbstarctCollection??ArrayList ?? Vector ?????????????ArrayList ??ж??κ????????????????????????????????Vector ?о??????????????????????????????????????LinkedList ???????????????????????????б????????????????????????? 3 ?????????????????????????????
?????? ArrayList ???????????????????????С???????????????????????У?????д????????鸴????????????鸴???????????? System.arraycopy() ??????LinkedList ??????????????????????????????????С???????ε?????????????????? Entry ?????????и??????????????????????????£???????????????????????????????μ???????????????????????????????????????????????β???????????????????????????????????????鸴???
????ArrayList ??????????????????????????????????????????????????λ?ò?????????????????λ?ú???????????????????У??????Ч??????????????????β????LinkedList ???????????????????????????
????ArrayList ????????Ч???????????????????????????飬????????????λ??????????????????????????????????λ????????????С??LinkedList ?????м????????????????? List??
?????嵥 4. ArrayList ?? LinkedList ??????
????import java.util.ArrayList;
????import java.util.LinkedList;
????public class ArrayListandLinkedList {
????public static void main(String[] args){
????long start = System.currentTimeMillis();
????ArrayList list = new ArrayList();
????Object obj = new Object();
????for(int i=0;i<5000000;i++){
????list.add(obj);
????}
????long end = System.currentTimeMillis();
????System.out.println(end-start);
????start = System.currentTimeMillis();
????LinkedList list1 = new LinkedList();
????Object obj1 = new Object();
????for(int i=0;i<5000000;i++){
????list1.add(obj1);
????}
????end = System.currentTimeMillis();
????System.out.println(end-start);
????start = System.currentTimeMillis();
????Object obj2 = new Object();
????for(int i=0;i<1000;i++){
????list.add(0??obj2);
????}
????end = System.currentTimeMillis();
????System.out.println(end-start);
????start = System.currentTimeMillis();
????Object obj3 = new Object();
????for(int i=0;i<1000;i++){
????list1.add(obj1);
????}
????end = System.currentTimeMillis();
????System.out.println(end-start);
????start = System.currentTimeMillis();
????list.remove(0);
????end = System.currentTimeMillis();
????System.out.println(end-start);
????start = System.currentTimeMillis();
????list1.remove(250000);
????end = System.currentTimeMillis();
????System.out.println(end-start);
????}
????}
?????嵥 5. ???????
????639
????1296
????6969
????0
????0
????15
????HashMap ??? Key ?? Hash ??????? Hash ????????????????? Key ?????????????? HashMap ?У??????????????????飬??ν???????????????±???????HashMap ????????????????????
????Hash ?????????Ч???
????Hash ???????? (????????) ???????????
????????????? (????????) ?????????????????
????HashMap ??????????????????顣????????????????? HashMap ????????????????? HashCode() ?? Hash() ???????????????????????????????????????? HashMap ???????????????????????????????????к???????????????? HashCode() ???? Hash() ???????????????????????????£?HashMap ???????????????????? HashMap ??????????????????????????
????HashMap ??????????????????????????????? HashMap ?е?????????? HashMap ??????????????????????????????????????????? LinkedHashMap ?????
????LinkedHashMap ????? HashMap?????и?Ч??????? HashMap ??????????????????????????????????????????
????HashMap ??? hash ????????????? Put() ?? Get() ??????TreeMap ????????????????? Map ??????????????TreeMap ????? HashMap ?????????????????? SortedMap ????????ζ???????????????????TreeMap ????????????? HashMap????????????????????????????????? HashMap ??????????????????? TreeMap ??????????????????????С?LinkedHashMap ????????????????????????????????????TreeMap ????????????????? (?? Comparator ???? Comparable ???)??
????LinkedHashMap ?????????????????????????????????? TreeMap ????????? Key ????????
?????嵥 6 ??????????????? TreeMap ???????????????
?????嵥 6. TreeMap ???????
????import java.util.Iterator;
????import java.util.Map;
????import java.util.TreeMap;
????public class Student implements Comparable<Student>{
????public String name;
????public int score;
????public Student(String name??int score){
????this.name = name;
????this.score = score;
????}
????@Override
????//???? TreeMap ???????
????public int compareTo(Student o) {
????// TODO Auto-generated method stub
????if(o.score<this.score){
????return 1;
????}else if(o.score>this.score){
????return -1;
????}
????return 0;
????}
????@Override
????public String toString(){
????StringBuffer sb = new StringBuffer();
????sb.append("name:");
????sb.append(name);
????sb.append(" ");
????sb.append("score:");
????sb.append(score);
????return sb.toString();
????}
????public static void main(String[] args){
????TreeMap map = new TreeMap();
????Student s1 = new Student("1"??100);
????Student s2 = new Student("2"??99);
????Student s3 = new Student("3"??97);
????Student s4 = new Student("4"??91);
????map.put(s1?? new StudentDetailInfo(s1));
????map.put(s2?? new StudentDetailInfo(s2));
????map.put(s3?? new StudentDetailInfo(s3));
????map.put(s4?? new StudentDetailInfo(s4));
????//???????λ?? S4 ?? S2 ??????
????Map map1=((TreeMap)map).subMap(s4?? s2);
????for(Iterator iterator=map1.keySet().iterator();iterator.hasNext();){
????Student key = (Student)iterator.next();
????System.out.println(key+"->"+map.get(key));
????}
????System.out.println("subMap end");
????//????????? s1 ?????
????map1=((TreeMap)map).headMap(s1);
????for(Iterator iterator=map1.keySet().iterator();iterator.hasNext();){
????Student key = (Student)iterator.next();
????System.out.println(key+"->"+map.get(key));
????}
????System.out.println("subMap end");
????//????????? s1 ?????
????map1=((TreeMap)map).tailMap(s1);
????for(Iterator iterator=map1.keySet().iterator();iterator.hasNext();){
????Student key = (Student)iterator.next();
????System.out.println(key+"->"+map.get(key));
????}
????System.out.println("subMap end");
????}
????}
????class StudentDetailInfo{
????Student s;
????public StudentDetailInfo(Student s){
????this.s = s;
????}
????@Override
????public String toString(){
????return s.name + "'s detail information";
????}
????}
?????嵥 7 .???????
????name:4 score:91->4's detail information
????name:3 score:97->3's detail information
????subMap end
????name:4 score:91->4's detail information
????name:3 score:97->3's detail information
????name:2 score:99->2's detail information
????subMap end
????name:1 score:100->1's detail information
????subMap end