Logo
Section 1 / 11

集合入门

探索 Java 集合框架,使用统一的接口来存储、组织和管理对象组。

导入

集合入门

探索 Java 集合框架,使用统一的接口来存储、组织和管理对象组。

代码示例

1Iterable <Interface> 2 └── Collection <Interface> 3 ├── List <Interface> (有序,允许重复) 4 ├── Set <Interface> (无序,元素唯一) 5 └── Queue <Interface> (处理顺序,例如 FIFO) 6 7Map <Interface> (键值对的独立继承体系)
1import java.util.ArrayList; 2import java.util.Collection; 3 4public class CollectionExample { 5 public static void main(String[] args) { 6 // <String> 是泛型。它告诉编译器此集合 7 // 只能持有 String 类型。这确保了类型安全。 8 Collection supplies = new ArrayList<>(); 9 10 supplies.add("笔记本"); 11 supplies.add("笔"); 12 13 // 接口保证这些方法存在 14 System.out.println("大小: " + supplies.size()); 15 System.out.println("包含笔吗? " + supplies.contains("笔")); 16 } 17}
1import java.util.ArrayList; 2import java.util.HashSet; 3import java.util.List; 4import java.util.Set; 5 6public class ListVsSet { 7 public static void main(String[] args) { 8 // List: 有序,允许重复 9 List<String> shoppingList = new ArrayList<>(); 10 shoppingList.add("牛奶"); 11 shoppingList.add("面包"); 12 shoppingList.add("牛奶"); // 允许重复 13 14 System.out.println("List: " + shoppingList); 15 16 // Set: 无序,元素唯一 17 Set<String> uniqueItems = new HashSet<>(); 18 uniqueItems.add("牛奶"); 19 uniqueItems.add("面包"); 20 uniqueItems.add("牛奶"); // 重复项被忽略 21 22 System.out.println("Set: " + uniqueItems); 23 } 24}
1import java.util.ArrayDeque; 2import java.util.Deque; 3import java.util.LinkedList; 4import java.util.Queue; 5 6public class QueueAndDeque { 7 public static void main(String[] args) { 8 // Queue: 先进先出 (FIFO) 9 // LinkedList 实现了 Queue,因此可以在这里使用 10 Queue<String> printJobs = new LinkedList<>(); 11 12 // add() 将元素添加到队列末尾 13 printJobs.add("报告.pdf"); 14 printJobs.add("照片.png"); 15 16 // poll() 移除队列头部(第一个项目) 17 String currentJob = printJobs.poll(); 18 System.out.println("正在打印: " + currentJob); 19 System.out.println("下一个任务: " + printJobs.peek()); 20 21 // Deque: 后进先出栈行为 (LIFO) 22 // ArrayDeque 是高效的 Deque 实现 23 Deque<String> browserHistory = new ArrayDeque<>(); 24 25 // push() 将元素添加到前端(栈顶) 26 browserHistory.push("主页"); 27 browserHistory.push("个人资料页"); 28 29 // pop() 移除前端元素(最近添加的) 30 System.out.println("返回: " + browserHistory.pop()); 31 System.out.println("当前页面: " + browserHistory.peek()); 32 } 33}
1import java.util.HashMap; 2import java.util.Map; 3 4public class MapExample { 5 public static void main(String[] args) { 6 // Map: 存储键 -> 值 对 7 Map<String, Integer> scores = new HashMap<>(); 8 9 // 关联键与值 10 scores.put("Alice", 90); 11 scores.put("Bob", 85); 12 13 // 更新值(Alice 的键已存在) 14 scores.put("Alice", 95); 15 16 // 获取值 17 System.out.println("Alice 的分数: " + scores.get("Alice")); 18 System.out.println("Charlie 的分数: " + scores.get("Charlie")); // null 19 20 System.out.println("总条目数: " + scores.size()); 21 } 22}
1import java.util.ArrayList; 2import java.util.List; 3 4public class GenericsExample { 5 public static void main(String[] args) { 6 // 类型安全的列表:仅允许字符串 7 List<String> userNames = new ArrayList<>(); 8 9 userNames.add("Sarah"); 10 // userNames.add(101); // 编译错误:类型不兼容 11 12 String name = userNames.get(0); // 无需强制转换 13 System.out.println("用户: " + name); 14 } 15}

继承与多态

使用 ArrayList、HashSet 和 HashMap