+ -

Java中常见的8种数据结构(附实现代码)

时间:2025-08-28

来源:互联网

在手机上看
手机扫描阅读

在 Java 编程中,数据结构是程序设计和算法实现的基础。合理选择和使用数据结构可以显著提高程序的效率和可维护性。Java 提供了丰富的内置数据结构类,如 List、Set、Map 等,同时也支持自定义实现。本文将介绍 Java 中常见的 8 种数据结构,并附上简单的实现代码,帮助开发者更好地理解和应用这些数据结构。

一、数组(Array)

数组是最基础的数据结构,用于存储固定大小的同类型元素。Java 中的数组可以通过声明和初始化来创建。

int[]arr={1,2,3,4,5};
System.out.println(arr[0]);//输出1

数组访问速度快,但长度固定,无法动态扩展。

二、链表(Linked List)

链表由节点组成,每个节点包含数据和指向下一个节点的指针。Java 中没有直接的链表类,但可以通过自定义类实现。

classNode{
intdata;
Nodenext;
publicNode(intdata){
this.data=data;
this.next=null;
}
}
publicclassLinkedList{
Nodehead;
publicvoidadd(intdata){
Nodenode=newNode(data);
if(head==null){
head=node;
}else{
Nodecurrent=head;
while(current.next!=null){
current=current.next;
}
current.next=node;
}
}
publicvoidprint(){
Nodecurrent=head;
while(current!=null){
System.out.print(current.data+"");
current=current.next;
}
}
}

链表适合频繁插入和删除操作,但随机访问效率较低。

三、栈(Stack)

栈是一种后进先出(LIFO)的数据结构,常用于递归、表达式求值等场景。

importjava.util.Stack;
publicclassStackExample{
publicstaticvoidmain(String[]args){
Stack<Integer>stack=newStack<>();
stack.push(10);
stack.push(20);
System.out.println(stack.pop());//输出20
System.out.println(stack.peek());//输出10
}
}

四、队列(Queue)

队列是一种先进先出(FIFO)的数据结构,适用于任务调度、缓冲区等场景。

importjava.util.Queue;
importjava.util.LinkedList;
publicclassQueueExample{
publicstaticvoidmain(String[]args){
Queue<Integer>queue=newLinkedList<>();
queue.add(10);
queue.add(20);
System.out.println(queue.poll());//输出10
System.out.println(queue.peek());//输出20
}
}

五、哈希表(Hash Table)

哈希表通过键值对存储数据,提供快速的查找、插入和删除操作。Java 中的 HashMap 是其典型实现。

importjava.util.HashMap;
publicclassHashTableExample{
publicstaticvoidmain(String[]args){
HashMap<String,Integer>map=newHashMap<>();
map.put("Apple",1);
map.put("Banana",2);
System.out.println(map.get("Apple"));//输出1
}
}

六、树(Tree)

树是一种非线性的层次结构,常用于表示具有父子关系的数据。Java 中可通过自定义类实现二叉树。

classTreeNode{
intvalue;
TreeNodeleft,right;
publicTreeNode(intvalue){
this.value=value;
left=right=null;
}
}
publicclassBinaryTree{
publicstaticvoidmain(String[]args){
TreeNoderoot=newTreeNode(1);
root.left=newTreeNode(2);
root.right=newTreeNode(3);
}
}

七、图(Graph)

图由顶点和边构成,用于表示复杂的关系网络。Java 中可通过邻接表或邻接矩阵实现。

importjava.util.*;
publicclassGraph{
privateMap<Integer,List<Integer>>adjList;
publicGraph(){
adjList=newHashMap<>();
}
publicvoidaddEdge(intv1,intv2){
adjList.putIfAbsent(v1,newArrayList<>());
adjList.putIfAbsent(v2,newArrayList<>());
adjList.get(v1).add(v2);
adjList.get(v2).add(v1);
}
publicvoidprint(){
for(Map.Entry<Integer,List<Integer>>entry:adjList.entrySet()){
System.out.println(entry.getKey()+"->"+entry.getValue());
}
}
}

八、集合(Set)

集合用于存储不重复的元素,Java 中有 HashSet、TreeSet 等实现。

importjava.util.HashSet;
publicclassSetExample{
publicstaticvoidmain(String[]args){
HashSet<String>set=newHashSet<>();
set.add("A");
set.add("B");
set.add("A");//不会重复添加
System.out.println(set);//输出[A,B]
}
}

Java中常见的8种数据结构(附实现代码)

Java 中常见的 8 种数据结构各具特点,适用于不同的应用场景。从基础的数组到复杂的图结构,掌握它们的原理和实现方式,有助于提升编程能力和算法设计水平。无论是日常开发还是面试准备,理解并灵活运用这些数据结构都是必不可少的技能。希望本文能够帮助读者更深入地了解 Java 中的数据结构及其实际应用。

以上就是php小编整理的全部内容,希望对您有所帮助,更多相关资料请查看php教程栏目。

今日更新

热门下载

更多