1. 程式人生 > >開發java中常用的幾種數據類型

開發java中常用的幾種數據類型

api dep 同步問題 end ike 文檔 添加 nbsp gravity

JAVA中常用的數據結構(java.util. 中)

java中有幾種常用的數據結構,主要分為Collection和map兩個主要接口(接口只提供方法,並不提供實現),而程序中最終使用的數據結構是繼承自這些接口的數據結構類。其主要的關系(繼承關系)有: (----詳細參見java api文檔!)

Collection---->Collections Map----->SortedMap------>TreeMap

Collection---->List----->(Vector \ ArryList \ LinkedList) Map------>HashMap

Collection---->Set------>(HashSet \ LinkedHashSet \ SortedSet)

技術分享圖片

技術分享圖片

--------------Collection----------------

1、Collections

API----This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends.

The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.

2、List

API----This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends.

The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.

List是有序的Collection,使用此接口能夠精確的控制每個元素插入的位置。用戶能夠使用索引(元素在List中的位置,類似於數組下 >標)來訪問List中的元素,這類似於Java的數組。

3、Vector

API----The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of aVector can grow or shrink as needed to accommodate adding and removing items after theVector has been created.

基於數組(Array)的List,其實就是封裝了數組所不具備的一些功能方便我們使用,所以它難易避免數組的限制,同時性能也不可能超越數組。所以,在可能的情況下,我們要多運用數組。另外很重要的一點就是Vector是線程同步的(sychronized)的,這也是Vector和ArrayList 的一個的重要區別。

4、ArrayList

API----Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, includingnull. In addition to implementing theList interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent toVector, except that it is unsynchronized.)

同Vector一樣是一個基於數組上的鏈表,但是不同的是ArrayList不是同步的。所以在性能上要比Vector好一些,但是當運行到多線程環境中時,可需要自己在管理線程的同步問題。

5、LinkedList

API----Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (includingnull). In addition to implementing theList interface, the LinkedList class provides uniformly named methods toget, remove andinsert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack,queue, ordouble-ended queue.

LinkedList不同於前面兩種List,它不是基於數組的,所以不受數組性能的限制。
它每一個節點(Node)都包含兩方面的內容:
1.節點本身的數據(data);
2.下一個節點的信息(nextNode)。
所以當對LinkedList做添加,刪除動作的時候就不用像基於數組的ArrayList一樣,必須進行大量的數據移動。只要更改nextNode的相關信息就可以實現了,這是LinkedList的優勢。

List總結:

  • 所有的List中只能容納單個不同類型的對象組成的表,而不是Key-Value鍵值對。例如:[ tom,1,c ]

  • 所有的List中可以有相同的元素,例如Vector中可以有 [ tom,koo,too,koo ]

  • 所有的List中可以有null元素,例如[ tom,null,1 ]

基於Array的List(Vector,ArrayList)適合查詢,而LinkedList 適合添加,刪除操作

6、Set(接口)

API-----A collection that contains no duplicate elements. More formally, sets contain no pair of elementse1 ande2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematicalset abstraction.

Set是不包含重復元素的Collection

7、HashSet

