1、Collection和Collections區別:
Collection是java的一個集合接口,集合類的頂級接口
Collections是一個包裝類(工具類),不能被實例化(由於其構造函數設為私有的private),其中包含了各種關於集合操作的靜態方法,服務於Collection集合框架
2、List與Set接口的區別:
List和Set都是由Collection派生而來,其中List內的元素有序且可重復,set內的元素無序且不可重復。。。
List接口有三個實現類:ArrayList,Vector,LinkedList
Set<String> s = new HashSet<String>(); s.add(null); s.add(null); System.out.println(s.size()); //1 List<String> l = new ArrayList<String>(); l.add(null); l.add(null); System.out.println(l.size()); //2
3、HashMap,HashTable,TreeMap之間的區別:
想要理清集合類或接口之間的關系。。。首先就得看看源碼他們之間存在何種的繼承實現關系。。。
~HashMap:public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>
~AbstractMap:public abstract class AbstractMap<K,V> implements Map<K,V>
~HashTable:public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>
~Dictionary:public abstract class Dictionary<K,V>
~TreeMap:public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>
~NavigableMap:public interface NavigableMap<K,V> extends SortedMap<K,V>
~SortedMap:public interface SortedMap<K,V> extends Map<K,V>
(1):HashTable中的方法是同步的,在多線程的應用程序中,不用專門的操作就可以安全的使用HashTable,而對於HashMap
則需要額外的同步機制,其可以通過集合工具類Collections中的Collections.synchronizedMap(Map m)來實
現多線程安全,TreeMap中的方法也是不同步的。在單線程情況下,HashMap的效率要高於HashTable。
(2):在HashMap中可以允許null作為鍵且這樣的鍵只有一個,可以允許一個或多個的鍵對應的值為null,當get()方法返回null時,
既可以表示HashMap中沒有該鍵,也可以表示存在該鍵且該鍵所對應的值為null,因此在HashMap中不能用get()方法來
判斷HashMap中是否存在該鍵,而是應該用containsKey()來判斷。
Map<String, Object> map = new HashMap<String, Object>();
map.put(null, "hello");
map.put(null, "world");
map.put("linjm01", null);
map.put("linjm02", null);
System.out.println("hello:" + map.get(null)); //hello:world
System.out.println("linjm01:" + map.get("linjm01")); //linjm01:null
System.out.println("linjm02:" + map.get("linjm02")); //linjm02:null
System.out.println("linjm03:" + map.get("linjm03")); //linjm03:nul
Map<String, Object> table = new Hashtable<String, Object>();
table.put(null, "world");
table.put("table01", null);
System.out.println("table:" + table.get(null)); //java.lang.NullPointerException
System.out.println("table:" + table.get("table01")); //java.lang.NullPointerExceptio
(3):存取值的輸出順序:其中TreeMap中的元素是按照鍵來進行排序的。TreeMap自定義排序用法
Map<String, Object> map = new HashMap<String, Object>(); map.put("map1", 1); map.put("map2", 2); map.put("map3", 3); for (Entry<String, Object> entry : map.entrySet()) { System.out.println("key:" + entry.getKey() + " value:" + entry.getValue()); } /** * map輸出: * key:map3 value:3 * key:map2 value:2 * key:map1 value:1 * */ Map<String, Object> table = new Hashtable<String, Object>(); table.put("table1", 10); table.put("table2", 11); table.put("table3", 12); for (Entry<String, Object> entry : table.entrySet()) { System.out.println("key:" + entry.getKey() + " value:" + entry.getValue()); } /** * table輸出: * key:map3 value:3 * key:map2 value:2 * key:map1 value:1 * */ Map<String, Object> tree = new TreeMap<String, Object>(); tree.put("world", 101); tree.put("hello", 102); tree.put("tree", 103); tree.put("hi", 104); tree.put("welcome", 105); for (Entry<String, Object> entry : tree.entrySet()) { System.out.println("key:" + entry.getKey() + " value:" + entry.getValue()); } /** * tree輸出: * key:hello value:102 * key:hi value:104 * key:tree value:103 * key:welcome value:105 * key:world value:101 * */
(4):HashMap與HashTable的數據結構都是哈希表,而TreeMap的數據結構是二叉樹。
(5):ConcurrentHashMap是線程安全的,不過不同於hashTable,具體請參考JDK1.5查看原理,
主要采用表分段、鎖分離技術實現線程安全。。。
HashTable使用Synchronized是鎖住整張Hash表讓線程獨占、、、
而ConcurrentHashMap則不同。。將整張Hash表分為多個片段。。。每個片段都有自己的鎖進行控制。。。
因此可以實現並發地實現修改操作。。。
4、ArrayList、Vector、LinkedList三者的異同點:
(1):在初始化ArrayList時沒指定初始化長度的話,默認的長度是10;
ArrayList在增加新元素且超過了原始容量的話,ArrayList的擴容方案為原始容量的(3/2+1)1.5倍;
Vector的擴容方案為原始容量的2倍;
(2):ArrayList是非線程安全,而Vector類中的方法是同步的,在多線程的應用程序中,直接用Vector是安全的,
在單線程的應用程序中,使用ArrayList的效率要高於Vector,說白了,Vector就是ArrayList在多線程應用下的一個替代。
(3):LinkedList是鏈式線性表,ArrayList是數組線性表,其區別:
1):LinkedList適用於需要頻繁地進行插入和刪除操作,但隨機訪問速度慢,查找一個元素需要從頭開始一個一個找
2):ArrayList隨機訪問速度快,但不適用於需要頻繁地進行插入和刪除操作,因為每次插入和刪除都需要移動數組中的元素
3):兩者都不是線程安全的
4):LinkedList實現了Deque接口,而Deque接口又繼承了Queue接口,因此LinkedList也可以被當做堆棧來使用
附錄:
/** List接口 */ List<String> l1 = new ArrayList<String>(); List<String> l2 = new Vector<String>(); List<String> l3 = new LinkedList<String>(); /** Set接口 */ Set<String> s1 = new HashSet<String>(); Set<String> s2 = new LinkedHashSet<String>(); Set<String> s3 = new TreeSet<String>(); /** Map接口 */ Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> table = new Hashtable<String, Object>(); Map<String, Object> tree = new TreeMap<String, Object>();
對於java中數組轉list和list轉數組的用法:
/** 數組轉list */ String[] array = {"hello", "world", "simope", "wsview", "java"}; List<String> list = new ArrayList<String>(); list = Arrays.asList(array); for (String str : list) { System.out.print(str + " "); } System.out.println("\n=============================="); /** list轉數組 */ String[] arr = new String[list.size()]; list.toArray(arr); for (int i = 0, len = arr.length; i < len; i++) { System.out.print(arr[i] + " "); }
其中Arrays類中提供的方法都為靜態方法。。。主要用於實現數組的快速排序和搜索等。。。
集合中也有對應的類專門服務於集合操作。。。Collections類。。。