Java之集合的认识

以下英文部分转载自java se 12 & jdk 12 doc,快速访问

Iterable< E>

简介

接口。泛型。大部分集合类的父类。用于遍历列表。

方法

其中我们可以看下default修饰符,这也是Java 8后新出现的,我们知道,如果我们给一个接口新添加一个方法,那么所有他的具体子类都必须实现此方法,为了能给接口拓展新功能,而又不必每个子类都要实现此方法,Java 8新加了default关键字,被其修饰的方法可以不必由子类实现,并且由dafault修饰的方法在接口中有方法体,这打破了Java之前对接口方法的规范。

修饰和类型方法描述
default voidforEach​(Consumer<? super T> action)Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.为该迭代器的每个元素执行给定的动作,当所有元素都处理完或者该动作抛出一个异常则停止。
Iteratoriterator()Returns an iterator over elements of type T.返回一个包括类型T的所有元素的迭代器。
default Spliteratorspliterator()Creates a Spliterator over the elements described by this Iterable.创建并返回一个可分割迭代器。提供了一个可以并行遍历元素的迭代器,以适应现在cpu多核时代并行遍历的需求。

Collection< E>

简介

接口。泛型。父类是Iterable。

方法

修饰和类型方法描述
booleanadd​(E e)Ensures that this collection contains the specified element (optional operation).添加集合的元素。
booleanaddAll​(Collection<? extends E> c)Adds all of the elements in the specified collection to this collection (optional operation).将指定集合的所有元素添加到该集合。
voidclear()Removes all of the elements from this collection (optional operation).移除所有元素。
booleancontains​(Object o)Returns true if this collection contains the specified element.如果集合包含指定元素就返回true。
booleancontainsAll​(Collection<?> c)Returns true if this collection contains all of the elements in the specified collection.如果该集合包含指定集合的所有元素就返回true。
booleanequals​(Object o)Compares the specified object with this collection for equality.比较与其他对象是否浅相等。
inthashCode()Returns the hash code value for this collection.返回该集合的哈希码值。
booleanisEmpty()Returns true if this collection contains no elements.如果该集合没有包含任何元素就返回true。
Iteratoriterator()Returns an iterator over the elements in this collection.
default StreamparallelStream()Returns a possibly parallel Stream with this collection as its source.通过该集合返回一个可能的平行流作为该集合的来源。
booleanremove​(Object o)Removes a single instance of the specified element from this collection, if it is present (optional operation).从该集合移除单个指定元素的实例,如果该实例存在。
booleanremoveAll​(Collection<?> c)Removes all of this collection’s elements that are also contained in the specified collection (optional operation).移除该集合中与指定集合有相同元素的所有元素。
default booleanremoveIf​(Predicate<? super E> filter)Removes all of the elements of this collection that satisfy the given predicate.移除满足给定条件的所有元素。
booleanretainAll​(Collection<?> c)Retains only the elements in this collection that are contained in the specified collection (optional operation).只保留该集合中与指定集合有相同元素的所有元素。
intsize()Returns the number of elements in this collection.返回该集合元素的数量。
default Spliteratorspliterator()Creates a Spliterator over the elements in this collection.
default Streamstream()Returns a sequential Stream with this collection as its source.返回该集合的一个顺序流作为他的源头。
Object[]toArray()Returns an array containing all of the elements in this collection.返回一个包含该集合的所有元素的数组。
default T[]toArray​(IntFunction<T[]> generator)Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.返回一个包含该集合的所有元素的数组,使用提供的生成器功能分配返回的数组。
T[]toArray​(T[] a)Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.返回一个包含该集合的所有元素的数组;返回数组的运行时类型与指定数组一致。

List< E>

简介

接口。泛型。父类是Collection。

方法

