Common data Structures in Java: List and map-how the bottom layer is implemented

Source: Internet
Author: User
Tags comparable

1: Set 2 Collection (Single column collection) 3 List (ordered, repeatable) 4 ArrayList 5 The underlying data structure is an array, query fast, delete and subtract slowly 6             Thread insecure, high efficiency 7 Vector 8 The underlying data structure is an array, fast query, delete and deletion slow 9 thread safety, low efficiency 10 LinkedList 11 The underlying data structure is linked list, query slow, and delete fast 12 thread unsafe, high efficiency Set (unordered, unique) Hashse T 15 the underlying data structure is a hash table.                         16 hash Table Dependent two methods: Hashcode () and equals () 17 execution order: 18 First Judge whether the Hashcode () value is the same 19                             Yes: Continue with equals () to see its return value 20 is true: The description element is duplicated, not added 21 is false: Add directly to Collection 22 No: Add directly to Collection 23 final: 24 automatically generates Hashcode () and Equals ( ) Linkedhashset 27 The underlying data structure consists of a linked list and a hash table. 28 The chain list ensures that the elements are ordered. 29 The element is guaranteed to be unique by a hash table. TreeSet 31 The underlying data structure is a red-black tree. (is a self-balancing two-fork tree) 32 How to guarantee the uniqueness of elements? 33 based on whether the return value of the comparison is 34 how to guarantee the ordering of the elements? 352 Ways 36 Natural sort (elements are comparative) 37 let the class that the element belongs to implements the comparable interface         38 Comparator sort (set with comparison) 39 Let the collection receive a comparator implementation class object. MAP (double column collection) 41 The data structure of the A:map collection is only valid for the key, regardless of the value. B: A key-value pair is stored as an element, the key is unique, and the value is repeatable. The HASHMAP 45 underlying data structure is a hash table. Thread insecure, high efficiency 46 hash table relies on two methods: Hashcode () and Equals () 47 execution order: 48 First Judge Hashcode () value                             Whether the same 49 is: Continue to execute equals (), see its return value 50 is true: The description element is duplicated, not added 51 is false: added directly to the collection 52 No: Added directly to the collection 53 final: 54 automatically generated hashcode () and Equals () can be linkedhashmap 56 The underlying data structure consists of a linked list and a hash table. 57 The chain list ensures that the elements are ordered. 58 The element is guaranteed to be unique by a hash table. HashtabThe underlying data structure of Le 60 is a hash table. Thread safety, low efficiency 61 hash table relies on two methods: Hashcode () and Equals () 62 execution order: 63 First Judge Hashcode () value is                             No same 64 is: Continue to execute equals (), see its return value 65 is true: The description element is duplicated, not added 66 is false: Add directly to the collection 67 No: Add directly to the collection 68 final: 69 Automatically generate Hashcode ( ) and Equals () can be TREEMAP 71 the underlying data structure is a red-black tree. (is a self-balancing two-fork tree) 72 How to guarantee the uniqueness of the element? 73 based on whether the return value of the comparison is 74 how to guarantee the ordering of the elements? 752 Ways 76 Natural sort (elements are comparative) 77 let the class that the element belongs to implements the comparable interface      78 Comparator sort (set with comparison) 79 let the collection receive a comparator implementation class object 80 81 2. About Collection Selection principle 82 83 is the key-value object form: 84 is: The MAP 85 key needs to be sorted: 86 Yes: TreeMap 87 No: HASHMAP 88 don't know, just use HashMap. 89 90 No: CollectThe Ion 91 element is unique: 92 is: Whether the Set 93 element needs to be sorted: 94 Yes: TreeSet 95 No: HashSet 96 do not know, use HashSet 97 98 No: Li                             St 99 to be safe: 100 Yes: Vector101 No: ArrayList or LinkedList102 Delete more: LinkedList103 query more: ArrayList104 do not know, use         ArrayList105 don't know, just use ArrayList106 107 3: Common methods of collection and Traverse mode 108 collection:109 Add () 110              Remove () 111 contains () iterator () 113 size () 114 115 times calendar: 116 Enhanced for117                 Iterator 118 119 |--list120 Get () 121 122 times calendar: 123 Normal for124 |--set125 126 map:127 put () () () 129 containskey (), Containsval UE () KeySet ()131 get () 133 value () EntrySet () 134 size () 135 136 times calendar: 137 by key              Find value 138 find the key and value 139 for the object based on the key value

