★09.容器

Collection容器

示意图

★09.容器_第1张图片

Set

  • HashSet:哈希集合
  • LinkedHashSet:链式哈希集合
  • LinkedHashSet:添加顺序哈希集合
  • TreeSet:排序集合
  • CopyOnWriteArraySet:写时复制集合
  • ConcurrentSkipListSet:线程安全排序集合

List

  • ArrayList:数组列表
  • LinkedList:链式列表
  • Vector:线程安全数组列表
  • CopyOnWriteArrayList:写时复制列表

Queue

  • LinkedList:链式列表
  • ArrayDeque:数组双端队列
  • PriorityQueue:优先队列
  • ConcurrentLinkedQueue:线程安全链式队列
  • BlockingDeque:阻塞双端队列
    • LinkedBlockingDeque:链式阻塞双端队列
  • BlockingQueue:阻塞队列
    • DelayQueue:延迟阻塞队列
    • ArrayBlockingQueue:数组阻塞队列
    • LinkedBlockingQueue:链式阻塞队列
    • PriorityBlockingQueue:优先阻塞队列
    • SynchronousQueue:同步阻塞队列
    • LinkedTransferQueue:链式迁移队列

Map容器

示意图

★09.容器_第2张图片

Map

  • HashMap:哈希表
    • LinkedHashMap:链式哈希表
  • Hashtable:线程安全哈希表
    • Properties:属性表
  • IdentityHashMap:键哈希表
  • EnumMap:枚举表
  • WeakHashMap:弱哈希表
  • TreeMap:有序表
  • ConcurrentSkipListMap:线程安全有序表
  • ConcurrentHashMap:线程安全哈希表

常用工具

Arrays

  • Arrays.asList():接受一个数组或者一个逗号分隔的元素列表,返回一个尺寸无法改变的List。

Collections

  • Collections.addAll:接受一个Collection对象和一个逗号分隔的元素列表。把元素列表添加到这个Collection中,返回这个Collection对象。
  • Collections.sort
  • Collections.shuffle
  • Collections.fill:用元素引用填充容器,参数(容器,元素)。
  • Collections.nCopies:生成n个对象的List,参数(个数,元素)。

迭代器

迭代器解惑

List pets = Arrays.asList("a", "b", "c", "d", "e", "f", "g");

ListIterator it1 = pets.listIterator(7);
while (it1.hasPrevious()) {
    System.out.println(it1.previousIndex() + ", " + it1.previous() + "; ");
}

System.out.println();
ListIterator it2 = pets.listIterator(0);
while (it2.hasNext()) {
    System.out.println(it2.nextIndex() + ", " + it2.next() + ", ");
}

foreach

  1. foreach依赖Iterable接口:
    1. for(var v : Iterable接口)
    2. Iterable.iterator():返回一个Iterator<>类。

哈希方法

  1. 编写HashCode的技巧:
    1. 定义一个result的int变量,并赋值为某个非零常量,如17。
    2. 根据下表为每一个有意义的域f计算出一个散列码c。


      ★09.容器_第3张图片
    3. 合并计算:
String s = "something";
Integer id = 100;
int result = 17;
result = 37 * result + s.hashCode();
result = 37 * result + id.hashCode();

你可能感兴趣的:(★09.容器)