修饰和类型方法描述
static ListcopyOf​(Collection<? extends E> coll)Returns an unmodifiable List containing the elements of the given Collection, in its iteration order.返回一个包含给定集合的所有元素,且按他的迭代器的顺序的不可修改的列表。
static Listof()Returns an unmodifiable list containing zero elements.返回一个包含0个元素的不可变列表。
static Listof​(E e1)Returns an unmodifiable list containing one element.返回一个包含1(2、3、4…10)个元素的不可变列表。
static Listof​(E… elements)Returns an unmodifiable list containing an arbitrary number of elements.返回一个包含任意数量的元素的不可变列表。
voidadd​(int index, E element)Inserts the specified element at the specified position in this list (optional operation).在这个列表的指定位置插入指定元素(可选操作)。
booleanadd​(E e)Appends the specified element to the end of this list (optional operation).在列表末尾追加指定元素。
booleanaddAll​(int index, Collection<? extends E> c)Inserts all of the elements in the specified collection into this list at the specified position (optional operation).在列表的指定位置插入指定集合里的元素。
booleanaddAll​(Collection<? extends E> c)Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator (optional operation).在列表末尾追加指定集合,顺序由集合的迭代器返回元素顺序决定。
voidclear()Removes all of the elements from this list (optional operation).移除所有元素。
booleancontains​(Object o)Returns true if this list contains the specified element.如果包含指定元素就返回是。
booleancontainsAll​(Collection<?> c)Returns true if this list contains all of the elements of the specified collection.如果列表包含所有在指定集合里的元素就返回是。
booleanequals​(Object o)Compares the specified object with this list for equality.比较指定对象与该列表的(浅)相等。
Eget​(int index)Returns the element at the specified position in this list.返回指定位置的元素。
inthashCode()Returns the hash code value for this list.返回该列表的哈希码。
intindexOf​(Object o)Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.返回列表中第一次遇到指定元素的序号,或者当列表不包含此元素时返回-1。
booleanisEmpty()Returns true if this list contains no elements.如果列表不包含元素就返回是。
Iteratoriterator()Returns an iterator over the elements in this list in proper sequence.返回遍历器。
intlastIndexOf​(Object o)Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.返回列表中最后遇到指定元素的序号,或者当列表不包含此元素时返回-1。
ListIteratorlistIterator()Returns a list iterator over the elements in this list (in proper sequence).返回一个列表遍历器。
ListIteratorlistIterator​(int index)Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.返回一个从指定位置开始的列表遍历器。
Eremove​(int index)Removes the element at the specified position in this list (optional operation).移除并返回指定位置的元素。
booleanremove​(Object o)Removes the first occurrence of the specified element from this list, if it is present (optional operation).移除列表中首次遇到的指定元素,如果存在的话(可选操作)。
booleanremoveAll​(Collection<?> c)Removes from this list all of its elements that are contained in the specified collection (optional operation).移除列表中所有包含在指定集合里的元素(可选操作)。
default voidreplaceAll​(UnaryOperator operator)Replaces each element of this list with the result of applying the operator to that element.用应用操作器到那个元素替代这个列表的每个元素。
booleanretainAll​(Collection<?> c)Retains only the elements in this list that are contained in the specified collection (optional operation).只保留列表中包含在指定集合内的元素。
Eset​(int index, E element)Replaces the element at the specified position in this list with the specified element (optional operation).用指定元素替代指定位置的元素。
intsize()Returns the number of elements in this list.返回列表元素数量。
default voidsort​(Comparator<? super E> c)Sorts this list according to the order induced by the specified Comparator.根据指定比较器操作的顺序排列这个列表。
default Spliteratorspliterator()Creates a Spliterator over the elements in this list.为列表中的所有元素创建一个分路器。
ListsubList​(int fromIndex, int toIndex)Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.返回这个列表从指定开始序号(包括)到结束序号(不包括)之间的子列表。
Object[]toArray()Returns an array containing all of the elements in this list in proper sequence (from first to last element).返回一个包含所有元素且以合适的顺序排列(从第一个到最后一个元素)的数组。
T[]toArray​(T[] a)Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.返回一个包含所有元素且以合适的顺序排列(从第一个到最后一个元素)的数组;返回数组的运行时类型是指定的数组。