1: Set (Own)

Collection (Single-column collection)

List (ordered, repeatable)

ArrayList the underlying data structure is an array, query fast, delete slow thread unsafe, high efficiency vector data structure is an array, query fast, delete slow thread safety, low efficiency linkedlist the underlying data structure is linked list, query slow, delete fast thread unsafe, high efficiency set (unordered, Only

HashSet the underlying data structure is a hash table. The hash table relies on two methods: Hashcode () and Equals () execution order: first to determine whether the hashcode () value is the same: Continue to execute equals (), and see that its return value is true: The description element is repeated, not added is false: Add directly to the collection no: Add directly to the collection final: Automatically generate Hashcode () and Equals () to linkedhashset the underlying data structure consists of a linked list and a hash table. The chain list guarantees an orderly element. The element is guaranteed to be unique by a hash table.

TreeSet the underlying data structure is a red-black tree. (is a self-balancing two-fork tree) How to guarantee the uniqueness of the element?

Determines how the element is ordered based on whether the return value of the comparison is the maximum? Two ways of natural ordering (the elements are comparable) to allow the class to which the element belongs implements the comparable interface comparer ordering (the collection is comparable) let the collection receive a comparator implementation class object map (Double column collection) The data structure of the A:map collection is only valid for the key, regardless of the value. B: The elements stored in the form of a key-value pair, the key is unique, and the value is repeatable.

HashMap the underlying data structure is a hash table. Thread insecure, high efficiency hash table relies on two methods:

Hashcode () and Equals () execution order: first determine whether the hashcode () value is the same: Continue to execute equals (), and see that its return value is true: The description element is repeated, not added is false: Add directly to the collection no: Add directly to the collection final: Automatically generate Hashcode () and Equals () to linkedhashmap the underlying data structure consists of a linked list and a hash table. The chain list guarantees an orderly element. The element is guaranteed to be unique by a hash table.

Hashtable the underlying data structure is a hash table. Thread-safe, inefficient hash table relies on two methods:

Hashcode () and Equals () execution order: first determine whether the hashcode () value is the same: Continue to execute equals (), and see that its return value is true: The description element is repeated, not added is false: Add directly to the collection no: Add directly to the collection final: Automatically generate Hashcode () and Equals () to treemap the underlying data structure is a red-black tree. (Is it a self-balancing two-fork tree) How to guarantee the uniqueness of the element? Based on whether the return value of the comparison determines how the order of the elements is guaranteed? Two natural sorts (the element is comparative) to allow the class of the element to implement the comparable interface comparer ordering (the collection is comparable) Let the collection receive a comparator implementation class object

2: In the end use that kind of collection (own) to see the demand.

is the key-value object form: Yes: The map key needs to be sorted: Yes: TreeMap No: HashMap don't know, use HashMap. No: The collection element is unique: Yes: The set element needs to be sorted: Yes: TreeSet No: hashset do not know, use HashSet No: List to be safe: Vector (in fact, we do not use it, after we explain the multithreading, I'm giving you a review of WHO to use) No: ArrayList or LinkedList and more: LinkedList query more: ArrayList do not know, use ArrayList do not know, use ARRAYLIST3: Common methods of collection and traversal mode Collection:add () Remove () contains () iterator () size () Traversal: Enhanced for iterator |--listget () Traversal: normal For|--setmap:put () Remove () ContainsKey (), Containsvalue () KeySet () get () value () EntrySet () size () Traversal: Find values based on key values for key and value jobs: Any collection I've explained, I ask you to store what you are able to store. And, you have to be able to traverse it.

4:arraylist,linkedlist,hashset,hashmap (mastering) storing strings and custom object data and traversing 5: nested Traversal of a collection (understanding)

Note:

1. The Arralist code contains a large number of system.arraycopy () methods used to replicate the array

System.arraycopy (Elementdata, index+1, Elementdata, Index,
nummoved);

Common data Structures in Java: List and map-how the bottom layer is implemented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.