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

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); } 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<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Orange"); fruits.add("Grapes"); System.out.println("原始列表:" + fruits);
String first = fruits.get(0); System.out.println("第一个水果是:" + first);
fruits.set(1, "Blueberry"); System.out.println("修改后的列表:" + fruits);
fruits.remove(2); System.out.println("删除后的列表:" + fruits);
fruits.remove("Apple"); System.out.println("删除 Apple 后:" + fruits);
boolean hasGrapes = fruits.contains("Grapes"); System.out.println("包含 Grapes?" + hasGrapes);
int size = fruits.size(); System.out.println("列表长度:" + size);
System.out.println("列表是否为空?" + fruits.isEmpty());
fruits.clear(); System.out.println("清空后的列表:" + fruits);
List<String> newFruits = List.of("Mango", "Peach", "Cherry"); fruits.addAll(newFruits); System.out.println("添加多个水果:" + fruits);
System.out.println("Peach 的位置:" + fruits.indexOf("Peach"));
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) { Set<String> set = new HashSet<>();
set.add("Apple"); set.add("Banana"); set.add("Orange"); set.add("Apple");
System.out.println("集合内容:" + set);
boolean hasApple = set.contains("Apple"); System.out.println("包含 Apple?" + hasApple);
set.remove("Orange"); System.out.println("删除 Orange 后:" + set);
System.out.println("集合大小:" + set.size());
System.out.println("集合是否为空?" + set.isEmpty());
set.clear(); System.out.println("清空后:" + set);
Set<String> newSet = Set.of("Mango", "Peach", "Cherry"); set.addAll(newSet); System.out.println("添加多个元素:" + set);
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) { Map<String, Integer> map = new HashMap<>();
map.put("Apple", 3); map.put("Banana", 2); map.put("Orange", 5); map.put("Apple", 4); System.out.println("原始 Map:" + map);
int appleCount = map.get("Apple"); System.out.println("Apple 数量:" + appleCount);
boolean hasBanana = map.containsKey("Banana"); System.out.println("包含 Banana?" + hasBanana);
boolean hasValue5 = map.containsValue(5); System.out.println("包含值 5?" + hasValue5);
map.remove("Banana"); System.out.println("移除 Banana 后:" + map);
System.out.println("Map 大小:" + map.size());
System.out.println("Map 是否为空?" + map.isEmpty());
System.out.println("所有 key:" + map.keySet());
System.out.println("所有 value:" + map.values());
|
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(); }
|