Java集合体系学习笔记(二)

74 阅读9分钟

前言

Java集合体系学习笔记(一)中介绍了Collection体系 并画出了一个简单的类图 接下来就学习一下Collection接口的子接口以及Queue接口和Map接口

image.png

List接口

List接口简介

image.png

从List的源码可以知道 List接口是继承了Collection接口的


An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

Unlike sets, lists typically allow duplicate elements.

The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator, add, remove, equals, and hashCode methods. Declarations for other inherited methods are also included here for convenience.

The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.

The List interface provides a special iterator, called a ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.


从文档的内容可以知道

  1. List是一个有序集合的接口
  2. List中允许重复元素的存在
  3. 使用接口的人需要准确控制顺序位置
  4. 可以通过位置查找元素
  5. List允许存在重复元素
  6. List提供了ListIterator迭代器,允许元素插入和替换以及双向访问。

List接口方法

List接口方法分为两种 一类是继承自Collection接口的方法 另一类是没有继承自Collection接口的方法

继承自Collection接口的方法

  1. int size():返回List的大小

  2. boolean isEmpty():返回List是否为空

  3. boolean contains(Object o):如果此列表包含指定元素,则返回 true,否则返回false

  4. Iterator<E> iterator():按正确顺序返回此List中元素的迭代器。

  5. Object[] toArray():返回一个数组,其中按正确顺序(从第一个元素到最后一个元素)包含此List中的所有元素。

  6. boolean add(E e): 添加一个元素到List中

  7. boolean remove(Object o) 删除一个List中的元素

    ······

非继承自Collection接口的方法

非继承自Collection的方法大多都是为了能够根据index进行操作的方法 具体如下

  1. E get(int index): 查找特定位置的元素

  2. E set(int index, E element):修改特定位置的元素

  3. void add(int index, E element): 在此列表中的指定位置插入指定元素 插入之后的元素位置会后移

  4. E remove(int index):移除指定位置的元素,移除之后 位置后的元素会前移动

  5. int indexOf(Object o): 返回此列表中指定元素第一次出现的索引,如果此列表不包含该元素,则返回 -1。

  6. int lastIndexOf(Object o):返回此列表中指定元素最后一次出现的索引,如果此列表不包含该元素,则返回 -1。

  7. ListIterator<E> listIterator(): 返回此列表中元素的列表迭代器(按正确的顺序)。

  8. ListIterator<E> listIterator(int index):返回列表中元素的列表迭代器(按正确的顺序),从列表中的指定位置(index)开始。指定的索引指示对 next 的初始调用将返回的第一个元素。对 previous 的初始调用将返回指定索引减一的元素。

  9. List<E> subList(int fromIndex, int toIndex): 返回此列表中指定的 fromIndex(包含)和 toIndex(不包含)之间的部分的视图。 (如果 fromIndex 和 toIndex 相等,则返回的列表为空。)

Set 接口


A collection that contains no duplicate elements.

The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add, equals and hashCode methods.


从上述信息中可以得到

  1. 集合中无重复元素
  2. 如果用Collection参数的构造器,传入的参数不能有重复元素
  3. 集合中的元素没有顺序性
  4. 要实现一个集合 需要实现一个有效的equals方法(如果equals方法有问题 则判断是否重复可能会错误)

Set就是数学中所定义的集合 因此它满足如下的性质

  1. 确定性
  2. 互异性
  3. 无序性

Set接口方法

  1. add(E e) 用于将指定的元素添加到集合中,如果元素已经存在,则不会进行重复添加。
  2. addAll(Collection<? extends E> c) 将另一个集合中的所有元素添加到当前集合中,确保不添加重复元素。
  3. clear() 清空集合中的所有元素。
  4. contains(Object o) 判断集合是否包含指定的元素。
  5. isEmpty() 检查集合是否为空。
  6. iterator() 返回一个用于遍历集合中元素的迭代器。
  7. remove(Object o) 从集合中删除指定的元素。
  8. removeAll(Collection<?> c) 从集合中删除另一个集合中包含的所有元素。
  9. retainAll(Collection<?> c) 保留集合中与另一个集合共享的元素,删除其他元素。
  10. size() 返回集合中元素的数量。
  11. toArray() 将集合转换为一个数组。
  12. boolean equals(Object o):

比对 SetCollection 方法:

Set 接口是 Collection 接口的子接口,它在确保集合中不包含重复元素方面提供了额外的保证和方法。

