list and map - how the bottom layer is implemented

1: Collection
  2 Collection (single-column collection)
  3 List (ordered, repeatable)
  4             ArrayList
  5 The underlying data structure is an array, the query is fast, the addition and deletion are slow
  6 Thread unsafe, high efficiency
  7             Vector
  8 The underlying data structure is an array, fast query, slow addition and deletion
  9 thread-safe, inefficient
 10             LinkedList
 11 The underlying data structure is a linked list, the query is slow, and the addition and deletion are fast
 12 Thread unsafe, efficient
 13 Set (unordered, unique)
 14             HashSet
 15 The underlying data structure is a hash table.
 16 Hash tables rely on two methods: hashCode() and equals()
 17 Execution order:
 18 First determine whether the hashCode() value is the same
 19 is: continue to execute equals() and see its return value
 20 is true: the element is repeated and not added
 21 is false: just add it directly to the collection
 22 No: just add to the collection directly
 23 Finally:
 24 Automatically generate hashCode() and equals()
 25                     
 26                 LinkedHashSet
 27 The underlying data structure consists of linked lists and hash tables.
 28 Elements are guaranteed to be ordered by a linked list.
 29 Elements are guaranteed to be unique by a hash table.
 30             TreeSet
 31 The underlying data structure is a red-black tree. (is a self-balancing binary tree)
 32 How to ensure element uniqueness?
 33 Determined according to whether the return value of the comparison is 0
 34 How to ensure the ordering of elements?
 35 Two ways
 36 Natural sorting (elements are comparative)
 37 Make the class to which the element belongs implements the Comparable interface
 38 Comparator sorting (sets are comparable)
 39 Let the collection receive a Comparator implementation class object
 40 Map (two-column collection)
 41 A: The data structure of the Map collection is only valid for the key, and has nothing to do with the value.
 42 B: Elements in the form of key-value pairs are stored, with unique keys and repeatable values.
 43         
 44         HashMap
 45 The underlying data structure is a hash table. Thread unsafe, efficient
 46 Hash tables rely on two methods: hashCode() and equals()
 47 Execution order:
 48 First determine whether the hashCode() value is the same
 49 is: continue to execute equals() and see its return value
 50 is true: the element is repeated and not added
 51 is false: just add it directly to the collection
 52 No: just add to the collection directly
 53 Finally:
 54 Automatically generate hashCode() and equals()
 55 LinkedHashMap
 56 The underlying data structure consists of linked lists and hash tables.
 57 Elements are guaranteed to be ordered by a linked list.
 58 Elements are guaranteed to be unique by the hash table.
 59         Hashtable
 60 The underlying data structure is a hash table. thread-safe, inefficient
 61 Hash tables rely on two methods: hashCode() and equals()
 62 Execution order:
 63 First determine whether the hashCode() value is the same
 64 is: continue to execute equals() and see its return value
 65 is true: the element is repeated, not added
 66 is false: just add it directly to the collection
 67 No: just add to the collection directly
 68 Finally:
 69 Automatically generate hashCode() and equals()
 70         TreeMap
 71 The underlying data structure is a red-black tree. (is a self-balancing binary tree)
 72 How to ensure element uniqueness?
 73 Determined according to whether the return value of the comparison is 0
 74 How to ensure the ordering of elements?
 75 two ways
 76 Natural sorting (elements are comparative)
 77 Make the class to which the element belongs implements the Comparable interface
 78 Comparator sorting (sets are comparable)
 79 Let the collection receive a Comparator implementation class object
 80     
 81 2. About the set selection principle
 82     
 83 Is it in the form of a key-value object:
 84 is: Map
 Does the 85 key need to be sorted:
 86 is: TreeMap
 87                 否:HashMap
 88 Don't know, just use HashMap.
 89             
 90         否:Collection
 91 Is the element unique:
 92 is: Set
 93 Whether elements need to be sorted:
 94 is: TreeSet
 95                         否:HashSet
 96 Don't know, use HashSet
 97                     
 98 No: List
 99 To be safe:
100 is: Vector
101 No: ArrayList or LinkedList
102 Additions and deletions: LinkedList
103 Multiple queries: ArrayList
104 Don't know, just use ArrayList
105 Don't know, just use ArrayList
106             
107 3: Common methods of collections and traversal methods
108     Collection:
109         add()
110         remove()
111         contains()
112         iterator()
113         size()
114         
115 Traversal:
116 Enhanced for
117 Iterators
118             
119         |--List
120             get()
121             
122 traversal:
123 Normal for
124         |--Set
125     
126     Map:
127         put()
128         remove()
129         containskey(),containsValue()
130         keySet()
131         get()
132         value()
133         entrySet()
134         size()
135         
136 traversal:
137 Find value by key
138 Find the key and value according to the key-value pair object
139             
复制代码
复制代码

 

1: Collection (complete by yourself)

Collection (single-column collection)

List (ordered, repeatable)

The underlying data structure of ArrayList is an array, the query is fast, the addition and deletion are slow, the thread is unsafe, and the efficiency is high. The vector underlying data structure is an array, the query is fast, and the addition and deletion are slow. , efficient Set (unordered, unique)

The underlying data structure of HashSet is a hash table. The hash table relies on two methods: hashCode() and equals() Execution order: first determine whether the hashCode() value is the same: continue to execute equals(), see if the return value is true: it means that the element is repeated, and it is false if it is not added: Just add it directly to the set No: just add it directly to the set Final: automatically generate hashCode() and equals() to link the underlying data structure of LinkedHashSet is composed of linked list and hash table. The order of elements is guaranteed by the linked list. Elements are guaranteed to be unique by a hash table.

The underlying data structure of TreeSet is a red-black tree. (It is a self-balancing binary tree) How to ensure the uniqueness of elements?

       How to ensure the ordering of elements is determined according to whether the return value of the comparison is 0? Two ways of natural ordering (elements are comparable) let the class to which the element belongs implement the Comparable interface Comparable sorting (collections are comparable) let the collection receive a Comparator The implementation class object Map (two-column collection) A: The data structure of the Map collection is only valid for the key, and has nothing to do with the value. B: Elements in the form of key-value pairs are stored, with unique keys and repeatable values.

The underlying data structure of HashMap is a hash table. Thread-unsafe, efficient hash tables rely on two methods:

  hashCode()和equals()执行顺序:首先判断hashCode()值是否相同是:继续执行equals(),看其返回值是true:说明元素重复,不添加是false:就直接添加到集合否:就直接添加到集合最终:自动生成hashCode()和equals()即可LinkedHashMap底层数据结构由链表和哈希表组成。由链表保证元素有序。由哈希表保证元素唯一。

Hashtable底层数据结构是哈希表。线程安全,效率低哈希表依赖两个方法:

  hashCode()和equals()执行顺序:首先判断hashCode()值是否相同是:继续执行equals(),看其返回值是true:说明元素重复,不添加是false:就直接添加到集合否:就直接添加到集合最终:自动生成hashCode()和equals()即可TreeMap底层数据结构是红黑树。(是一种自平衡的二叉树)如何保证元素唯一性呢?根据比较的返回值是否是0来决定如何保证元素的排序呢?两种方式自然排序(元素具备比较性)让元素所属的类实现Comparable接口比较器排序(集合具备比较性)让集合接收一个Comparator的实现类对象

2:到底使用那种集合(自己补齐)看需求。

     是否是键值对象形式:是:Map键是否需要排序:是:TreeMap否:HashMap不知道,就使用HashMap。否:Collection元素是否唯一:是:Set元素是否需要排序:是:TreeSet否:HashSet不知道,就使用HashSet否:    List要安全吗:是:Vector(其实我们也不用它,后面我们讲解了多线程以后,我在给你回顾用谁)   否:ArrayList或者LinkedList增删多:LinkedList查询多:ArrayList不知道,就使用ArrayList不知道,就使用ArrayList3:集合的常见方法及遍历方式Collection:add()remove()contains()iterator()size()遍历:增强for迭代器|--Listget()遍历:普通for|--SetMap:put()remove()containskey(),containsValue()keySet()get()value()entrySet()size()遍历:根据键找值根据键值对对象分别找键和值作业:我讲解过的任意一个集合,我要求你存储什么,你就能够存储什么。并且,还要能够遍历出来。

4:ArrayList,LinkedList,HashSet,HashMap(掌握)存储字符串和自定义对象数据并遍历5:集合的嵌套遍历(理解)

 

注:

1.其中的Arralist  代码中大量的用了System.arraycopy () 方法 进行数组进行复制

System.arraycopy(elementData, index+1, elementData, index,
numMoved);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325992835&siteId=291194637