当前位置: 首页 > news >正文

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

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

  • Java集合
    • 1. 概览
    • 2. Java 集合框架总览
      • 集合的优势
      • 基本组成
        • 第三方集合库
        • java.util.Collection 接口
        • java.util.Map 接口
    • 3. Java 集合框架內建实现
    • 4. Java 集合框架抽象实现
    • 5. 集合框架面试题
    • 6. 资源推荐

Java集合

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

1. 概览

  • Java8 Documentation
    • Reference - Java SE API Documentation
    • Learn the Language - Java Tutorials Learning Paths
      • Building On The Foundation - Collections
        • https://docs.oracle.com/javase/tutorial/collections/intro/index.html
        • https://docs.oracle.com/javase/tutorial/collections/interfaces/index.html
    • Reference - Developer Guides
      • lang and util Base Libraries - collections
        • https://docs.oracle.com/javase/8/docs/technotes/guides/collections/overview.html
        • https://docs.oracle.com/javase/8/docs/technotes/guides/collections/reference.html

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

相关文章:

  • C#3.0入门系列(四)
  • GCC 的简单使用
  • 关于Keil 的一些看法
  • Jackson 简单使用记录
  • java基础设计 开源框架
  • Spring源码学习笔记
  • 正则资料笔记
  • C#接口慨述
  • Inversion of Control 简要笔记
  • 定义接口及定义接口成员
  • 小马哥讲Spring核心编程思想 - 第二章 重新认识IoC
  • 访问接口
  • 记录一次 post 请求的并发测试
  • RPC与 Dubbo 资料梳理
  • 实现接口
  • 收藏网友的 源程序下载网
  • 77. Combinations
  • Android交互
  • CentOS6 编译安装 redis-3.2.3
  • Hibernate【inverse和cascade属性】知识要点
  • Java 23种设计模式 之单例模式 7种实现方式
  • JavaScript异步流程控制的前世今生
  • java中的hashCode
  • java中具有继承关系的类及其对象初始化顺序
  • spring-boot List转Page
  • underscore源码剖析之整体架构
  • 闭包--闭包作用之保存(一)
  • 免费小说阅读小程序
  • 你不可错过的前端面试题(一)
  • 如何设计一个比特币钱包服务
  • 【干货分享】dos命令大全
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • raise 与 raise ... from 的区别
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • "无招胜有招"nbsp;史上最全的互…
  • # Panda3d 碰撞检测系统介绍
  • #define、const、typedef的差别
  • #免费 苹果M系芯片Macbook电脑MacOS使用Bash脚本写入(读写)NTFS硬盘教程
  • $(selector).each()和$.each()的区别
  • (2015)JS ES6 必知的十个 特性
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (Java数据结构)ArrayList
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (使用vite搭建vue3项目(vite + vue3 + vue router + pinia + element plus))
  • (转)可以带来幸福的一本书
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版
  • .NET CF命令行调试器MDbg入门(二) 设备模拟器
  • .NET Remoting学习笔记(三)信道
  • .NET设计模式(11):组合模式(Composite Pattern)
  • .NET中GET与SET的用法
  • [ Linux 长征路第五篇 ] make/Makefile Linux项目自动化创建工具
  • [20150707]外部表与rowid.txt
  • [AutoSar]BSW_Com07 CAN报文接收流程的函数调用
  • [BetterExplained]书写是为了更好的思考(转载)