API-----This class implements theSet interface, backed by a hash table (actually aHashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits thenull element.

雖然Set同List都實現了Collection接口,但是他們的實現方式卻大不一樣。List基本上都是以Array為基礎。但是Set則是在 HashMap的基礎上來實現的,這個就是Set和List的根本區別。HashSet的存儲方式是把HashMap中的Key作為Set的對應存儲項。看看 HashSet的add(Object obj)方法的實現就可以一目了然了。

8、LinkedHashSet

API----Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (includingnull). In addition to implementing theList interface, the LinkedList class provides uniformly named methods toget, remove andinsert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack,queue, ordouble-ended queue.

HashSet的一個子類,一個鏈表。

9、SortedSet

API---ASet that further provides atotal ordering on its elements. The elements are ordered using theirnatural ordering, or by aComparator typically provided at sorted set creation time. The set‘s iterator will traverse the set in ascending element order. Several additional operations are provided to take advantage of the ordering. (This interface is the set analogue ofSortedMap.)

有序的Set,通過SortedMap來實現的。

Set總結:

(1)Set實現的基礎是Map(HashMap)(2)Set中的元素是不能重復的,如果使用add(Object obj)方法添加已經存在的對象,則會覆蓋前面的對象

-------------Map-----------------

Map 是一種把鍵對象和值對象進行關聯的容器,而一個值對象又可以是一個Map,依次類推,這樣就可形成一個多級映射。對於鍵對象來說,像Set一樣,一個 Map容器中的鍵對象不允許重復,這是為了保持查找結果的一致性;如果有兩個鍵對象一樣,那你想得到那個鍵對象所對應的值對象時就有問題了,可能你得到的並不是你想的那個值對象,結果會造成混亂,所以鍵的唯一性很重要,也是符合集合的性質的。當然在使用過程中,某個鍵所對應的值對象可能會發生變化,這時會按照最後一次修改的值對象與鍵對應。對於值對象則沒有唯一性的要求,你可以將任意多個鍵都映射到一個值對象上,這不會發生任何問題(不過對你的使用卻可能會造成不便,你不知道你得到的到底是那一個鍵所對應的值對象)。

1、HashMap

API----Hash table based implementation of theMap interface. This implementation provides all of the optional map operations, and permitsnull values and the null key. (The HashMap class is roughly equivalent toHashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

2、TreeMap

API----A Red-Black tree basedNavigableMap implementation. The map is sorted according to thenatural ordering of its keys, or by aComparator provided at map creation time, depending on which constructor is used.

TreeMap則是對鍵按序存放,因此它便有一些擴展的方法,比如firstKey(),lastKey()等,你還可以從TreeMap中指定一個範圍以取得其子Map。
鍵和值的關聯很簡單,用put(Object key,Object value)方法即可將一個鍵與一個值對象相關聯。用get(Object key)可得到與此key對象所對應的值對象。

------------說明-----------

一、幾個常用類的區別
1.ArrayList: 元素單個,效率高,多用於查詢
2.Vector: 元素單個,線程安全,多用於查詢
3.LinkedList:元素單個,多用於插入和刪除
4.HashMap: 元素成對,元素可為空
5.HashTable: 元素成對,線程安全,元素不可為空
二、Vector、ArrayList和LinkedList
大多數情況下,從性能上來說ArrayList最好,但是當集合內的元素需要頻繁插入、刪除時LinkedList會有比較好的表現,但是它們三個性能都比不上數組,另外Vector是線程同步的。所以:
如果能用數組的時候(元素類型固定,數組長度固定),請盡量使用數組來代替List;
如果沒有頻繁的刪除插入操作,又不用考慮多線程問題,優先選擇ArrayList;
如果在多線程條件下使用,可以考慮Vector;
如果需要頻繁地刪除插入,LinkedList就有了用武之地;
如果你什麽都不知道,用ArrayList沒錯。
三、Collections和Arrays
在 Java集合類框架裏有兩個類叫做Collections(註意,不是Collection!)和Arrays,這是JCF裏面功能強大的工具,但初學者往往會忽視。按JCF文檔的說法,這兩個類提供了封裝器實現(Wrapper Implementations)、數據結構算法和數組相關的應用。
想必大家不會忘記上面談到的“折半查找”、“排序”等經典算法吧,Collections類提供了豐富的靜態方法幫助我們輕松完成這些在數據結構課上煩人的工作:
binarySearch:折半查找。
sort:排序,這裏是一種類似於快速排序的方法,效率仍然是O(n * log n),但卻是一種穩定的排序方法。
reverse:將線性表進行逆序操作,這個可是從前數據結構的經典考題哦!
rotate:以某個元素為軸心將線性表“旋轉”。
swap:交換一個線性表中兩個元素的位置。
……
Collections還有一個重要功能就是“封裝器”(Wrapper),它提供了一些方法可以把一個集合轉換成一個特殊的集合,如下:
unmodifiableXXX:轉換成只讀集合,這裏XXX代表六種基本集合接口:Collection、List、Map、Set、SortedMap和SortedSet。如果你對只讀集合進行插入刪除操作,將會拋出UnsupportedOperationException異常。
synchronizedXXX:轉換成同步集合。
singleton:創建一個僅有一個元素的集合,這裏singleton生成的是單元素Set,
singletonList和singletonMap分別生成單元素的List和Map。
空集:由Collections的靜態屬性EMPTY_SET、EMPTY_LIST和EMPTY_MAP表示。

開發java中常用的幾種數據類型