引言
集合框架是编程中一个非常重要的概念,它提供了丰富的数据结构来帮助我们高效地处理数据。在这个文章中,我将通过20个实用的代码实例,手把手教你如何轻松掌握集合框架。无论是初学者还是有一定基础的程序员,都能从中受益。
实例1:使用List存储和访问元素
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list.get(0)); // 输出: Apple
在这个例子中,我们创建了一个ArrayList来存储字符串,并展示了如何添加和访问元素。
实例2:使用Set去除重复元素
Set<String> set = new HashSet<>(Arrays.asList("Apple", "Banana", "Apple", "Cherry"));
System.out.println(set); // 输出: [Apple, Banana, Cherry]
在这个例子中,我们使用HashSet来存储字符串,自动去除重复元素。
实例3:使用Map存储键值对
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);
System.out.println(map.get("Apple")); // 输出: 1
在这个例子中,我们使用HashMap来存储字符串和整数的键值对。
实例4:使用LinkedList实现动态数组
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Apple");
linkedList.add("Banana");
linkedList.add("Cherry");
System.out.println(linkedList.get(0)); // 输出: Apple
在这个例子中,我们使用LinkedList来实现动态数组。
实例5:使用Queue实现先进先出
Queue<String> queue = new LinkedList<>();
queue.add("Apple");
queue.add("Banana");
queue.add("Cherry");
System.out.println(queue.poll()); // 输出: Apple
在这个例子中,我们使用LinkedList实现了一个队列,展示了如何实现先进先出的操作。
实例6:使用Stack实现后进先出
Stack<String> stack = new Stack<>();
stack.push("Apple");
stack.push("Banana");
stack.push("Cherry");
System.out.println(stack.pop()); // 输出: Cherry
在这个例子中,我们使用Stack实现了一个栈,展示了如何实现后进先出的操作。
实例7:使用PriorityQueue实现优先级队列
PriorityQueue<String> priorityQueue = new PriorityQueue<>();
priorityQueue.add("Apple");
priorityQueue.add("Banana");
priorityQueue.add("Cherry");
System.out.println(priorityQueue.poll()); // 输出: Apple
在这个例子中,我们使用PriorityQueue实现了一个优先级队列。
实例8:使用HashSet进行快速查找
Set<String> set = new HashSet<>(Arrays.asList("Apple", "Banana", "Cherry"));
boolean contains = set.contains("Banana");
System.out.println(contains); // 输出: true
在这个例子中,我们使用HashSet进行快速查找,展示了其高效的查找性能。
实例9:使用HashMap进行快速键值对查找
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);
boolean containsKey = map.containsKey("Banana");
System.out.println(containsKey); // 输出: true
在这个例子中,我们使用HashMap进行快速键值对查找,展示了其高效的查找性能。
实例10:使用LinkedList实现插入和删除操作
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Apple");
linkedList.add("Banana");
linkedList.add("Cherry");
linkedList.add(1, "Orange");
System.out.println(linkedList); // 输出: [Apple, Orange, Banana, Cherry]
linkedList.remove(2);
System.out.println(linkedList); // 输出: [Apple, Orange, Banana]
在这个例子中,我们使用LinkedList实现插入和删除操作。
实例11:使用ArrayList进行随机访问
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Apple");
arrayList.add("Banana");
arrayList.add("Cherry");
System.out.println(arrayList.get(1)); // 输出: Banana
在这个例子中,我们使用ArrayList进行随机访问。
实例12:使用HashSet进行去重操作
Set<String> set = new HashSet<>(Arrays.asList("Apple", "Banana", "Apple", "Cherry"));
System.out.println(set); // 输出: [Apple, Banana, Cherry]
在这个例子中,我们使用HashSet进行去重操作。
实例13:使用HashMap进行键值对存储
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);
System.out.println(map); // 输出: {Apple=1, Banana=2, Cherry=3}
在这个例子中,我们使用HashMap进行键值对存储。
实例14:使用LinkedList进行插入和删除操作
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Apple");
linkedList.add("Banana");
linkedList.add("Cherry");
linkedList.addFirst("Orange");
System.out.println(linkedList); // 输出: [Orange, Apple, Banana, Cherry]
linkedList.removeLast();
System.out.println(linkedList); // 输出: [Orange, Apple, Banana]
在这个例子中,我们使用LinkedList进行插入和删除操作。
实例15:使用Queue实现队列操作
Queue<String> queue = new LinkedList<>();
queue.add("Apple");
queue.add("Banana");
queue.add("Cherry");
System.out.println(queue.poll()); // 输出: Apple
System.out.println(queue.peek()); // 输出: Banana
在这个例子中,我们使用Queue实现队列操作。
实例16:使用Stack实现栈操作
Stack<String> stack = new Stack<>();
stack.push("Apple");
stack.push("Banana");
stack.push("Cherry");
System.out.println(stack.pop()); // 输出: Cherry
System.out.println(stack.peek()); // 输出: Banana
在这个例子中,我们使用Stack实现栈操作。
实例17:使用PriorityQueue实现优先级队列操作
PriorityQueue<String> priorityQueue = new PriorityQueue<>();
priorityQueue.add("Apple");
priorityQueue.add("Banana");
priorityQueue.add("Cherry");
System.out.println(priorityQueue.poll()); // 输出: Apple
System.out.println(priorityQueue.peek()); // 输出: Banana
在这个例子中,我们使用PriorityQueue实现优先级队列操作。
实例18:使用HashSet进行集合操作
Set<String> set1 = new HashSet<>(Arrays.asList("Apple", "Banana"));
Set<String> set2 = new HashSet<>(Arrays.asList("Banana", "Cherry"));
Set<String> union = new HashSet<>(set1);
union.addAll(set2);
Set<String> intersection = new HashSet<>(set1);
intersection.retainAll(set2);
Set<String> difference = new HashSet<>(set1);
difference.removeAll(set2);
System.out.println(union); // 输出: [Apple, Banana, Cherry]
System.out.println(intersection); // 输出: [Banana]
System.out.println(difference); // 输出: [Apple]
在这个例子中,我们使用HashSet进行集合操作,包括并集、交集和差集。
实例19:使用HashMap进行键值对操作
Map<String, Integer> map1 = new HashMap<>();
map1.put("Apple", 1);
map1.put("Banana", 2);
Map<String, Integer> map2 = new HashMap<>();
map2.put("Banana", 3);
map2.put("Cherry", 4);
Map<String, Integer> putAll = new HashMap<>(map1);
putAll.putAll(map2);
Map<String, Integer> merge = new HashMap<>(map1);
merge.merge("Banana", 3, Integer::sum);
Map<String, Integer> replace = new HashMap<>(map1);
replace.replace("Apple", 5);
System.out.println(putAll); // 输出: {Apple=1, Banana=3, Cherry=4}
System.out.println(merge); // 输出: {Apple=1, Banana=5, Cherry=4}
System.out.println(replace); // 输出: {Apple=5, Banana=3, Cherry=4}
在这个例子中,我们使用HashMap进行键值对操作,包括合并、合并时计算值和替换键值。
实例20:使用集合框架进行排序
List<String> list = new ArrayList<>(Arrays.asList("Apple", "Banana", "Cherry"));
Collections.sort(list);
System.out.println(list); // 输出: [Apple, Banana, Cherry]
在这个例子中,我们使用集合框架进行排序。
总结
通过以上20个实用代码实例,相信你已经对集合框架有了更深入的了解。在实际编程中,合理运用集合框架可以让我们更加高效地处理数据。希望这篇文章能帮助你轻松掌握集合框架,祝你编程愉快!
