目录
List(列表):
ArrayList
LinkedList
ArrayList和LinkedList的关系
Set(集合):
HashSet
TreeSet
HashSet和TreeSet的关系和区别
Map(映射):
HashMap
TreeMap
List、Set、Map这三个大类的关系和区别
当涉及到数据集合时,List、Set和Map是常用的数据结构。它们之间有一些相似之处,同时也有一些区别。
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("Alice");
list.add("Bob");
list.add("Charlie");
list.add("David");
System.out.println("List size: " + list.size());
for(String name : list) {
System.out.println(name);
}
}
}
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add("Alice");
list.add("Bob");
list.add("Charlie");
list.add("David");
System.out.println("List size: " + list.size());
for(String name : list) {
System.out.println(name);
}
}
}
// HashSet 示例
HashSet hashSet = new HashSet<>();
// 添加元素
hashSet.add("Apple");
hashSet.add("Banana");
hashSet.add("Orange");
hashSet.add("Mango");
hashSet.add("Apple"); // 重复元素不会被添加
// 输出 HashSet
System.out.println("HashSet: " + hashSet);
// 输出结果为:HashSet: [Orange, Banana, Apple, Mango]
// 元素的顺序是不确定的
// 判断元素是否存在
boolean containsApple = hashSet.contains("Apple");
System.out.println("HashSet contains \"Apple\": " + containsApple);
// 输出结果为:HashSet contains "Apple": true
// 删除元素
hashSet.remove("Banana");
System.out.println("HashSet after removing \"Banana\": " + hashSet);
// 输出结果为:HashSet after removing "Banana": [Orange, Apple, Mango]
// 判断 HashSet 是否为空
boolean isEmpty = hashSet.isEmpty();
System.out.println("Is HashSet empty? " + isEmpty);
// 输出结果为:Is HashSet empty? false
// 获取 HashSet 的大小
int size = hashSet.size();
System.out.println("HashSet size: " + size);
// 输出结果为:HashSet size: 3
// TreeSet 示例
TreeSet treeSet = new TreeSet<>();
// 添加元素
treeSet.add("Apple");
treeSet.add("Banana");
treeSet.add("Orange");
treeSet.add("Mango");
treeSet.add("Apple"); // 重复元素不会被添加
// 输出 TreeSet
System.out.println("TreeSet: " + treeSet);
// 输出结果为:TreeSet: [Apple, Banana, Mango, Orange]
// 元素按照字母顺序进行排序存储
// 获取第一个元素
String firstElement = treeSet.first();
System.out.println("First element in TreeSet: " + firstElement);
// 输出结果为:First element in TreeSet: Apple
// 获取最后一个元素
String lastElement = treeSet.last();
System.out.println("Last element in TreeSet: " + lastElement);
// 输出结果为:Last element in TreeSet: Orange
// 获取小于某个元素的最大元素
String lowerElement = treeSet.lower("Banana");
System.out.println("Element lower than \"Banana\": " + lowerElement);
// 输出结果为:Element lower than "Banana": Apple
// 获取大于某个元素的最小元素
String higherElement = treeSet.higher("Banana");
System.out.println("Element higher than \"Banana\": " + higherElement);
// 输出结果为:Element higher than "Banana": Mango
// 删除元素
treeSet.remove("Banana");
System.out.println("TreeSet after removing \"Banana\": " + treeSet);
// 输出结果为:TreeSet after removing "Banana": [Apple, Mango, Orange]
// 清空 TreeSet
treeSet.clear();
System.out.println("TreeSet after clearing: " + treeSet);
// 输出结果为:TreeSet after clearing: []
HashMap 是基于哈希表实现的,它使用键的哈希值来进行存储和检索操作。它的特点是存储顺序不确定,不保证元素的顺序。HashMap 允许使用 null 作为键和值,并且允许存在重复的值,但不允许存在重复的键。
基本使用示例:
// HashMap 示例
HashMap hashMap = new HashMap<>();
// 添加键值对
hashMap.put(3, "Apple");
hashMap.put(1, "Banana");
hashMap.put(2, "Orange");
hashMap.put(4, "Mango");
hashMap.put(1, "Grape"); // 重复的键会覆盖旧的值
// 输出 HashMap
System.out.println("HashMap: " + hashMap);
// 输出结果为:HashMap: {1=Grape, 2=Orange, 3=Apple, 4=Mango}
// 元素的顺序是不确定的
// 获取值
String value = hashMap.get(3);
System.out.println("Value for key 3: " + value);
// 输出结果为:Value for key 3: Apple
// 判断是否包含键
boolean containsKey = hashMap.containsKey(2);
System.out.println("HashMap contains key 2: " + containsKey);
// 输出结果为:HashMap contains key 2: true
// 删除键值对
hashMap.remove(1);
System.out.println("HashMap after removing key 1: " + hashMap);
// 输出结果为:HashMap after removing key 1: {2=Orange, 3=Apple, 4=Mango}
// 判断 HashMap 是否为空
boolean isEmpty = hashMap.isEmpty();
System.out.println("Is HashMap empty? " + isEmpty);
// 输出结果为:Is HashMap empty? false
// 获取 HashMap 的大小
int size = hashMap.size();
System.out.println("HashMap size: " + size);
// 输出结果为:HashMap size: 3
TreeMap 是基于红黑树实现的,它会根据键的自然顺序或者自定义比较器的顺序来进行存储和检索操作。TreeMap 内部会对键进行排序,并且提供了按照键的自然顺序或者自定义比较器顺序的迭代器。TreeMap 不允许使用 null 作为键,但可以使用 null 作为值。TreeMap 中的键是有序的,因此迭代时会按照键的顺序进行遍历。
使用的简单示例:
// TreeMap 示例
TreeMap treeMap = new TreeMap<>();
// 添加键值对
treeMap.put(3, "Apple");
treeMap.put(1, "Banana");
treeMap.put(2, "Orange");
treeMap.put(4, "Mango");
treeMap.put(1, "Grape"); // 重复的键会覆盖旧的值
// 输出 TreeMap
System.out.println("TreeMap: " + treeMap);
// 输出结果为:TreeMap: {1=Grape, 2=Orange, 3=Apple, 4=Mango}
// 元素按照键的自然顺序进行排序存储
// 获取第一个键值对
Map.Entry firstEntry = treeMap.firstEntry();
System.out.println("First entry in TreeMap: " + firstEntry);
// 输出结果为:First entry in TreeMap: 1=Grape
// 获取最后一个键值对
Map.Entry lastEntry = treeMap.lastEntry();
System.out.println("Last entry in TreeMap: " + lastEntry);
// 输出结果为:Last entry in TreeMap: 4=Mango
// 获取小于某个键的最大键值对
Map.Entry lowerEntry = treeMap.lowerEntry(3);
System.out.println("Entry with key lower than 3: " + lowerEntry);
// 输出结果为:Entry with key lower than 3: 2=Orange
// 获取大于某个键的最小键值对
Map.Entry higherEntry = treeMap.higherEntry(3);
System.out.println("Entry with key higher than 3: " + higherEntry);
// 输出结果为:Entry with key higher than 3: 4=Mango
// 删除键值对
treeMap.remove(1);
System.out.println("TreeMap after removing key 1: " + treeMap);
// 输出结果为:TreeMap after removing key 1: {2=Orange, 3=Apple, 4=Mango}
// 清空 TreeMap
treeMap.clear();
System.out.println("TreeMap after clearing: " + treeMap);
// 输出结果为:TreeMap after clearing: {}