Dark Horse Programmer-----The Java Foundation-----Collection (you will learn to assemble these after reading them)

Source: Internet
Author: User
Tags comparable

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------


System Learning Collection

One: The system of the set
Collection (Single-column collection)
List (ordered, repeatable)
ArrayList
The underlying data structure is an array, query fast, and delete slowly
Thread insecure, high efficiency
Vector
The underlying data structure is an array, query fast, and delete slowly
Thread-safe, low-efficiency
LinkedList
The underlying data structure is linked list, query slow, delete quickly
Thread insecure, high efficiency
Set (unordered, unique)
HashSet
The underlying data structure is a hash table.
A hash table relies on two methods: Hashcode () and Equals () to guarantee uniqueness.
Execution order:
First determine if the hashcode () value is the same
Yes: Continue with equals () to see its return value
Is true: The description element is duplicated, not added
is false: It is added directly to the collection
No: Add directly to the collection
Eventually:
Automatic generation of hashcode () and Equals () via shortcut keys

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 elements?
Depends on whether the return value of the comparison is
How to ensure the ordering of elements?
Two different ways
Natural sorting (elements are comparative)
Implementing the comparable interface for the class to which the element belongs
Comparator sorting (collection with comparison)
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 if the hashcode () value is the same
Yes: Continue with equals () to see its return value
Is true: The description element is duplicated, not added
is false: It is added directly to the collection
No: Add directly to the collection
Eventually:
Automatically generate Hashcode () and Equals ()
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, low-efficiency
Hash table relies on two methods: Hashcode () and Equals ()
Execution order:
First determine if the hashcode () value is the same
Yes: Continue with equals () to see its return value
Is true: The description element is duplicated, not added
is false: It is added directly to the collection
No: Add directly to the collection
Eventually:
Automatically generate Hashcode () and Equals ()
TreeMap
The underlying data structure is a red-black tree. (is a self-balancing two-fork tree)
How to guarantee the uniqueness of elements?
Depends on whether the return value of the comparison is
How to ensure the ordering of elements?
Two different ways
Natural sorting (elements are comparative)
Implementing the comparable interface for the class to which the element belongs
Comparator sorting (collection with comparison)
Let the collection receive a comparator implementation class object

Two: What kind of collection do you use?
Look at the demand.

Is the form of a key-value object:
Yes: Map
Whether the key needs to be sorted:
Yes: TreeMap
No: HashMap
Don't know, just use HashMap.

No: Collection
Element is unique:
Yes: Set
Whether the element needs to be sorted:
Yes: TreeSet
No: HashSet
I don't know, just use HashSet.

No: List
Is it safe to:
is: Vector (in fact, we do not use it, after we explain the multi-threaded, I will give you a review of WHO to use)
No: ArrayList or LinkedList
More additions and deletions: LinkedList
Query MORE: ArrayList
I don't know, just use ArrayList.
I don't know, just use ArrayList.
So what we use most is HashMap, HashSet, ArrayList collection.

3: Common methods of collection and traverse mode
Collection:
A: Add Features
Boolean Add (Object obj); add an Element
Boolean AddAll (Collection c); Add a collection element
B: Delete Feature
void clear (); Remove all elements
Boolean remove (Object o); Remove an element
Boolean RemoveAll (Collection c); Removes all elements from a collection
C: Judging function
A Boolean contains (Object O) that determines whether the specified element is contained in the collection
Boolean containsall (Collection c); Determines whether the collection contains the specified collection
Boolean isEmpty (); Determines whether the collection is empty.
D: Get Features
Iterator<e> Iterator (); an iterator that is used to traverse.
E: Length function
int size (); Gets the length of the number of elements in the collection

Traverse:
Enhanced for
Iterators

|--list
In addition to having collections functions, it has its own unique functions.
Get features
Object get (int index); Gets the element at the specified position

Traverse:
Enhanced for
Iterators
Common for:size () and get () methods combine
|--set
Features with collections


Traverse:
Enhanced for
Iterators

MAP:
A: Add Features
V put (K key,v value): Adds an element.
If the key is stored for the first time, the element is stored directly, returning NULL
If the key is not stored for the first time, replace the previous value with a value and return the previous value.
B: Delete Feature
void Clear (); Removes all key-value pairs of elements, this method is not commonly used.
V Remove (Object key): deletes the corresponding element according to the key and returns the corresponding value.
C: Judging function
Boolean ContainsKey (Object key): Determines whether the collection contains the specified key.
A Boolean Containsvalue (Object value) that determines whether the collection contains the specified value
Boolean isEmpty (); Determines whether the collection is empty.
D: Get Features
Set<map.entry<k,v>> EntrySet ();
V get (Object key); Get value by key
Set<k> KeySet (); Gets the collection of all the keys in the collection
Collection<v> value (); Gets the collection of all values in the collection.
E: Length function
int size (); Returns the number of key-value pairs in the collection.

Traverse:
Find a value based on a key (equivalent to a husband looking for a wife)

Search for keys and values based on key values (equivalent to finding husbands and wives based on marriage certificates)


Dark Horse Programmer-----The Java Foundation-----Collection (you will learn to assemble these after reading them)

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.