Collection 常用方法

方法 详解
boolean add(E e) 添加元素到 Collection 集合中
boolean addAll(Collection<? extends E> c) 将指定 c 中的所有元素都添加到此 Collection 集合中
void clear() 移除此 Collection 集合中的所有元素
boolean contains(Object o) 检查 Collection 集合中是否包含 o 对象,如果包含则返回 true,否则返回 false
boolean containsAll(Collection<?> c) 检查 Collection 集合中是否包含c的全部对象,全部包含则返回 true,否则返回 false
boolean equals(Object o) 比较此 Collection 集合与指定对象是否相等,是比较的是里面元素是否相等,而不是比较地址是否相等
int hashCode() 返回此 Collection 集合的哈希码值
boolean isEmpty() 检查 Collection 集合是否包含有元素,如果没有包含元素,则返回 true,否则返回 false
Iterator<E> iterator() 返回在此 Collection 集合的元素上进行迭代的迭代器
boolean remove(Object o) 从 Collection 集合中删除指定的元素,如果集合中有这个元素,并且删除成功,那么就返回 true,否则返回 false
boolean removeAll(Collection<?> c) 从集合中删除c集合中所有的元素
boolean retainAll(Collection<?> c) 集合中仅保留c集合中的所有元素
int size() 返回集合中元素个数
Object[] toArray() 返回包含此 Collection 集合中所有元素的数组

List 集合常用方法

继承 Collection 常用方法

方法 详解
void add(int index, E element) 在指定位置插入元素,后面的元素都往后移一个元素
boolean addAll(int index, Collection<? extends E> c) 在指定的位置中插入 c 集合全部的元素,如果集合发生改变,则返回 true,否则返回 false
E get(int index) 返回 list 集合中指定索引位置的元素
int indexOf(Object o) 返回 list 集合中第一次出现 o 对象的索引位置,如果 list 集合中没有 o 对象,那么就返回 -1
ListIterator<E> listIterator() 返回此列表元素的列表迭代器(按适当顺序)
ListIterator<E> listIterator(int index) 从指定位置开始,返回此列表元素的列表迭代器(按适当顺序)
E remove(int index) 删除指定索引的对象
E set(int index, E element) 在索引为 index 位置的元素更改为 element 元素
List<E> subList(int fromIndex, int toIndex) 返回从索引 fromIndex 到 toIndex 的元素集合,包左不包右

Set 集合使用方法

继承 Collection 常用方法

Map 集合常用方法

方法 详解
V put(K key, V value) 向 map 集合中添加 Key 为 key,Value 为 value 的元素,当添加成功时返回 null,否则返回 value
void putAll(Map<? extends K,? extends V> m) 向 map 集合中添加指定集合的所有元素
void clear() 把 map 集合中所有的键值删除
boolean containsKey(Object key) 检出 map 集合中有没有包含 Key 为 key 的元素,如果有则返回 true,否则返回 false
boolean containsValue(Object value) 检出 map 集合中有没有包含 Value 为 value 的元素,如果有则返回 true,否则返回 false
Set<Map.Entry<K,V>> entrySet() 返回 map 到一个 Set 集合中,以 map 集合中的 Key=Value 的形式返回到 set 中
boolean equals(Object o) 判断两个 Set 集合的元素是否相同
V get(Object key) 根据 map 集合中元素的 Key 来获取相应元素的 Value
int hashCode() 返回 map 集合的哈希码值
boolean isEmpty() 检出 map 集合中是否有元素,如果没有则返回 true,如果有元素则返回 false
Set<K> keySet() 返回 map 集合中所有 Key
V remove(Object key) 删除 Key 为 key 值的元素
int size() 返回 map 集合中元素个数
Collection<V> values() 返回 map 集合中所有的 Value 到一个 Collection 集合

HashMap

其余的方法都是实现Map集合的

方法 详解
public Object clone() 返回 hashMap 集合的副本

Hashtable

其他的方法都是实现 Map 集合的方法

方法 详解
public Object clone() 返回 Hashtable 的副本
public Enumeration<V> elements() 返回此哈希表中的值的枚举

TreeMap

其他的方法都是实现 Map 集合的方法

方法 详解
public Map.Entry<K,V> ceilingEntry(K key) 返回指定的 Key 大于或等于的最小值的元素,如果没有,则返回 null
public K ceilingKey(K key) 返回指定的 Key 大于或等于的最小值的 Key,如果没有,则返回 null
public Object clone() 返回集合的副本
public Comparator<? super K> comparator() 如果使用默认的比较器,就返回 null,如果使用其他的比较器,则返回比较器的哈希码值
public NavigableSet<K> descendingKeySet() 返回集合的全部 Key,并且是逆序的
public NavigableMap<K,V> descendingMap() 把集合逆序返回
public Map.Entry<K,V> firstEntry() 返回集合中最小 Key 的元素
public K firstKey() 返回集合中最小 Key 的 key
public Map.Entry<K,V> floorEntry(K key) 与 ceilingEntry() 方法相反,是返回小于等于 key 的最大 Key 的元素
public K floorKey(K key) 返回小于等于 key 的最大 Key 的 key
public SortedMap<K,V> headMap(K toKey) 返回 Key 小于 toKey 的所有元素
public NavigableMap<K,V> headMap(K toKey, boolean inclusive) 当 inclusive 为 true 时,就是返回 Key 小于等于 toKey 的所有元素
public Map.Entry<K,V> higherEntry(K key) 返回 Key 大于 key 的所有元素
public K higherKey(K key) 返回 Key 大于 key 的所有 Key
public Map.Entry<K,V> lastEntry() 返回 Key 最大的元素
public K lastKey() 返回 Key 最大的 Key
public Map.Entry<K,V> lowerEntry(K key) 返回小于 key 的最大元素
public K lowerKey(K key) 返回小于 key 最大的 Key
public Map.Entry<K,V> pollFirstEntry() 删除 key 最小的元素
public Map.Entry<K,V> pollLastEntry() 删除最大 Key 的元素
public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) 截取集合中 Key 从 fromKey 到 toKey 的元素,否是截取他们本身,取决于 true 或者 false
public SortedMap<K,V> subMap(K fromKey, K toKey) 截取集合中 Key 从 fromKey 到 toKey 的元素,包括 fromKey,不包括 toKey
public SortedMap<K,V> tailMap(K fromKey) 截取 Key 大于等于 fromKey 的所有元素
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) 当 inclusive 为 true 时,截取 Key 大于等于 fromKey 的所有元素,否则截取 Key 大于 fromKey 的所有元素