ArrayList< E>

简介

类。泛型。在内存中存储列表数据。

构造器

构造器描述
ArrayList()Constructs an empty list with an initial capacity of ten.构造一个初始容量为10的空列表。
ArrayList​(int initialCapacity)Constructs an empty list with the specified initial capacity.构造一个给定容量的空列表。
ArrayList​(Collection<? extends E> c)Constructs a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.构造一个包含给定集合所有元素的列表,顺序由集合遍历器返回决定。

Set< E>

简介

接口。泛型。父类是Collection。

方法

修饰和类型方法描述
static SetcopyOf​(Collection<? extends E> coll)Returns an unmodifiable Set containing the elements of the given Collection.返回一个包含给定集合的所有元素的不可变的组。
static Setof()Returns an unmodifiable set containing zero elements.返回一个不包含元素的不可变的组。
static Setof​(E e1)Returns an unmodifiable set containing one element.返回一个包含1个元素的不可变的组。
static Setof​(E… elements)Returns an unmodifiable set containing an arbitrary number of elements.返回一个包含任意数量元素的不可变的组。
booleanadd​(E e)Adds the specified element to this set if it is not already present (optional operation).如果给定元素不在组内就添加进去。
booleanaddAll​(Collection<? extends E> c)Adds all of the elements in the specified collection to this set if they’re not already present (optional operation).如果给定集合内的元素不在组中就添加进去。
voidclear()Removes all of the elements from this set (optional operation).移除所有组中的元素。
booleancontains​(Object o)Returns true if this set contains the specified element.如果组包含给定的元素就返回是。
booleancontainsAll​(Collection<?> c)Returns true if this set contains all of the elements of the specified collection.如果组包含给定集合中所有的元素就返回是。
booleanequals​(Object o)Compares the specified object with this set for equality.比较与给定对象是否浅相等。
inthashCode()Returns the hash code value for this set.返回这个组的哈希值。
booleanisEmpty()Returns true if this set contains no elements.如果该组不含元素就放是。
Iteratoriterator()Returns an iterator over the elements in this set.返回这个组的遍历器。
booleanremove​(Object o)Removes the specified element from this set if it is present (optional operation).如果给定元素存在就移除。
booleanremoveAll​(Collection<?> c)Removes from this set all of its elements that are contained in the specified collection (optional operation).移除所有包含在给定集合的元素。
booleanretainAll​(Collection<?> c)Retains only the elements in this set that are contained in the specified collection (optional operation).只保留存在指定集合中的元素。
intsize()Returns the number of elements in this set (its cardinality).返回组中元素的数量。
default Spliteratorspliterator()Creates a Spliterator over the elements in this set.创建并返回一个分路器。
Object[]toArray()Returns an array containing all of the elements in this set.返回一个包含组中所有元素的数组。
T[]toArray​(T[] a)Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.返回一个包含组中所有元素的数组;返回数组的运行时类型就是给定数组的运行时类型。

Map<K,​V>

简介

接口。泛型。

内部类

修饰和类型方法描述
static interfaceMap.Entry<K,​V>A map entry (key-value pair).一个map的实体(键值对)

方法

