Java collections, you really know it?

Nature collection

集合的主要作用是存储对象的容器。

Familiar containers (collection) class, there ArrayList, HashMap, HashSet. Etc. I believe when you go to the interview, the interviewer will often ask ArrayList, LinkedList how to choose? HashMap underlying data storage organization can talk about it in detail? Other issues
that Why the number of containers it? or say why the interviewer cares about these problems?从我的理解来看,因为不同的集合容器类,他的特点不一样,或者说不同的集合类可以基于不同的场景进行选择。集合(容器)的底层是由于数据组织的方式不一样。或者这么理解:出现不同的集合是因为他们的数据结构不一样,从而出现了不同的特性。

data structure

Arrays:
Features:
1. Continuous space requirements
2. Under the subject can be accessed by members of the high performance index access (indexed)
bring greater performance overhead 3. additions and deletions operation (data to ensure that cross-border issues, to be dynamic expansion )
typical JAVA collection classes ArrayList
Here Insert Picture Description
list (two-way, for example)
features:
1. flexible space requirements, storage space does not require continuous
2 does not support access to the underlying support traversal search order (so there is a problem:. LinkedList has subscript access get (index) method, how is it? LinkedList follow explain in detail)
3. find the corresponding node point to change the beginning and end of the list for additions and deletions operation, the storage location without moving elements
typical Java collection classes LinkedList
Here Insert Picture Description
tree (a binary search tree)
features:
1. flexible space requirements, does not require continuous storage space, can be arranged in a specified order
2. do not support access to the underlying support approximately half way to find access to
3. deletions lookup operation of a node, the operation there is also balanced tree after assurances
4. the reference relationship deletions node changes, without storing the location of the mobile element
typical Java collection class TreeMap
Here Insert Picture Description

Factions overall understanding of the collection

Java 中集合类分为 2 大主要派系:Collection 和 Map

Collection factions

Here Insert Picture Description

Collection 是一个接口,是高度抽象出来的集合,它包含了集合的基本操作和 属性。

Collection 实现了 Iterable 接口来完成集合中元素的迭代遍历能力 Iterator iterator();
Collection 包含了 List 和 Set 两大分支。
1.List 是一个有序的队列,每一个元素都有它的索引。List 的实现类有 LinkedList, ArrayList, Vector。
2.Set 是一个不允许有重复元素的集合。Set 的实现类有 HastSet 和 TreeSet。(注意:HashSet 依赖于 HashMap。它实际上是通过 HashMap 实现的;TreeSet 依赖于 TreeMap,它实际上是通过 TreeMap 实现的。)
Iterator:

Iterator 是一个接口,它是集合的迭代器。集合可以通过 Iterator 去遍历集合 中的元素。Iterator 提供的 API
接口,包括:是否存在下一个元素、获取下一 个元素、删除当前元素
abstract boolean hasNext()
abstract E next()
abstract void remove()

Map 派系

Here Insert Picture Description

Map 是一个映射接口即 key-value 键值对

1.Map 提供接口分别用于返回键集、值集或键-值映射关系集。
entrySet()用于返回 键-值集的Set 集合
keySet()用于返回键的Set 集合
values()用户返回值集的Collection 集合
注意:因为 Map 中不能包含重复的键;每个键最多只能映射到一个值。所以,键-值集、键集都是 Set,值在 Map 中是可以允许重复的所有值集是Collection。
2.Map 提供了“键-值对”、“根据键获取值”、“删除键”、“获取容量大小”等基 本方法。
V get(Object key);
V remove(Object key);
int size();、

Map 派系与 Collection 派系有什么关系?

We should have carried out with HashMap HashMap of key and value for the operation of traversing it specific use, such as:.
HashMapObj.keySet (); Gets a collection of all the keys
hashMapObj.values (); Gets a collection of all the values
so finishing speaking factions Map . collection factions need to rely on
in our branch collection of Set, HashSet and TreeSet is dependent on HashMap and TreeMap be achieved (specific analysis in subsequent Set collection classes) so that 两大集合派系是相辅相成的the final illustration, we can look:
Here Insert Picture Description

Published 17 original articles · won praise 29 · views 6541

Guess you like

Origin blog.csdn.net/weixin_45240169/article/details/104210633