Vector 常用方法

Vector 类是实现 List 接口,所以继承的方法就不在这里讲了

方法 详解
public void add(int index, E element) 从 index 索引的位置添加 element 元素,后面的元素都往后移一位
public boolean addAll(int index, Collection<? extends E> c) 从 index 索引位置开始添加 c 集合里所有的元素,后面的元素都往后移 c.size() 位
public void addElement(E obj) 在集合后面添加一个元素,无论该元素是什么类型的,都会把他的 toString() 的返回值添加进去
public int capacity() 返回此向量的当前容量,不是元素个数
public void copyInto(Object[] anArray) 把集合中的元素复制到 anArray 数组中去
public E elementAt(int index) 返回索引位置的元素
public Enumeration<E> elements() 返回集合的枚举
public void ensureCapacity(int minCapacity) 增加集合的容量,如果增大的容量小于10,那么无效,也就是增大容量要是10倍数
public void insertElementAt(E obj, int index) 在指定索引位置中插入 obj 元素
public void removeAllElements() 删除集合的所有元素,并且设置容量为0,和 clear() 方法一样, clear 底层也是用 removeAllElements() 方法的
public void setSize(int newSize) 设置集合的容量大小为 newSize,如果 newSize 大于集合元素个数,那么会在后面添加 null,如果 newSize 小于集合元素个数,那么直保留 newSize 个元素
public void trimToSize() 整理集合的容量大小,如果集合元素个数等于容量大小,那么没有变化,如果集合个数小于容量大小,那么容量会设置为元素个数大小

Arrays 工具类常用方法

  • Arrays.asList(T… data)

    返回一个受指定数组支持的固定大小的列表。

  • Arrays.binarySearch()

    注意:在调用该方法之前,必须先调用 sort() 方法进行排序,如果数组没有排序,
    那么结果是不确定的,此外如果数组中包含多个指定元素,则无法保证将找到哪个元素

    • Arrays.binarySearch(Object[] array, Object key)

      使用二分法查找数组内指定元素的索引值

    • Arrays.binarySearch(Object[] array, int fromIndex, int toIndex, Object obj)

      使用二分法查找数组内指定范围内的指定元素的索引值

  • Arrays.copyOf(T[] original, int newLength)

    拷贝数组,其内部调用了 System.arraycopy() 方法,从下标0开始,如果超过原数组长度,会用null进行填充

  • Arrays.copyOfRange(T[] original, int from, int to)

    拷贝数组,指定起始位置和结束位置,如果超过原数组长度,会用null进行填充

  • Arrays.equals(Object[] array1, Object[] array2)

    判断两个数组是否相等,实际上比较的是两个数组的哈希值,即 Arrays.hashCode(data1) == Arrays.hashCode(data2)

  • Arrays.deepEquals(Object[] array1, Object[] array2)

    判断两个多维数组是否相等,实际上比较的是两个数组的哈希值,即 Arrays.hashCode(data1) == Arrays.hashCode(data2)

  • Arrays.fill()

    • Arrays.fill(Object[] array, Object obj)

      用指定元素填充整个数组(会替换掉数组中原来的元素)

    • Arrays.fill(Object[] array, int fromIndex, int toIndex, Object obj)

      用指定元素填充数组,从起始位置到结束位置,取头不取尾(会替换掉数组中原来的元素)

  • Arrays.sort()

    • Arrays.sort(Object[] array)

      对数组元素进行排序(串行排序)

    • Arrays.sort(T[] array, Comparator<? super T> comparator)

      使用自定义比较器,对数组元素进行排序(串行排序)

    • Arrays.sort(Object[] array, int fromIndex, int toIndex)

      对数组元素的指定范围进行排序(串行排序)

    • Arrays.sort(T[] array, int fromIndex, int toIndex, Comparator<? super T> c)

      使用自定义比较器,对数组元素的指定范围进行排序(串行排序)

  • Arrays.parallelSort()

    • Arrays.parallelSort(T[] array)

      对数组元素进行排序(并行排序),当数据规模较大时,会有更好的性能

  • Arrays.hashCode(Object[] array)

    返回数组的哈希值

  • Arrays.deepHashCode(Object[] array)

    返回多维数组的哈希值

  • Arrays.toString(Object[] array)

    返回数组元素的字符串形式

  • Arrays.deepToString(Object[] array)

    返回多维数组元素的字符串形式

  • Arrays.setAll(T[] array, IntFunction)

    把 array 里的每个元素执行 IntFunction 表达式(一个 lambda 表达式)

  • Arrays.parallelSetAll(T[] array, IntFunction)

    并行地把 array 里的每个元素执行 IntFunction 表达式(一个 lambda 表达式)

  • Arrays.spliterator(T[] array)

    返回数组的分片迭代器,用于并行遍历数组

  • Arrays.stream(T[] array)

    返回数组的流 Stream,然后我们就可以使用 Stream 相关的许多方法了