30 Java常用类之集合类

在前一章中,我们讨论了 Java 的日期类及其在处理日期和时间时的重要性。本章将重点介绍 Java 的集合类,它们是数据存储和操作的核心工具。集合类提供了多种数据结构来存储对象,并为我们提供了许多便利的方法,以提高程序的易用性和灵活性。

1. 集合框架概述

Java 集合框架包含了一组接口和类,它们可以对一组对象进行存储和操作。集合框架的核心包含:

  • 集合(Collection):根接口,用于存储一组对象。
  • 映射(Map):用于存储键值对。

1.1 集合的主要接口

  • List:有序集合,可以存储重复元素。
  • Set:不允许重复元素的集合。
  • Queue:用于存储排队的元素,遵循先进先出(FIFO)原则。
  • Deque:双端队列,允许在两端插入和删除元素。

1.2 映射的主要接口

  • Map:键值对集合,允许用键(Key)来查找值(Value)。
  • SortedMap:有序的 Map 接口。

2. 常用集合类

2.1 ArrayList

ArrayList 是用于存储可变数量的元素的列表。它实现了 List 接口,允许插入重复元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.ArrayList;

public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Banana"); // 允许重复

System.out.println("Fruits: " + fruits);
}
}

输出:

1
Fruits: [Apple, Banana, Cherry, Banana]

2.2 LinkedList

LinkedList 也是 List 接口的实现,适合频繁插入和删除操作的场景。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.LinkedList;

public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> cities = new LinkedList<>();
cities.add("New York");
cities.add("Los Angeles");
cities.add("Chicago");

cities.remove("Los Angeles"); // 删除操作

System.out.println("Cities: " + cities);
}
}

输出:

1
Cities: [New York, Chicago]

2.3 HashSet

HashSet 实现了 Set 接口,不允许重复元素,并且不保证存储顺序。

1
2
3
4
5
6
7
8
9
10
11
12
import java.util.HashSet;

public class HashSetExample {
public static void main(String[] args) {
HashSet<String> uniqueFruits = new HashSet<>();
uniqueFruits.add("Apple");
uniqueFruits.add("Banana");
uniqueFruits.add("Apple"); // 不会添加,因重复

System.out.println("Unique Fruits: " + uniqueFruits);
}
}

输出:

1
Unique Fruits: [Banana, Apple]

2.4 HashMap

HashMapMap 接口的一个实现,支持快速访问和插入,允许使用 null 作为键和/或值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.HashMap;

public class HashMapExample {
public static void main(String[] args) {
HashMap<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 85);
scores.put("Charlie", 92);

scores.put("Alice", 95); // 更新 Alice 的分数

System.out.println("Scores: " + scores);
}
}

输出:

1
Scores: {Alice=95, Bob=85, Charlie=92}

3. 遍历集合

遍历集合是操作集合中的元素的常见方法。不同的集合类可能有不同的遍历方式。

3.1 使用迭代器

对于 ArrayListHashSet,我们可以使用 Iterator 进行遍历。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.ArrayList;
import java.util.Iterator;

public class IteratorExample {
public static void main(String[] args) {
ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");

Iterator<String> iterator = colors.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}

输出:

1
2
3
Red
Green
Blue

3.2 使用增强的 for 循环

增强的 for 循环(也称为“for-each”循环)也可用于遍历 Iterable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.HashSet;

public class ForEachExample {
public static void main(String[] args) {
HashSet<String> animals = new HashSet<>();
animals.add("Dog");
animals.add("Cat");
animals.add("Rabbit");

for (String animal : animals) {
System.out.println(animal);
}
}
}

输出:

1
2
3
Dog
Cat
Rabbit

4. 结论

通过本章的学习,我们对 Java 的集合类有了初步的了解。不同的集合类适用于不同的场景,理解这些集合的特性将帮助我们在编写 Java 程序时选择合适的工具。

在下一章节中,我们将介绍 Java 的输入输出流,这对于处理数据的读取和写入是至关重要的。希望大家能继续保持学习的热情,与我一起深入 Java 的世界。

30 Java常用类之集合类

https://zglg.work/java-zero/30/

作者

IT教程网(郭震)

发布于

2024-08-10

更新于

2024-08-10

许可协议

分享转发

交流

更多教程加公众号

更多教程加公众号

加入星球获取PDF

加入星球获取PDF

打卡评论