一入 Java 深似海 -- S02E02 学习笔记

Java集合

  • 集合框架总览
  • 集合框架内建实现
  • 集合框架抽象实现
  • 集合框架面试题

1. 概览

2. Java 集合框架总览

A collection is an object that represents a group of objects (such as the classic Vector class). A collections framework is a unified architecture for representing and manipulating collections, enabling collections to be manipulated independently of implementation details.

集合的优势

  • Reduces programming effort by providing data structures and algorithms so you don’t have to write them yourself.
    • 减少编程的负担
      • 数据结构
        • 链表、栈,队列,树,…
  • Increases performance by providing high-performance implementations of data structures and algorithms. Because the various implementations of each interface are interchangeable, programs can be tuned by switching implementations.
    • 提高性能
  • Provides interoperability between unrelated APIs by establishing a common language to pass collections back and forth.
    • 提供无关 API 之间的互用性
      • Collction.addAll(Collection c)
      • List.addAll(Collection c)
      • Arrays.asList(T… a)
  • Reduces the effort required to learn APIs by requiring you to learn multiple ad hoc collection APIs.
    • 减少学习 API 的负担
      • 相对于数组而言,集合操作更便捷
      • System.arrayCopy(Object src, Object dest, int destPos, int length)
        • Why is Object ? (answer:因为有多维数组的存在,设计时应考虑更大限度的抽象)
  • Reduces the effort required to design and implement APIs by not requiring you to produce ad hoc collections APIs.
    • 减少设计与实现 API 的负担
      • EnumSet.of
  • Fosters software reuse by providing a standard interface for collections and algorithms with which to manipulate them.
    • 促进软件重用

基本组成

  • Collection interface 集合接口
    • Collection
    • Map
  • Infrastructure 基础设施
  • General-purpose implementations 通用实现
    • ArrayList
    • LinkedList
    • HashMap
    • TreeMap
    • HashSet
  • Abstract implementations 抽象实现
  • Legacy implementations 遗留实现
  • Convenience implementations 便利实现
    • EnumSet
      • Enum
      • Enumeration
    • Set.of(T… t)
    • ImmutableCollections
  • Wrapper implementations 包装实现
  • Special-purpose implementations 特殊实现
    • WeakSoftHashMap
  • Array Utilities 数组工具类
    • Arrays
      • binarySearch
  • Algorithms 算法
  • Concurrent implementations 并发实现
第三方集合库
  • guava
  • apache commons-collections
    • Bag
    • BoundedMap
      • 范围默认 Integer.MAX_VALUE
java.util.Collection 接口
  • 通用接口

    • java.util.Collection
      • iterator()
        • ConcurrentModificationException
      • T[] toArray()
    • java.util.List
      • int size()
        • 最大 Integer.MAX_VALUE
        • 为何不用 long ? 和底层 CPU 架构有关,在 32 位操作系统上,long 类型非线程安全(占用8字节,即64bit),因为32位OS并不能完整存储一个long类型对象,拆分高位和低位存储。
    • java.util.Set
      • 并不能保证有序
    • java.util.SortedSet
      • 有序的
    • java.util.NavigableSet
      • since 1.6
      • 继承自 SortedSet
      • SortedSet tailSet(E fromElement);
    • java.util.Queue
      • Doug Lea
      • add(E e)
        • 与 Collection 中的 add 方法语义中发生一些细微的变化,如抛出的异常
      • 只读 Collections.unmodifiableList
    • java.util.Deque
      • 继承 Queue
      • 实现类 LinkedList
  • 并发接口

    • java.util.concurrent.BlockingQueue since1.5
    • java.util.concurrent.BlockingDeque since1.6
    • java.util.concurrent.TransferQueue since1.7
      • 推荐
      • LinkedTransferQueue
java.util.Map 接口
  • 通用接口

    • java.util.SortedMap
      • Set 的底层使用的是 Map 的实现;
    • java.util.NavigableMap since1.6
  • 并发接口

    • java.util.concurrent.ConcurrentMap
      • ConcurrentHashMap
    • java.util.concurrent.ConcurrentNavigableMap
      • ConcurrentSkipListMap

小插曲:1. hash 和 equal 方法要写好;2. 反射很耗性能;

一致性哈希 Consistent Hash :

  • https://en.wikipedia.org/wiki/Consistent_hashing

  • https://github.com/Jaskey/ConsistentHash

  • https://www.toptal.com/big-data/consistent-hashing

  • https://www.acodersjourney.com/system-design-interview-consistent-hashing/

  • https://www.jianshu.com/p/58fde9b2d0a3

  • http://www.zsythink.net/archives/1182/

3. Java 集合框架內建实现

  • 遗留实现

    • java.util.Vector
      • 与 ArrayList 比较
      • synchronized
    • java.util.Stack
      • 继承 Vector
      • LIFO
      • 频繁扩容
    • java.util.Hashtable
      • 对比 HashMap
        • synchronized
      • 继承 Dictionary
    • java.util.Enumeration
      • vs Enum
      • StringTokenizer
    • java.util.BitSet
  • 通用实现
    在这里插入图片描述

  • 并发接⼝

    • java.util.concurrent.BlockingQueue
    • java.util.concurrent.TransferQueue
    • java.util.concurrent.BlockingDeque
    • java.util.concurrent.ConcurrentMap
    • java.util.concurrent.ConcurrentNavigableMap
  • 并发实现

    • java.util.concurrent.LinkedBlockingQueue
    • java.util.concurrent.ArrayBlockingQueue
    • java.util.concurrent.PriorityBlockingQueue
    • java.util.concurrent.DelayQueue
    • java.util.concurrent.SynchronousQueue
    • java.util.concurrent.LinkedBlockingDeque
    • java.util.concurrent.LinkedTransferQueue
    • java.util.concurrent.CopyOnWriteArrayList
    • java.util.concurrent.CopyOnWriteArraySet
    • java.util.concurrent.ConcurrentSkipListSet
    • java.util.concurrent.ConcurrentHashMap
    • java.util.concurrent.ConcurrentSkipListMap

4. Java 集合框架抽象实现

  • 基于 java.util.Collection 接⼝
    • java.util.AbstractCollection
      • java.util.AbstractList
        • Arrays.ArrayList Arrays.asList(T …)
        • ArrayList
      • java.util.AbstractSet
      • java.util.AbstractQueue(Since Java 1.5)
        • offer peek poll
  • 基于 java.util.Map 接⼝
    • java.util.AbstractMap

5. 集合框架面试题

  • Java Interview Questions.pdf

6. 资源推荐

  • 《Java Generics and Collections》 O’Reilly, October 2006, 0-596-52775-6
  • Cheat Sheet : https://zeroturnaround.com/rebellabs/Java-Collections-Cheat-Sheet/
    • https://www.jrebel.com/blog/java-collections-cheat-sheet
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值