【Java入门|集合全解析:List、Set与Map详解】
Java集合Java集合分为单列集合和双列集合也就是 Collection 和 Map 。顾名思义 Collection 一个位置上仅存放一个元素 Map 一个位置上有两个元素分为键和值。 Map 和 Collection 下又分别衍生出多种集合种类下面笔者将一一介绍。一、Collection单列集合体系Collection 是单列集合的根接口它定义了所有单列集合的通用方法比如 add() 、 remove() 、 contains() 、 size() 等。它主要有两个子接口 List 和 Set 二者的核心区别在于 List 是有序、可重复的集合 Set 是无序、不可重复的集合。1. List 子接口List 代表有序、可重复的集合集合中的元素有明确的索引支持通过索引快速访问并且可以存储重复元素。- ArrayList基于动态数组实现是最常用的List实现类。它的查询效率极高通过索引直接定位但在中间插入、删除元素时需要移动后续元素效率较低线程不安全适合读多写少的场景。import java.util.ArrayList; import java.util.List; public class ArrayListDemo { public static void main(String[] args) { // 初始化ArrayList ListString list new ArrayList(); // 添加元素 list.add(Java); list.add(Python); list.add(Java); // 允许重复 // 根据索引获取元素 System.out.println(list.get(0)); // 输出: Java // 删除元素 list.remove(Python); // 遍历集合 for (String s : list) { System.out.println(s); // 输出: Java、Java } // 获取集合大小 System.out.println(集合大小 list.size()); // 输出: 2 } }- LinkedList基于双向链表实现在集合的首尾进行插入、删除操作效率极高但查询元素需要遍历链表效率较低同样线程不安全适合写多读少的场景同时还实现了 Deque 接口可以作为栈、队列、双端队列使用。import java.util.LinkedList; import java.util.List; import java.util.Queue; public class LinkedListDemo { public static void main(String[] args) { // 作为List使用 ListString list new LinkedList(); list.add(A); list.add(B); // 作为队列使用FIFO QueueString queue new LinkedList(); queue.offer(C); queue.offer(D); System.out.println(queue.poll()); // 输出: C出队 // 作为栈使用LIFO LinkedListString stack new LinkedList(); stack.push(E); stack.push(F); System.out.println(stack.pop()); // 输出: F出栈 } }- Vector早期的List实现类基于动态数组和ArrayList类似但它的方法都加了 synchronized 修饰线程安全但性能远低于ArrayList现在基本不再使用。import java.util.Vector; public class VectorDemo { public static void main(String[] args) { VectorInteger vector new Vector(); vector.add(1); vector.add(2); System.out.println(vector.get(0)); // 输出: 1 } }- Stack继承自Vector是栈的实现类遵循“后进先出LIFO”原则同样线程安全但性能差现在更推荐用 Deque 实现栈的功能。2. Set 子接口Set 代表无序、不可重复的集合集合中的元素没有索引不允许存储重复元素重复元素会被覆盖。- HashSet基于HashMap实现是最常用的Set实现类。它的底层是哈希表查询、插入、删除的效率都很高但不保证元素的顺序线程不安全。import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; public class HashSetDemo { public static void main(String[] args) { // HashSet无序去重 SetString set new HashSet(); set.add(Java); set.add(Python); set.add(Java); // 重复元素不会被添加 System.out.println(set); // 输出: [Java, Python]顺序不固定 // LinkedHashSet有序去重保留插入顺序 SetString linkedSet new LinkedHashSet(); linkedSet.add(Java); linkedSet.add(Python); linkedSet.add(C); System.out.println(linkedSet); // 输出: [Java, Python, C] } }它的子类 LinkedHashSet 基于哈希表双向链表实现既保留了HashSet的高效操作又能保证元素的插入顺序适合需要既去重又保留顺序的场景。- TreeSet基于TreeMap实现底层是红黑树它会对存储的元素进行自然排序或自定义比较器排序因此可以按照指定顺序遍历元素线程不安全适合需要排序的去重场景。import java.util.Set; import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { // 自然排序Integer默认升序 SetInteger set new TreeSet(); set.add(3); set.add(1); set.add(2); System.out.println(set); // 输出: [1, 2, 3] // 自定义排序字符串长度排序 SetString customSet new TreeSet((s1, s2) - s1.length() - s2.length()); customSet.add(Java); customSet.add(Python); customSet.add(C); System.out.println(customSet); // 输出: [C, Java, Python] } }- EnumSet专门为枚举类型设计的Set实现类基于位运算实现性能极高所有元素必须是指定枚举类型的枚举值线程不安全。二、Map双列集合体系Map 是双列集合的根接口存储的是键Key-值Value映射关系其中键不可重复值可以重复每个键最多对应一个值。它的核心方法有 put() 、 get() 、 remove() 、 containsKey() 、 keySet() 等。1. 常用Map实现类- HashMap基于哈希表JDK 8后为数组链表红黑树实现是最常用的Map实现类。它的查询、插入、删除效率极高键允许为 null 值也允许为 null 线程不安全适合高并发读多写少的场景。import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; public class HashMapDemo { public static void main(String[] args) { // HashMap无序 MapString, Integer map new HashMap(); map.put(Java, 1); map.put(Python, 2); map.put(Java, 3); // 键重复会覆盖旧值 System.out.println(map.get(Java)); // 输出: 3 // 遍历MapkeySet for (String key : map.keySet()) { System.out.println(key : map.get(key)); } // 遍历MapentrySet效率更高 for (Map.EntryString, Integer entry : map.entrySet()) { System.out.println(entry.getKey() : entry.getValue()); } // LinkedHashMap保留插入顺序 MapString, Integer linkedMap new LinkedHashMap(); linkedMap.put(A, 1); linkedMap.put(B, 2); linkedMap.put(C, 3); System.out.println(linkedMap); // 输出: {A1, B2, C3} } }它的子类 LinkedHashMap 基于哈希表双向链表实现既保留了HashMap的高效操作又能保证元素的插入顺序适合需要既高效又保留插入顺序的场景。- TreeMap基于红黑树实现会对键进行自然排序或自定义比较器排序可以按照键的顺序遍历元素键不允许为 null 除非自定义比较器支持线程不安全适合需要按键排序的场景。import java.util.Map; import java.util.TreeMap; public class TreeMapDemo { public static void main(String[] args) { // 自然排序String默认字典序 MapString, Integer map new TreeMap(); map.put(Java, 1); map.put(Python, 2); map.put(C, 3); System.out.println(map); // 输出: {C3, Java1, Python2} // 自定义排序按键长度排序 MapString, Integer customMap new TreeMap((k1, k2) - k1.length() - k2.length()); customMap.put(Java, 1); customMap.put(Python, 2); customMap.put(C, 3); System.out.println(customMap); // 输出: {C3, Java1, Python2} } }- Hashtable早期的Map实现类基于哈希表方法都加了 synchronized 修饰线程安全但性能远低于HashMap键和值都不允许为 null 现在基本不再使用。- ConcurrentHashMap线程安全的HashMap替代类JDK 8后基于CASsynchronized哈希表实现在高并发场景下性能远高于Hashtable键和值都不允许为 null 是高并发场景下的首选Map实现。import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class ConcurrentHashMapDemo { public static void main(String[] args) { MapString, Integer map new ConcurrentHashMap(); map.put(A, 1); map.put(B, 2); // 多线程环境下安全操作 new Thread(() - map.put(C, 3)).start(); new Thread(() - System.out.println(map.get(A))).start(); } }- EnumMap专门为枚举类型设计的Map实现类键必须是指定枚举类型的枚举值基于数组实现性能极高线程不安全。- Properties继承自Hashtable专门用于处理配置文件键和值都是 String 类型常用于读取 .properties 配置文件线程安全。import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; public class PropertiesDemo { public static void main(String[] args) throws Exception { Properties prop new Properties(); // 加载配置文件 prop.load(new FileInputStream(config.properties)); // 获取配置项 String url prop.getProperty(db.url); System.out.println(url); // 修改并保存配置 prop.setProperty(db.user, root); prop.store(new FileOutputStream(config.properties), update config); } }三、集合工具类与使用注意事项1. 工具类- Collections针对 Collection 、 List 、 Set 、 Map 的工具类提供了排序 sort() 、洗牌 shuffle() 、同步化 synchronizedList() 等、不可修改集合 unmodifiableList() 等等静态方法。import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { ListInteger list new ArrayList(); list.add(3); list.add(1); list.add(2); // 排序 Collections.sort(list); System.out.println(list); // 输出: [1, 2, 3] // 洗牌随机打乱 Collections.shuffle(list); // 同步化线程安全 ListInteger syncList Collections.synchronizedList(list); // 不可修改集合 ListInteger unmodList Collections.unmodifiableList(list); // unmodList.add(4); // 会抛出UnsupportedOperationException } }- Arrays针对数组的工具类提供了数组转集合 asList() 、排序、二分查找等方法注意 asList() 返回的是固定长度的集合不能进行增删操作。import java.util.Arrays; import java.util.List; public class ArraysDemo { public static void main(String[] args) { // 数组转集合注意返回的是固定长度集合不能增删 ListString list Arrays.asList(A, B, C); // 数组排序 int[] arr {3, 1, 2}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); // 输出: [1, 2, 3] } }2. 线程安全问题- 大部分集合ArrayList、LinkedList、HashSet、TreeSet、HashMap、TreeMap等都是线程不安全的在多线程环境下直接使用会出现数据不一致问题。- 可以通过 Collections.synchronizedXxx() 方法将非线程安全集合转为线程安全集合也可以直接使用线程安全集合如 Vector 、 Hashtable 、 ConcurrentHashMap 。3. 迭代器与快速失败fail-fast- 集合的迭代器 Iterator 是遍历集合的标准方式在遍历过程中如果直接修改集合如 add() 、 remove() 会触发 ConcurrentModificationException 快速失败机制如果需要在遍历时删除元素需要使用迭代器的 remove() 方法。四、总结Java集合体系可以总结为- 单列集合Collection List 有序可重复ArrayList/LinkedList、 Set 无序不可重复HashSet/TreeSet- 双列集合Map HashMap 高效无顺序、 TreeMap 按键排序、 ConcurrentHashMap 高并发线程安全- 工具类 Collections 、 Arrays集合类型选型场景推荐实现类单列-有序可重复读多写少ArrayList单列-有序可重复写多读少、首尾操作频繁LinkedList单列-无序去重普通去重HashSet单列-有序去重去重且保留插入顺序/排序LinkedHashSet/TreeSet双列-键值对普通存储HashMap双列-键值对保留插入顺序LinkedHashMap双列-键值对按键排序TreeMap双列-键值对高并发场景ConcurrentHashMap*在实际开发中需要根据业务场景选择合适的集合读多写少选ArrayList写多读少选LinkedList去重选HashSet排序选TreeSet键值对存储选HashMap高并发选ConcurrentHashMap。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2630286.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!