Java集合类:List、Set、Map常用方法解析
文章目录
一、List集合:有序的元素集合
List集合是有序的元素集合,允许存储重复的元素。常见的List实现类有ArrayList、LinkedList等。
1. ArrayList
ArrayList是最常用的List实现类,它底层使用数组来存储元素,因此查找元素的速度较快,但插入和删除元素时可能需要移动其他元素,所以速度相对较慢。
import java.util.ArrayList;
import java.util.List;
public class ArrayListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
// 遍历List
for (String fruit : list) {
System.out.println(fruit);
}
// 使用索引访问和修改元素
System.out.println("Element at index 1: " + list.get(1));
list.set(1, "Blueberry"); // 将索引为1的元素替换为"Blueberry"
}
}
2. LinkedList
LinkedList底层使用链表实现,因此在元素的插入和删除操作上效率较高,但在查找元素时可能需要遍历整个链表,所以速度较慢。
import java.util.LinkedList;
import java.util.List;
public class LinkedListDemo {
public static void main(String[] args) {
List<String> list = new LinkedList<>();
list.add("Apple");
list.addFirst("Orange"); // 在链表头部添加元素
list.addLast("Watermelon"); // 在链表尾部添加元素
// 遍历List
for (String fruit : list) {
System.out.println(fruit);
}
// 使用索引访问元素(注意LinkedList的get和set方法性能较低)
System.out.println("Element at index 1: " + list.get(1));
// 移除元素
list.remove("Apple");
}
}
二、Set集合:无序的不重复元素集合
Set集合不允许存储重复的元素。常见的Set实现类有HashSet、TreeSet等。
1. HashSet
HashSet基于哈希表实现,元素是无序的,且元素存储的顺序与插入顺序无关,查找效率高。
import java.util.HashSet;
import java.util.Set;
public class HashSetDemo {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // 重复元素不会被添加
// 遍历Set
for (String fruit : set) {
System.out.println(fruit);
}
}
}
2. TreeSet
TreeSet基于红黑树实现,可以对元素进行自然排序或自定义排序。
import java.util.Set;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
Set<String> set = new TreeSet<>();
set.add("Banana");
set.add("Apple");
set.add("Cherry");
// 遍历Set(元素已按自然顺序排序)
for (String fruit : set) {
System.out.println(fruit);
}
// 自定义排序,需要实现Comparator接口
Set<String> customSet = new TreeSet<>((o1, o2) -> o2.compareTo(o1)); // 降序排序
customSet.add("Banana");
customSet.add("Apple");
for (String fruit : customSet) {
System.out.println(fruit);
}
}
}
三、Map集合:键值对映射
Map集合用于存储键值对映射关系,键(Key)是唯一的,每个键最多映射到一个值(Value)。常见的Map实现类有HashMap、TreeMap等。
1. HashMap
HashMap基于哈希表实现,允许使用null作为键和值,并且不保证映射的顺序。HashMap在存储键值对时,通过计算键的哈希值来确定存储位置,因此查找效率非常高。
import java.util.HashMap;
import java.util.Map;
public class HashMapDemo {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);
// 遍历Map
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
// 获取和修改值
System.out.println("Value for Apple: " + map.get("Apple"));
map.put("Apple", 4); // 修改键为"Apple"的值
// 检查键是否存在
if (map.containsKey("Orange")) {
System.out.println("Orange exists in the map.");
} else {
System.out.println("Orange does not exist in the map.");
}
// 移除键值对
map.remove("Banana");
}
}
2. TreeMap
TreeMap基于红黑树实现,它可以对键进行自然排序或自定义排序,因此返回的元素是排序好的。
import java.util.Map;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
Map<String, Integer> map = new TreeMap<>();
map.put("Banana", 2);
map.put("Apple", 1);
map.put("Cherry", 3);
// 遍历TreeMap(元素已按键的自然顺序排序)
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
// 自定义排序,需要实现Comparator接口
Map<String, Integer> customMap = new TreeMap<>((o1, o2) -> o2.compareTo(o1)); // 降序排序
customMap.put("Banana", 2);
customMap.put("Apple", 1);
for (Map.Entry<String, Integer> entry : customMap.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
}
}
四、注意点
- 修改List和Set:List和Set的修改操作(如add、remove等)可能会影响到正在进行的遍历,因此在遍历过程中修改集合可能会导致ConcurrentModificationException异常。为了避免这个问题,可以使用迭代器(Iterator)的remove方法,或者在遍历之前先收集需要修改的元素,遍历结束后再进行修改。
- HashMap的null键和值:HashMap允许使用null作为键和值,但最多只能有一个null键。而TreeMap不允许使用null键或值。
- 性能考虑:不同的集合类在性能上有所差异。例如,ArrayList在查找上效率较高,但在插入和删除上可能较低;而LinkedList在插入和删除上效率较高,但在查找上可能较低。因此,在选择集合类时,需要根据实际使用场景进行权衡。这个主要跟两者之间的底层数据结构有关。
- 线程安全:上述提到的集合类(如ArrayList、HashSet、HashMap等)都不是线程安全的。如果在多线程环境下使用这些集合类,并对其进行修改操作,可能会导致数据不一致。这时,可以使用线程安全的集合类(如Collections的同步包装器或ConcurrentHashMap等),或者使用锁机制来保证线程安全。