Collection of essays in Java

Source: Internet
Author: User

The previous picture: about the Collection interface

First, collection in the common functions:

Boolean Add (Object e): Adding elements to the collection
void Clear (): empties all elements in the collection
Boolean contains (Object O): Determines whether an element is contained in the collection
Boolean IsEmpty (): Determines whether the elements in the collection are empty
Boolean remove (Object o): Deletes an element based on the content of the element
int size (): Gets the length of the collection
Object[] ToArray (): Ability to convert collections into arrays and store elements in the collection

Because collection is an interface, it cannot be instantiated

So Collection C = new Collection (); This is a bad notation.

Second, the list sub-system features:

A: Ordered (the order of storage and reading is consistent)
B: There is an integer index
C: Allow duplicate

Unique features of the 1.List:

void Add (int index, E Element): Adds an element to the index position
E get (int index): Gets an element based on index indices
E Remove (int index): Deletes elements based on index
e Set (int index, E Element): Sets the element for index position at index

(1) LinkedList unique features:

LinkedList the underlying use of the linked list structure, so the deletion is fast, the query relative ArrayList slower
void AddFirst (e e): Adding elements to the head of the linked list
void AddLast (e e): adding elements to the tail of the list
E GetFirst (): Gets the element of the chain head without deleting the element
E getlast (): Gets the element at the end of the chain, without deleting the element
E Removefirst (): Returns the element of the chain head and removes the element of the chain header
E removelast (): Returns the element at the end of the chain and deletes the element at the end of the chain

Three, the characteristics of the set interface:

Collection of Set Systems:
A: The order in which the collections are stored and the order in which they are taken is inconsistent
B: No Index
C: The elements stored in the collection are not duplicated

1.HashSet Uniqueness Principle:

Rule: Elements that are newly added to the HashSet collection are compared to existing elements in the collection
The hash value is first compared (each element will call Hashcode () to produce a hash value)
If the newly added element differs from the hash value of an existing element in the collection, the newly added element is stored in the collection
If the newly added element is the same as the hash value of an existing element in the collection, you also need to call the Equals (Object obj) comparison
If the Equals (Object obj) method returns true to indicate that the newly added element is the same as the property value of an element already in the collection, the newly added element is not stored in the collection
If the Equals (Object obj) method returns false, stating that the newly added element is different from the property value of an existing element in the collection, the newly added element is stored in the collection

Four. Map Collection 1.Map Interface overview

Collections in A:collection, where elements are isolated (understood to be single), stored as elements in the collection
A collection of b:map in which elements are paired (understood as couples). Each element consists of a key and a value, which can be used to find the corresponding value.
A collection in C:collection is called a single-column collection, and a collection in map is called a double-row collection.
It is important to note that the collection in the map cannot contain duplicate keys, the values can be repeated, and each key can only correspond to one value.

2.Map Common function A: mapping function:

V Put (K key, v value): Deposit to map collection as Key = value

B: Get Features:

V get (Object key): Gets the value based on the key
int size (): Returns the number of key-value pairs in the map

C: Judging function:

Boolean ContainsKey (Object key): Determines whether the Map collection contains key-value pairs with key keys
Boolean Containsvalue (Object value): Determines whether the map collection contains values that are value-key-value pairs
Boolean IsEmpty (): Determines if there are no key-value pairs in the Map collection

D: Delete function:

void Clear (): Clears all key-value pairs in the Map collection
V Remove (Object key): Deletes a key value pair from the map based on the key value

E: Traversal function:

Set<map.entry<k,v>> EntrySet (): Encapsulates each key-value pair into a Entry object, and then encapsulates all Entry objects into the set collection to return
Set<k> keySet (): Load all the keys in the map into the set collection to return
Collection<v> VALUES (): Returns a collection of the values of all value in the collection

Two ways to traverse 3.MAP

Collection of essays in Java

Related Article

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.