修饰和类型方法描述
static <K,​V> Map<K,​V>copyOf​(Map<? extends K,​? extends V> map)Returns an unmodifiable Map containing the entries of the given Map.返回一个包含给定地图的所有实体的不可变的地图。
static <K,​V> Map.Entry<K,​V>entry​(K k, V v)Returns an unmodifiable Map.Entry containing the given key and value.返回一个包含给定键值的地图实体。
static <K,​V> Map<K,​V>of()Returns an unmodifiable map containing zero mappings.返回一个包含空映射的不可变地图。
static <K,​V> Map<K,​V>of​(K k1, V v1)Returns an unmodifiable map containing a single mapping.返回一个包含1(2、3、4…10)个映射的不可变地图。
static <K,​V> Map<K,​V>ofEntries​(Map.Entry<? extends K,​? extends V>… entries)Returns an unmodifiable map containing keys and values extracted from the given entries.返回一个包含从给定的实体集中提取的键值对的不可变地图。
voidclear()Removes all of the mappings from this map (optional operation).移除地图中的所有映射(可选操作)。
default Vcompute​(K key, BiFunction<? super K,​? super V,​? extends V> remappingFunction)Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).尝试为指定键和他的当前映射值(若没有当前映射则为null)计算一个映射。
default VcomputeIfAbsent​(K key, Function<? super K,​? extends V> mappingFunction)If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.如果指定键之前没有关联一个值(或映射到null),就尝试使用给定的映射方法计算他的值并且将他放入这个地图除非值为null。
default VcomputeIfPresent​(K key, BiFunction<? super K,​? super V,​? extends V> remappingFunction)If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.如果给定键的值是存在的和非空的,就尝试用给定的键和他当前映射的值计算一个新的映射。
booleancontainsKey​(Object key)Returns true if this map contains a mapping for the specified key.如果该地图包含一个给定键的映射就返回是。
booleancontainsValue​(Object value)Returns true if this map maps one or more keys to the specified value.如果这个地图映射一个或更多键到给定的值就返回是。
Set<Map.Entry<K,​V>>entrySet()Returns a Set view of the mappings contained in this map.返回该地图包含的映射的Set集。
booleanequals​(Object o)Compares the specified object with this map for equality.与给定对象比较浅相等。
default voidforEach​(BiConsumer<? super K,​? super V> action)Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.为地图的每个实体执行给定的动作直到所有实体处理完或者动作抛出一个异常。
Vget​(Object key)Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.返回给定键映射的值,或者如果地图不含给定键的映射就返回null。
default VgetOrDefault​(Object key, V defaultValue)Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.同上。
inthashCode()Returns the hash code value for this map.返回这个地图的哈希值。
booleanisEmpty()Returns true if this map contains no key-value mappings.如果该地图不包含键值映射关系就返回是。
SetkeySet()Returns a Set view of the keys contained in this map.返回该地图包含的键的Set集。
default Vmerge​(K key, V value, BiFunction<? super V,​? super V,​? extends V> remappingFunction)If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.如果给定键没有关联一个值或者关联null,就关联给定的非空值。
Vput​(K key, V value)Associates the specified value with the specified key in this map (optional operation).在地图中插入给定键值对(可选操作)。
voidputAll​(Map<? extends K,​? extends V> m)Copies all of the mappings from the specified map to this map (optional operation).从给定地图中复制所有映射到该地图(可选操作)。
default VputIfAbsent​(K key, V value)If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.如果给定键不关联一个值(或映射到null),就关联到给定的值并且返回null,否则返回当前值。
Vremove​(Object key)Removes the mapping for a key from this map if it is present (optional operation).移除给定键的映射关系如果该关系存在。
default booleanremove​(Object key, Object value)Removes the entry for the specified key only if it is currently mapped to the specified value.移除给定键的实体仅当键映射到给定的值。
default Vreplace​(K key, V value)Replaces the entry for the specified key only if it is currently mapped to some value.替代给定键的实体仅当键映射到一些值。
default booleanreplace​(K key, V oldValue, V newValue)Replaces the entry for the specified key only if currently mapped to the specified value.替代给定键的实体仅当键映射到给定的值。
default voidreplaceAll​(BiFunction<? super K,​? super V,​? extends V> function)Replaces each entry’s value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.用调用给定方法作用到每个实体的结果替代每个实体的值,直到所有 实体都处理完成或者方法抛出一个异常。
intsize()Returns the number of key-value mappings in this map.返回地图键值对关系的数量。
Collectionvalues()Returns a Collection view of the values contained in this map.返回一个地图中所有的值的集合。

HashMap<K,​V>

简介

类。泛型。在内存中存储键值对。

构造器

