Java编程 III 集合

集合

集合是java中用来持有对象的一种”容器“。它具体表现为实现了两个基本接口

image

collection

一个由单独元素组成的序列,而这些元素需要符合一条或多条规则

List : 必须按照元素的插入顺序来进行保存

Set : 中不能存在重复元素

Queue : 按照排队顺序输出元素

map

一组键值对象,通过键来查找值。map通过一个对象来查找另一个对象

向集合中添加元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

import java.util.*;

public class AddingGroups{
public static void main(String[] args){
Collection<Integer> collection = new ArrayList<>(Arrays.asList(1,2,3,4,5));
Integer[] moreInts = {6,7,8,9,10};
collection.addAll(Arrays.asList(moreInts));
for(Integer i : collection){
System.out.println(i);
}
// Ragged array
List<Integer> list = Arrays.asList(moreInts);
list.set(0, 99);
System.out.println(list.get(0));
}
}

打印集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.* 

public class PrintingCollections{
static Collection fill(Collection< String > collection){
collection.add("rat");
collection.add("cat");
collection.add("dog");
collection.add("dog");
return collection;
}

static Map fill(Map<String> map){
map.put("rat","Fuzzy");
map.put("cat","Rats");
map.put("dog","Bosco");
map.put("dog","Spot");
return map ;
}
public static void main(String[] args){
System.out.println(fill(new ArrayList<>()));
System.out.println(fill(new LinkedList<>()));
System.out.println(fill(new Hashset<>()));
System.out.println(fill(new TreeSet<>()));
System.out.println(fill(new LinkedHashSet<>()));
System.out.println(fill(new HashMap<>()));
System.out.println(fill(new TreeMap<>()));
System.out.println(fill(new LinkedHashMap()));
}
}

List

list的创建和常用函数 (以arraylist为例)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    import java.util.ArrayList;
import java.util.List;

