Collections - source code analysis



1. What is the set

  • Container is used to store a set of data (also arrays), but can store different sets of different types of objects, and the size of the variable
  • Its common types of Set, List, Map. These types of commonly used to extract up there and Map Collection Interface


1.1 Collection interface methods

add(E):添加一个对象
addAll(Collection<? extengds E>):添加指定集合里的全部对象

clear():清空集合
remove(Object):移除一个对象
removeAll(Collection<?>):移除集合里的全部对象

contains(Object):是否包含某个对象
containsAll(Collection<?>):是否包含某集合的全部对象
isEmpty():集合是否为空
size():集合对象的个数
retainAll(Collection<?>):交集,结果放在调用方法的集合

iterator():获取迭代器


1.2 Iterator iterator

Collection Iterable interface extends the interface, and the interface has Iterable iterator () method, which returns an iterator (for traversing collection)


1.3 iterator is an interface, there are four methods


1.4 Map interface methods

containsKey(Object):是否包含该Key
containsValue(Object):是否包含该Value
get(Object):根据Key获取Value
put(K,V):添加一个键值对
remove(Object):根据Key移除一个键值对




2. A set of three common types


A. List-- orderly, repeatable

1. ArrayList (bottom layer is an array)

  • add is the most commonly used method, we take a look at the source code


  • get


  • set


  • remove


  • toArray






2. LinkedList (bottom is a doubly linked list)

  • add international practice, look at the source code


  • remove

List implementation illustrating remove elements


  • get


  • set






Two. Map

Unlike collection Map Collection, Map is stored in key-value pairs, and the key can not be repeated

1 .HashMap (underlying hash tables, arrays, linked lists implemented using Java, disorder)

  • put (corresponding to the Collection add)


  • get

  • remove the same way also the lazy hold out


2. TreeMap (underlying red-black tree, sorted by Comparator)

  • put

1575711596293

  • get

1575712773581

  • remove

1575712822192

Three. Set-- disorder, can not be repeated

1. HashSet

  • Note that the constructor, the bottom with a HashMap, and Set collections stored is not the key to how to do?

  • Set the value stored in the Map keys inside, and all values ​​Map Object storage with a

HashMap and the rest are the same, not repeat them

2. TreeSet

  • TreeMap and more consistent, too lazy to say




Guess you like

Origin www.cnblogs.com/Howlet/p/12003389.html