构造器描述
HashMap()Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).构造一个默认初始容量(16)和默认加载因子(0.75)的空的哈希地图。
HashMap​(int initialCapacity)Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).构造一个给定初始容量和默认加载因子(0.75)的空的哈希地图。
HashMap​(int initialCapacity, float loadFactor)Constructs an empty HashMap with the specified initial capacity and load factor.构造一个给定初始容量和加载因子的空的哈希地图。
HashMap​(Map<? extends K,​? extends V> m)Constructs a new HashMap with the same mappings as the specified Map.用相似的映射如给定的地图构造一个新的哈希地图。

ConcurrentMap<K,​V>

简介

接口。泛型。父接口为Map。

方法

修饰和类型方法描述
default Vcompute​(K key, BiFunction<? super K,​? super V,​? extends V> remappingFunction)Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).尝试为指定键和他的当前映射值(若没有当前映射则为null)计算一个映射。
default VcomputeIfAbsent​(K key, Function<? super K,​? extends V> mappingFunction)If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.如果指定键之前没有关联一个值(或映射到null),就尝试使用给定的映射方法计算他的值并且将他放入这个地图除非值为null。
default VcomputeIfPresent​(K key, BiFunction<? super K,​? super V,​? extends V> remappingFunction)If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.如果给定键的值是存在的和非空的,就尝试用给定的键和他当前映射的值计算一个新的映射。
default voidforEach​(BiConsumer<? super K,​? super V> action)Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.为每个地图的实体执行给定的动作直到所有实体处理完成或动作抛出异常。
default VgetOrDefault​(Object key, V defaultValue)Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.返回给定键映射的值,或者如果地图不包含该键的映射就返回默认值。
default Vmerge​(K key, V value, BiFunction<? super V,​? super V,​? extends V> remappingFunction)If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.如果给定键不关联一个值或者关联null,就用给定的非空值关联他。
VputIfAbsent​(K key, V value)If the specified key is not already associated with a value, associates it with the given value.如果给定键不关联一个值,就关联给定的值。
booleanremove​(Object key, Object value)Removes the entry for a key only if currently mapped to a given value.移除一个键映射到给定值的实体。
Vreplace​(K key, V value)Replaces the entry for a key only if currently mapped to some value.仅当一个键映射到一些值就替换此实体。
booleanreplace​(K key, V oldValue, V newValue)Replaces the entry for a key only if currently mapped to a given value.替代给定键的实体仅当键映射到给定的值。
default voidreplaceAll​(BiFunction<? super K,​? super V,​? extends V> function)Replaces each entry’s value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.用调用给定方法作用到每个实体的结果替代每个实体的值,直到所有 实体都处理完成或者方法抛出一个异常。

ConcurrentHashMap<K,​V>

简介

类。泛型。父类为AbstractMap<K,​V>。

构造器

构造器描述
ConcurrentHashMap()Creates a new, empty map with the default initial table size (16).创建一个默认初始表大小(16)的新的空的地图。
ConcurrentHashMap​(int initialCapacity)Creates a new, empty map with an initial table size accommodating the specified number of elements without the need to dynamically resize.创建一个新的空映射,初始表大小适应指定数量的元素,而无需动态调整大小。
ConcurrentHashMap​(int initialCapacity, float loadFactor)Creates a new, empty map with an initial table size based on the given number of elements (initialCapacity) and initial table density (loadFactor).创建一个新的空映射,初始表大小基于给定元素数量和初始表密度(加载因子)。
ConcurrentHashMap​(int initialCapacity, float loadFactor, int concurrencyLevel)Creates a new, empty map with an initial table size based on the given number of elements (initialCapacity), initial table density (loadFactor), and number of concurrently updating threads (concurrencyLevel).创建一个新的空映射,初始表大小基于给定元素数量,初始表密度(加载因子)和并发更新线程的数量(并发水平)。
ConcurrentHashMap​(Map<? extends K,​? extends V> m)Creates a new map with the same mappings as the given map.创建一个相似映射如给定的地图的新的地图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值