建设多用户网站,网站怎么做支付系统,WordPress更该主题,珠海品牌网站建Java集合类#xff1a;List、Set、Map常用方法解析 文章目录 Java集合类#xff1a;List、Set、Map常用方法解析一、List集合#xff1a;有序的元素集合1. ArrayList2. LinkedList 二、Set集合#xff1a;无序的不重复元素集合1. HashSet2. TreeSet 三、Map集合#xff1a…Java集合类List、Set、Map常用方法解析 文章目录 Java集合类List、Set、Map常用方法解析一、List集合有序的元素集合1. ArrayList2. LinkedList 二、Set集合无序的不重复元素集合1. HashSet2. TreeSet 三、Map集合键值对映射1. HashMap2. TreeMap 四、注意点 一、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) { ListString 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) { ListString 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) { SetString 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) { SetString set new TreeSet(); set.add(Banana); set.add(Apple); set.add(Cherry); // 遍历Set元素已按自然顺序排序 for (String fruit : set) { System.out.println(fruit); } // 自定义排序需要实现Comparator接口 SetString 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) { MapString, Integer map new HashMap(); map.put(Apple, 1); map.put(Banana, 2); map.put(Cherry, 3); // 遍历Map for (Map.EntryString, 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) { MapString, Integer map new TreeMap(); map.put(Banana, 2); map.put(Apple, 1); map.put(Cherry, 3); // 遍历TreeMap元素已按键的自然顺序排序 for (Map.EntryString, Integer entry : map.entrySet()) { System.out.println(Key: entry.getKey() , Value: entry.getValue()); } // 自定义排序需要实现Comparator接口 MapString, Integer customMap new TreeMap((o1, o2) - o2.compareTo(o1)); // 降序排序 customMap.put(Banana, 2); customMap.put(Apple, 1); for (Map.EntryString, Integer entry : customMap.entrySet()) { System.out.println(Key: entry.getKey() , Value: entry.getValue()); } }
}四、注意点
修改List和SetList和Set的修改操作如add、remove等可能会影响到正在进行的遍历因此在遍历过程中修改集合可能会导致ConcurrentModificationException异常。为了避免这个问题可以使用迭代器Iterator的remove方法或者在遍历之前先收集需要修改的元素遍历结束后再进行修改。HashMap的null键和值HashMap允许使用null作为键和值但最多只能有一个null键。而TreeMap不允许使用null键或值。性能考虑不同的集合类在性能上有所差异。例如ArrayList在查找上效率较高但在插入和删除上可能较低而LinkedList在插入和删除上效率较高但在查找上可能较低。因此在选择集合类时需要根据实际使用场景进行权衡。这个主要跟两者之间的底层数据结构有关。线程安全上述提到的集合类如ArrayList、HashSet、HashMap等都不是线程安全的。如果在多线程环境下使用这些集合类并对其进行修改操作可能会导致数据不一致。这时可以使用线程安全的集合类如Collections的同步包装器或ConcurrentHashMap等或者使用锁机制来保证线程安全。