目錄
JAVA中常用的資料結構(java.util. ​​中)" >JAVA中常用的資料結構(java.util. ​​中)
#--------------Collection----------------
# 1、Collections
2、List
3、Vector" >3、Vector
#5、LinkedList
List總結:" >List總結:
#6、Set(介面)
7、HashSet" >7、HashSet
有序的Set,透過SortedMap來實現的。 " >有序的Set,透過SortedMap來實現的。
-------------Map----------------
1、HashMap" >1、HashMap
2、TreeMap" >2、TreeMap
首頁 Java java教程 總結分享java中幾種常用資料結構

總結分享java中幾種常用資料結構

Aug 01, 2017 am 10:54 AM
java 幾種 常用

JAVA中常用的資料結構(java.util. ​​中)

#java中有幾種常用的資料結構,主要分為Collection和map兩個主要介面(介面只提供方法,不提供實作),而程式中最終使用的資料結構是繼承自這些介面的資料結構類別。其主要的關係(繼承關係)有:  (----詳見java api文件!)

Collection---->Collections                                                                    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", whichc algorithms that operate on collections, "wrappers", coll returnc algorithms that operate on collections, "apperwrd 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 collaled new collection collm. 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? 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 thate1.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, theLinkedList class provides uniformly named methformly named methformly 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

##9、SortedSet##   API---ASet that further provides atotal ordering

on its elements. The elements are ordered using theirnatural ordering, or by a

Comparator typically provided at sorted set creation time. The set's iterator will tr​​averse 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 ronized and permits nulls.) This class makes no guarantees ronized and permits nulls。 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 based#   API----A Red-Black tree basedNavigableMap implementation. The map is sorted according to thenatural ordering of its keys, or by a

Comparator

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中幾種常用資料結構的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1318
25
PHP教程
1269
29
C# 教程
1248
24
突破或從Java 8流返回? 突破或從Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP與Python:了解差異 PHP與Python:了解差異 Apr 11, 2025 am 12:15 AM

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP與其他語言:比較 PHP與其他語言:比較 Apr 13, 2025 am 12:19 AM

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP與Python:核心功能 PHP與Python:核心功能 Apr 13, 2025 am 12:16 AM

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

PHP的影響:網絡開發及以後 PHP的影響:網絡開發及以後 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Java程序查找膠囊的體積 Java程序查找膠囊的體積 Feb 07, 2025 am 11:37 AM

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4

PHP:許多網站的基礎 PHP:許多網站的基礎 Apr 13, 2025 am 12:07 AM

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。

See all articles