public class ListExample {
public static void main(String[] args) {
// 创建一个 List,使用 ArrayList 实现类
List<String> fruits = new ArrayList<>();
// 1. add() 添加元素
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Grapes");
System.out.println("原始列表:" + fruits); // [Apple, Banana, Orange, Grapes]

// 2. get(int index) 获取指定位置的元素
String first = fruits.get(0);
System.out.println("第一个水果是:" + first); // Apple

// 3. set(int index, E element) 替换指定位置的元素
fruits.set(1, "Blueberry"); // 替换 Banana
System.out.println("修改后的列表:" + fruits); // [Apple, Blueberry, Orange, Grapes]

// 4. remove(int index) 根据下标删除
fruits.remove(2); // 删除 Orange
System.out.println("删除后的列表:" + fruits); // [Apple, Blueberry, Grapes]

// 5. remove(Object o) 根据对象删除
fruits.remove("Apple");
System.out.println("删除 Apple 后:" + fruits); // [Blueberry, Grapes]

// 6. contains(Object o) 判断是否包含某个元素
boolean hasGrapes = fruits.contains("Grapes");
System.out.println("包含 Grapes?" + hasGrapes); // true

// 7. size() 返回元素个数
int size = fruits.size();
System.out.println("列表长度:" + size); // 2

// 8. isEmpty() 判断是否为空
System.out.println("列表是否为空?" + fruits.isEmpty()); // false

// 9. clear() 清空列表
fruits.clear();
System.out.println("清空后的列表:" + fruits); // []

// 10. addAll(Collection<? extends E> c) 添加一整个集合
List<String> newFruits = List.of("Mango", "Peach", "Cherry");
fruits.addAll(newFruits);
System.out.println("添加多个水果:" + fruits); // [Mango, Peach, Cherry]

// 11. indexOf(Object o) 获取元素第一次出现的位置
System.out.println("Peach 的位置:" + fruits.indexOf("Peach")); // 1

// 12. for-each 遍历
for (String fruit : fruits) {
System.out.println("水果:" + fruit);
}
}

Set

set 中不允许出现重复的对象值,

常用方法示例(以hashset为例)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.HashSet;
import java.util.Set;

public class SetExample {
public static void main(String[] args) {
// 创建一个 HashSet(最常用的 Set 实现)
Set<String> set = new HashSet<>();

// 1. add(E e) 添加元素(不会添加重复元素)
set.add("Apple");
set.add("Banana");
set.add("Orange");
set.add("Apple"); // 重复添加无效

System.out.println("集合内容:" + set); // [Banana, Apple, Orange](无序且不重复)

// 2. contains(Object o) 判断是否包含某个元素
boolean hasApple = set.contains("Apple");
System.out.println("包含 Apple?" + hasApple); // true

// 3. remove(Object o) 删除元素
set.remove("Orange");
System.out.println("删除 Orange 后:" + set); // [Banana, Apple]

// 4. size() 获取集合中元素个数
System.out.println("集合大小:" + set.size()); // 2

// 5. isEmpty() 判断集合是否为空
System.out.println("集合是否为空?" + set.isEmpty()); // false

// 6. clear() 清空集合
set.clear();
System.out.println("清空后:" + set); // []

// 7. addAll(Collection<? extends E> c) 添加一整个集合
Set<String> newSet = Set.of("Mango", "Peach", "Cherry");
set.addAll(newSet);
System.out.println("添加多个元素:" + set); // [Mango, Peach, Cherry]

// 8. 遍历 Set(使用增强 for 循环)
for (String fruit : set) {
System.out.println("遍历元素:" + fruit);
}
}
}

Map

常用的map 方法(以hashmap为例)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.HashMap;
import java.util.Map;

public class MapExample {
public static void main(String[] args) {
// 创建一个 HashMap(最常用的 Map 实现)
Map<String, Integer> map = new HashMap<>();

// 1. put(K key, V value) 添加键值对
map.put("Apple", 3);
map.put("Banana", 2);
map.put("Orange", 5);
map.put("Apple", 4); // 重复 key,会覆盖旧值
System.out.println("原始 Map:" + map); // {Banana=2, Apple=4, Orange=5}

// 2. get(Object key) 根据 key 获取 value
int appleCount = map.get("Apple");
System.out.println("Apple 数量:" + appleCount); // 4

// 3. containsKey(Object key) 是否包含某个 key
boolean hasBanana = map.containsKey("Banana");
System.out.println("包含 Banana?" + hasBanana); // true

// 4. containsValue(Object value) 是否包含某个 value
boolean hasValue5 = map.containsValue(5);
System.out.println("包含值 5?" + hasValue5); // true

// 5. remove(Object key) 移除指定 key 对应的映射
map.remove("Banana");
System.out.println("移除 Banana 后:" + map); // {Apple=4, Orange=5}

// 6. size() 返回映射关系数量
System.out.println("Map 大小:" + map.size()); // 2

// 7. isEmpty() 是否为空
System.out.println("Map 是否为空?" + map.isEmpty()); // false

// 8. keySet() 获取所有 key 组成的 Set
System.out.println("所有 key:" + map.keySet()); // [Apple, Orange]

// 9. values() 获取所有 value 组成的 Collection
System.out.println("所有 value:" + map.values()); // [4,

Collection类的作用

在 C/C++ 语言中,程序员常常需要手动管理内存,并通过指针操作实现数据结构(如数组、链表、哈希表等)的动态存储与访问。Java 中的 Collection 框架正是对这些底层容器机制的抽象与封装,提供了更安全、可扩展、面向对象的容器解决方案。

Collection 的本质与作用

在面向对象编程中,虽然一切皆对象,但对象之间的组织、管理与批量操作仍需借助容器结构。Java 使用 Collection 接口抽象了这类 “容器型对象” 的基本行为规范,定义了数据的添加、删除、遍历、查询等操作方式,类似于标准容器库(STL)在 C++ 中的角色。

底层结构与三大经典实现

Java 中常见的容器实现,底层源于三种经典数据结构,每种都针对特定的操作场景做出了效率取舍:

实现方式 底层结构 优势 劣势
Array 数组 随机访问快(O(1)) 插入/删除效率低,扩容成本高
Linked 链表 插入/删除快(O(1)) 查询慢(O(n))
Hash 哈希表 快速定位(O(1) 近似) 不支持顺序,可能存在哈希冲突
  • 数组(Array):通过“地址 + 偏移”的方式实现快速定位,适用于频繁查询但不常变动的场景。
  • 链表(Linked List):适合频繁插入/删除,但不适用于大规模随机访问。
  • 哈希表(Hash Table):通过哈希函数定位存储位置,实现高效的键值映射查询。

Lambda表达式

一种函数式的执行方式,在java中 因为java定义的接口是要求执行一个方法,所以可以通过这种方式,直接定义符合该接口目的的方法作为一个待执行的“类”交给应用去调用执行。本质是编译器帮你自动创建了一个实现了某个函数式接口的匿名对象,并将其赋值给一个接口类型的引用变量,你再通过这个引用去调用它的方法

使用方法 : 定义一个只有一个函数签名的接口,在调用时直接声明基于该接口的引用 然后

() → 重写接口定义的方法

1
2
3
4
5
6
7
8
9
10
11

public class LambdaFeastures{
public static void main(String[] args) {
sayHi hi = () -> System.out.println("hi");
hi.hi();
}
}

interface sayHi{
void hi();
}

对应的 匿名类实现 :

1
2
3
4
5
6
7
8
9
10
11
12
13
public class LambdaFeastures{
public static void main(String[] args){
sayHi hi = new sayHi() {
@Override
public void hi() {
System.out.println("hi");
}
}
}
}
interface sayHi{
void hi();
}

Java编程 III 集合
http://gadoid.io/2025/04/22/Java编程-III-集合/
作者
Codfish
发布于
2025年4月22日
许可协议