java容器類知識(shí)點(diǎn)詳細(xì)總結(jié)
1.java容器分類圖
2.容器類接口和抽象容器類
2.1 說明
容器接口是容器的基礎(chǔ)。使用接口可以將容器的實(shí)現(xiàn)與容器接口分開,因而可以使用相同的方法訪問容器而不需關(guān)心容器具體的數(shù)據(jù)結(jié)構(gòu)。
同理,Iterator接口也使用戶能夠使用相同的方法訪問不同的容器類。
2.2 容器接口(Collection,Map,Iterator)
1)collection接口
boolean add(Object obj): 添加對(duì)象,集合發(fā)生變化則返回true Iterator iterator():返回Iterator接口的對(duì)象 int size() boolean isEmpty() boolean contains(Object obj) void clear() <T> T[] toArray(T[] a)
2)Map接口(存放鍵值對(duì),Map中的值也可以是一個(gè)容器)
Object get(Object key) Object put(Object key, Object value) Set keySet() : returns the keys set Set<K> keySet() Set entrySet(): returns mappings set Set<Map.Entry<K,V>> entrySet() containsKey() containsValue()
3)Iterator接口
Object next() boolean hasNext() void remove()
注意:remove函數(shù)不能連續(xù)執(zhí)行多次,否則返回IllegalStateException
( if the next method has not yet been called, or the remove method has already been called after the last call to the next method.)
通常用法:
Iterator it=collection.iterator(); while(it.hasNext()) { Object obj=it.next(); //do something }
2.3 子接口(List,Set,ListIterator,SortedMap,SortedSet)
1)List(有順序可以重復(fù),有順序所以操作時(shí)可以在方法中加入索引參數(shù),如下:)
boolean add(E element) void add(int index, E element) E set(int index, E element) E get(int index);
2)Set(無順序不可以重復(fù),無序因而不能通過索引操作對(duì)象)
3)ListIterator(Iterator for List,List是雙向表,因而在Iterator上增加了一些新的方法,允許traverse the List in either direction)
boolean hasPrevious(); E previous(); int previousIndex()
4) SortedMap
說明:保證按照鍵的升序排列的映射,可以按照鍵的自然順序( Comparable 接口)進(jìn)行排序, 或者通過創(chuàng)建有序映射時(shí)提供的比較器進(jìn)行排序
(A Map that further provides a total ordering on its keys. The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time)
public interface SortedMap<K,V>extends Map<K,V> Comparator comparator() Object firstKey() Object lastKey()
5)SortedSet
主要用于排序操作,實(shí)現(xiàn)此接口的子類都是排序的子類
public interface SortedSet<E>extends Set<E> * Comparator comparator() * E first() :返回第一個(gè)元素 * E last() * SortedSet<E> headSet(E toElement): 返回less than toElement * SortedSet<E> tailSet(E fromElement) * SortedSet<E> subSet(E fromElement)
2.4抽象容器類
1)說明:使用抽象容器類可以方便的定義類,而不用在每個(gè)類中都實(shí)現(xiàn)容器接口container 中的所有的方法
2)包含:
* AbstractCollection public abstract class AbstractCollection<E>extends Objectimplements Collection<E> * AbstractList public abstract class AbstractList<E>extends AbstractCollection<E>implements List<E> * AbstractSet public abstract class AbstractSet<E>extends AbstractCollection<E>implements Set<E> * AbstactMap public abstract class AbstractMap<K,V>extends Object implements Map<K,V> * AbstractSequentialList public abstract class AbstractSequentialList<E> extends AbstractList<E>
3.具體容器類
3.1概括
1)collection: ArrayList,LinkedLsit,Vector,Stack
TreeSet,HashSet,LinkedHashSet
2) Map: HashMap,LinkedHashMap,WeakHashMap, TreeMap, HashTable, IdentityHashTable(其中key的比較是通過==而不是equals)
3.2常用的容器類
1)ArrayList 與 LinkedList(均非同步,多線程時(shí)需要考慮線程安全問題),Vector(同步),Stack
1. List 接口支持通過索引的方法來訪問元素:ArrayList 隨機(jī)訪問快改慢;LinkedList改快隨機(jī)訪問慢;Vector實(shí)現(xiàn)了同步,因而比ArrayList慢
2. LinkedList使用雙向鏈表實(shí)現(xiàn)LinkedList提供額外的get,remove,insert方法在LinkedList的首部或尾部。這些操作使LinkedList可被用作堆棧(stack),隊(duì)列(queue)或雙向隊(duì)列(deque)。
3. ArrayList沒有定義增長算法,當(dāng)需要插入大量元素是,可調(diào)用ensureCapacity方法提高添加效率
4. Vector類似與ArrayList,但是是同步的,多線程安全(另外一點(diǎn)區(qū)別是ArrayList擴(kuò)容時(shí)默認(rèn)增長一半,Vector增長一倍)。無論是單線程還是多線程,Vector都比ArrayList慢
5. Stack繼承自Vector,實(shí)現(xiàn)一個(gè)后進(jìn)先出的堆棧
6.若需要實(shí)現(xiàn)同步可以調(diào)用Collections工具類的synchronizedList方法,如下:
List list = Collections.synchronizedList(new ArrayList()); synchronized(list) { Iterator i = list.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } 或者: List list = Collections.synchronizedList(new LinkedList());
7.定義如下:(注意LinkedList實(shí)現(xiàn)了Deque)
public class ArrayList<E>extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, Serializable public class LinkedList<E>extends AbstractSequentialList<E>implements List<E>, Deque<E>, Cloneable, Serializable public class Vector<E>extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, Serializable
2)TreeSet, HashSet, LinkedHashSet(HashSet,TreeSet不是線程安全的)
1. TreeSet是SortedSet接口的唯一實(shí)現(xiàn)類,TreeSet可以確保集合元素處于排序狀態(tài),效率很高,可提高程序的效率;TreeSet通過compareTo或者compare排序,因而只要值相等即使equals不等(不同對(duì)象)也不能加到集合中(fails to obey Set interface)
2. HashSet,效率很高,和TreeSet不同的是通過比較對(duì)象的equals區(qū)分不同對(duì)象,這樣不同的對(duì)象可以不被重復(fù)的加入到集合中。
hashCode()函數(shù)不好確定,對(duì)象默認(rèn)的hashCode函數(shù)試對(duì)象的內(nèi)存地址值,hashCode函數(shù)的好壞是HashSet性能的關(guān)鍵。
3. LinkedHashSet,和HashSet相同,同樣是根據(jù)元素的hashCode值來決定元素的存儲(chǔ)位置,但是它同時(shí)使用鏈表維護(hù)元素的次序。LinkedHashSet在迭代訪問Set中的全部元素時(shí),性能比HashSet好,但是插入時(shí)性能稍微遜色于HashSet。
4. Set可以插入null,最多一個(gè)null
3) HashMap(非同步), HashTable(線程安全), TreeMap, WeakHashMap
1.HashTable與HashMap區(qū)別:(詳情請(qǐng)查看HashTable與HashMap)
- Hashtable繼承自Dictionary類,而HashMap繼承自AbstractMap類。但二者都實(shí)現(xiàn)了Map接口。
- Hashtable 中的方法是Synchronize的,而HashMap中的方法在缺省情況下是非Synchronize的
- Hashtable中,key和value都不允許出現(xiàn)null值;HashMap中,null可以作為鍵,這樣的鍵只有一個(gè);可以有一個(gè)或多個(gè)鍵所對(duì)應(yīng) 的值為null
- HashTable直接使用對(duì)象的hashCode。而HashMap重新計(jì)算hash值。
2. WeakHashMap是一種改進(jìn)的HashMap,它對(duì)key實(shí)行“弱引用”,WeakHashMap使用元素的引用而不是值作為key,也就是說必須在引用相同(a==b)的情況下才能找到相關(guān)的值。另外,如果一個(gè)key不再被外部所引用,那么該key可以被GC回收。
3. TreeMap是SortedMap接口的基于紅黑樹的實(shí)現(xiàn)。此類保證了映射按照升序順序排列關(guān)鍵字, 根據(jù)使用的構(gòu)造方法不同,可能會(huì)按照鍵的類的自然順序進(jìn)行排序
4.定義如下:
public class Hashtable extends Dictionary implements Map, Cloneable, Serializable public class HashMap extends AbstractMap implements Map, Cloneable, Serializable public class TreeMap<K,V>extends AbstractMap<K,V>implements NavigableMap<K,V>, Cloneable, Serializable
4.容器類使用補(bǔ)充
1)使用抽象編程思想,創(chuàng)建時(shí)使用父類引用指向子類對(duì)象,返回時(shí)返回抽象接口
2)如果涉及到堆棧,隊(duì)列等操作,應(yīng)該考慮用List,對(duì)于需要快速插入,刪除元素,應(yīng)該使用LinkedList,如果需要快速隨機(jī)訪問元素,應(yīng)該使用ArrayList。
3)如果程序在單線程環(huán)境中使用非同步的類,其效率較高
4)可以使用Collections 工具類中unmodifiableList/unmodifiableMap/unmodifiableSet/unmodifiableSortedMap/unmodifiableSortedSet等創(chuàng)建不能修改的List,Map,List等
5)可以使用Collections工具類中Collections.synchronizedList(new ArrayList())等實(shí)現(xiàn)同步
6) 可以使用Arrays.equal()判斷兩個(gè)數(shù)組是否相等
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot獲取Request對(duì)象的常見方法
HttpServletRequest 簡稱 Request,它是一個(gè) Servlet API 提供的對(duì)象,用于獲取客戶端發(fā)起的 HTTP 請(qǐng)求信息,那么在SpringBoot中,獲取 Request對(duì)象的方法有哪些呢,本文小編將給大家講講SpringBoot獲取Request對(duì)象的常見方法2023-08-08Java中通過sftp協(xié)議實(shí)現(xiàn)上傳下載的示例代碼
在java開發(fā)中遇到需要將linux系統(tǒng)中指定目錄下的文件下載到windows本地的需求,本文就來介紹Java中通過sftp協(xié)議實(shí)現(xiàn)上傳下載,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06Javaweb mybatis接口開發(fā)實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Javaweb mybatis接口開發(fā)實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07解析阿里一面CyclicBarrier和CountDownLatch的區(qū)別
這篇文章主要介紹了阿里一面CyclicBarrier和CountDownLatch的區(qū)別是啥,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03