首页  >  文章  >  Java  >  Java 集合类型

Java 集合类型

WBOY
WBOY原创
2024-08-30 15:48:31781浏览

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Skip List Java下一篇:Java Collection Sort