首頁 > Java > java教程 > 主體

Java 集合類型

WBOY
發布: 2024-08-30 15:48:31
原創
776 人瀏覽過

Java 集合類型,也稱為集合框架,提供了許多介面和類,有助於實現可重複使用的集合資料結構。這些集合類型提供了一種儲存一組物件並提供操作的體系結構。在 Java 中,任何表示為單一單元的單獨物件組都稱為物件集合。在此基礎上,JDK 1.2 引入了 Collection Framework 和 Types,其中包含所有集合類別和介面。一些現成的集合,如列表、集合、映射、佇列和堆疊等,解決了使用者處理一組同質和異質物件的常見問題。讓我們更深入地研究集合類型的主題並了解所有類型的語法。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

Java 集合類型/框架的層次結構

Java 集合類型

Java中的集合類型由多個介面組成,每個介面用於儲存特定的資料類型

可迭代介面

它是 Collection 框架的根介面。集合介面擴充了Iterable介面。因此,所有的類別和介面都實作這個介面。它只包含一個抽象方法,即迭代器。

文法:

Iterator iterator();
登入後複製

集合介面

它擴展了可迭代接口,並由集合框架的所有類別實現。它包含了每個集合都有的所有方法,例如刪除資料、新增資料或清除資料等。

列表介面

它是Collection接口的子接口,專用於列表類型數據,用於存儲有序的數據集合,也允許重複。列表介面由Vector、Array List、Stack等各種類別實作。由於所有這些子類別都實作了列表,因此使用者可以實例化列表物件。

文法:

List<T> array_list = new ArrayList<>();
List<T> linked_list = new LinkedList<>();
List<T> vector = new Vector<>();
登入後複製

「T」是物件的型別。

陣列列表

它提供了Java中的動態陣列。如果集合增大,則數組列表的大小會增加;如果從集合中刪除對象,則數組列表的大小會減少。在Java中,數組列表可以隨機訪問,並且不能用於原始類型,在這種情況下需要包裝類別。語法如上圖所示。

連結列表

該類別是鍊錶資料結構的實作。它是一種線性資料結構,其中元素不是連續儲存的,每個元素都是具有資料和位址部分的單獨物件。元素使用位址和指標連結起來,每個元素稱為一個節點。語法如上圖所示。

向量

該類別提供動態數組。它比標準數組慢,但對於需要大量操作的程式很有幫助。它與陣列列表類似,但 Vector 是同步的,而陣列列表不是同步的。語法如上圖所示。

堆疊

該類別建模並實作了 Stack 資料結構,並基於後進先出的基本原則。除了基本的推入、彈出操作外,此類還提供清空、檢視和搜尋功能。也可以稱為 Vector 的子類別。

文法:

Stack<T> stack = new Stack<>();
登入後複製

隊列介面

此介面維護先進先出的順序,類似佇列。它專門存儲元素順序重要的所有元素。它也由Deque、Priority Queue、Array Queue等各種類別組成。由於所有子類別都實作了佇列,因此使用者可以實例化佇列物件。

文法:

Queue<T> array_queue = new ArrayQueue<>();
Queue<T> priority_queue = new PriorityQueue<>();
登入後複製

優先隊列

當物件應該根據其優先權(即基於優先權堆)進行處理時使用它。優先權佇列中的元素會依照自然順序或使用比較器進行排序。語法如上圖所示。

數組隊列

此類在框架中實現,允許使用者擁有可調整大小的陣列。它是一種特殊類型的數組,允許使用者從隊列兩側刪除或添加元素。它沒有任何限制,並且會在必要時增長。語法如上圖所示。

雙端隊列介面

此介面是佇列資料結構的細微變化。它也稱為雙端佇列,因為可以在兩端新增和刪除元素。此介面實例化 ArrayDeque 類別。

文法:

Deque<T> deque = new ArrayDeque<>();
登入後複製

Set Interface

This class is inherent implementation for hash table data structure. Objects that are inserted into HashSet do not provide any guarantee that elements will be inserted in the same order. Objects are inserted based on hashcode and allow insertion of NULL elements too.

Syntax:

HashSet<T> hashset = new HashSet<>();
登入後複製

Linked Hash Set

It is much similar to Hash Set but uses a double linked list to store data by retaining the order of elements

Syntax:

LinkedHashSet<T> linked_hashset = new LinkedHashSet<>();
登入後複製

Sorted Set Interface

This interface is much similar to Set Interface. It has extra methods which maintain the order of elements. Interface is implemented by instantiating is Tree Set.

Syntax:

SortedSet<T> sorted_Set = new TreeSet<>();
登入後複製

Tree Set

This class uses a Tree for storage. Ordering of elements is maintained by using natural ordering else an external comparator is required.

Syntax:

TreeSet<T> tree_set = new treeSet<>();
登入後複製

Map Interface

This interface, Map is a data structure with key-value mapping data. It does not support duplicates because the same key cannot have multiple mappings. Map Interface is implemented by classes like Hash Map, Tree Map, etc.

Syntax:

Map<T> hash_map = new HashMap<>();
Map<T> tree_map = new TreeMap<>();
登入後複製

Hash Map

It provides a basic implementation of Java Map interface. To access a value in Hash Map, key is to be known. There is a technique of converting larger strings to small strings

Syntax:

HashMap<T, T> hashmap = new HashMap<T, T>();
登入後複製

Conclusion

With this, we conclude our topic “Java Collection Types”. We have seen various Interfaces and also the Iterable through which Interface came out. We have studied various interfaces such as Set, Java List, and Map interface and also covered subtypes of Java Collection Framework i.e. Stack, Queue, Deque. All the Syntax is given here which will be helpful to write logic and implement it programmatically. We have also seen the Java Collection Framework Hierarchy.

以上是Java 集合類型的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!