Queue接口

前置知识

计算机中的栈和队列

栈(Stack) 是一种线性数据结构,它遵循后进先出(LIFO)的原则。栈通常有两个主要操作:

  1. 压入(Push) :将元素添加到栈的顶部。
  2. 弹出(Pop) :从栈的顶部移除元素。

队列(Queue) 也是一种线性数据结构,遵循先进先出(FIFO)的原则。队列通常有两个主要操作:

  1. 入队(Enqueue) :将元素添加到队列的尾部。
  2. 出队(Dequeue) :从队列的前端移除元素。

数据结构中的栈和队列全部都是采用Queue接口去实现的

Queue介绍


A collection designed for holding elements prior to processing.

Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations.Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation).

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out)


上述内容告诉我们:

  1. Queue将元素放在一起,通过优先级进行处理
  2. 提供了额外的操作,操作有两种形式,报错和返回特殊值
  3. 队列不是必须是FIFO的,也可以是LIFO

Queue中的方法

image.png

一般情况下建议使用offer poll peek方法 也就是返回特殊值方法

Map接口

简介

首先看一下Map接口的介绍

image.png

从上可以看到 Map接口没有继承Collection接口 但是却是在Collection体系之中

Map接口简介

Map接口不同于Collection接口,Collection接口是一种单值接口,而Map是一种key,value形的接口 并且具有如下特性

  1. Map是Collection体系的一部分
  2. 一个用于作key和value之间之间的映射对象
  3. 不能包括重复的key
  4. 每个key最多最有一个value
  5. Java提供了三种view,分别是
view含义
Set key所有key构成的Set
Collection values所有value构成的Collection
Set Entry<K,V>Entry所构成的集合,Entry是key,value的映射
  1. 可变对象用作Map的key需要谨慎

Map中的接口

Map中的接口可以分为如下

  1. 内部接口Entry
  2. 普通方法
  3. 用于curd的方法
  4. 用于获取views的方法
  5. default方法

Entry接口

Map 接口中的 Entry 接口用于表示 Map 中的键值对(key-value pair),可以通过K getKey()方法拿到Entry键值对的key,通过V getValue()方法拿到Entry键值对的value

普通方法介绍

  1. int size(): 返回此映射中键-值映射的数量。如果映射包含超过 Integer.MAX_VALUE 个元素,则返回 Integer.MAX_VALUE。
  2. boolean isEmpty(): 如果map是空的 返回true 否则返回false
  3. void clear(): 清空map
  4. boolean containsKey(Object key): 接收一个Object类型的参数key,查看map中是否有这个key 如果有返回true 否则返回false
  5. boolean containsValue(Object value): 接收一个Object类型的参数value,查看map中是否有这个value 如果有返回true 否则返回false

curd方法介绍

  1. V get(Object key): 接收一个Object类型的参数key,查找key是否有对应的value,如果有对应的value 则返回这个value 否则返回null
  2. V put(K key,V value): 接收两个参数keyvalue,如果key不存在,则创建一个<key,value>的键值对,如果key存在,则用参数value更新key映射的值,返回这个key在更新前对应的值,如果没有的话 返回null
  3. V remove(Object key): 接受一个参数key,方法用于从Map中删除指定键(key)的映射。如果映射中存在具有指定键的映射,那么 remove 方法会返回与该键关联的值,并且该映射将从映射中移除。如果映射中没有具有指定键的映射,那么 remove 方法将返回 null

views方法介绍

  1. Set<K> keySet(): 方法返回由Map中所有key构成的set
  2. Collection<V> values(): 方法返回由Map中所有value构成的Collection
  3. Set <Map.Entry<key,value>> entrySet(): 方法返回所有键值对<key,value>组成的Set

default方法介绍

  1. default V getOrDefault(Object key, V defaultValue): 接收一个Object类型的参数key和一个参数defaultValue,查找key是否有对应的value,如果有对应的value 则返回这个value 否则返回defaultValue

  2. default V putIfAbsent(K key, V value): 接收一个参数key和一个参数value, 如果指定的键在映射中不存在,它将键值对添加到映射中,然后返回 null,如果指定的键已经存在于映射中,它将返回与该键关联的当前值,而不执行添加操作。

总结

本次学习了List接口,set接口,Queue接口,Map接口,根据本次学习的内容扩展之前的类图,具体如下

image.png