Java Collection Framework

Source: Internet
Author: User

Java Collection Framework--refer to the blog Park excellent article collation

First, overview:

Located under the Java.util package, the Java Collection Framework is a set of tools that bring together common data structures and algorithms, making it easier and more efficient to process data.

Only objects can be saved in the collection (a reference variable that holds the object). (Arrays can hold both basic types of data and objects).

When we put an object into the collection, the system treats all the collection elements as instances of the object class. Since JDK1.5, this state has improved: You can use generics to constrain the type of elements in a collection and let the collection remember the types of all the collection elements

The Java collection framework is primarily derived from two interfaces: Collection and map,collection and map are the root interfaces of the Java Collection framework, which in turn contains some interfaces or implementation classes. That is, there are two kinds of containers in Java, one is single-valued collection, and the other is a map of stored key-value pairs.

Note:

Set, list, and map can be considered as the three main interfaces of a set frame.

The list collection is an ordered collection, and the elements in the collection can be repeated, and accessing the elements in the collection can be accessed based on the index of the element.

The set collection is an unordered collection, and the elements in the collection cannot be duplicated, and access to the elements in the collection can only be accessed based on the element itself (and the reason that the elements in the collection are not allowed to be duplicated).

The map collection holds elements of the key-value pair form, which can only be accessed by the key of each element when accessing its value.

For set, list, and map three collections, the most common implementation classes are HashSet, ArrayList, and HashMap, respectively, of three implementation classes.

Second, collection interface

The Collection:set and list interfaces are two sub-interfaces derived from the Collection interface. A collection of single individuals that can contain only one element per unit.

Collection Basic Operation:

add element Add (Object obj); AddAll (Collection c);

Remove the element remove (Object obj); RemoveAll (Collection c);

Finding the intersection Retainall (Collection c);

Remove the element remove (Object obj); RemoveAll (Collection c);

Finding the intersection Retainall (Collection c);

A good way to access/traverse a collection element is to use the iterator interface (iterators are used to replace enumeration)

1 Interface  23    4void      5  6 }  
View Code

Set Set
--No collection of duplicate items

(There are two commonly used: HashSet and TreeSet, the internal implementation is a map, its elements are equivalent to the key in the map, and value is an object constant, all operations on the set are finally transferred to the operation of the map)

Hashset-based on a hash list of the elements to be added to the hash list to implement the Hashcode () method

Checklist List

--a set of positions. Elements added to the list can be added to a specific position in the list or added to the end

ArrayList: The data list, in fact, encapsulates an array, so it's very fast to access

ArrayList are similar to vectors and are used to scale array maintenance collections.

ArrayList and Vector differences:

(1) synchronization: Vector is thread-safe, that is, synchronous, and ArrayList is the line program is not secure, not synchronous
(2) Data growth: When growth is needed, vectors grow by default to the original, while ArrayList is half the original

LinkedList: The internal implementation is the leading node of the doubly linked list, suitable for in the middle of the list need to frequently insert and delete operations

Use Addlist () to add Element Removefirst () to delete elements in FIFO
Used in Filo, with AddFirst ()/removelast ()

The MAP:MAP implementation class is used to hold data that has a mapping relationship (Key-value). A collection of pairs of individuals with associations, each of which is a pair of associated elements.

Third,map interface

Mapping Table Map

--for keyword/value pairs, like a dictionary

Three collections for working with maps

Keyword set keyset ()

Value set Value ()

Project Set Enryset ()

Four specific versions

Common mapping table for hashmap-hash list

linkedhashmap-extension HashMap, maintaining the insertion order when iterating back the collection

weakhashmap-A mapping table based on a weakly-referenced hash list, if a reference to a keyword outside the map table is not persisted, the memory collector reclaims it

treemap-mapping table based on the balance tree

Iv. Related knowledge:

Iterators (Iterator)

Provides a way to access individual elements in a container (container) object without exposing the object's internal details.

Iterator mode is the preferred way to traverse all the elements in a container

Collection defines the iterator<e> iterator () method, the subclasses each implement the method, we call directly to

Although there is no definition in map, we can use the iterator () method of the Map.entryset ()

Java Collection Framework

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.