Java Collection Framework

Source: Internet
Author: User

The Java Collection Framework implements common basic data structures, such as: collections, linear tables, queues, stacks, mapping tables, and so on.

is the class diagram for the collection framework:

Java Collection framework is mainly composed of collection interface and map interface, collection interface has sub-interface list, Set, queue interface. Common collection classes are: ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap, and some legacy collection classes, such as vectors, stacks, hashtable, etc.

1. Linear table

There are two implementations of linear table, one is the sequential table ArrayList which can be randomly accessed, the other is the chain table LinkedList.

ArrayList is a sequential table that can access elements at random, and access elements are more efficient, but deleting elements requires moving a large number of elements, which is less efficient;

LinkedList is a doubly linked list, which can easily delete elements, but the access elements are inefficient and need to traverse the linked list;

2. Queues

The queue in Java is flexible and is a double-ended queue, where elements can be added and removed at the head and tail of the queue.

Queues also have two implementations, one is sequential queue arrayqueue, the other is a chain queue Linkedlist,linkedlist implements the Deque interface, which can be used as a chained table or as a chain queue.

Java also provides a class that implements the priority queue Priorityqueue,priorityqueue class always deletes the smallest element when the team header element is deleted. Priorityqueue does not sort the elements, but instead uses the heap to implement the minimum element each time it is returned. Priorityqueue requires the inserted element to implement the comparable interface.

3. Stack

Stacks are implemented using the Stack class, and the stack inherits from the vector class, so the stack in Java can actually be manipulated not only at the top of the stack, but can be deleted or added at any point.

4. Collection

Collections represent datasets that do not allow duplicate elements, and are implemented by the HashSet class and the TreeSet class.

HashSet is implemented using a hash table structure, as shown in the structure of the hash table:

In Java, the hash table is implemented using a list of linked lists, each linked list is called a bucket, and each bucket can hold several elements. When you add an element s hashcode hash to a hashset, select a bucket to save s, if there are no elements in the bucket, add s to the bucket directly, if there are elements in the bucket, you need to traverse the elements in the bucket and compare them, and then add s to the bucket if there are no elements that are the same as S.

TreeSet is also a hash table-based structure that, in addition to ensuring that there are no duplicate elements like HashSet, also establishes a relationship between all elements and uses a red-black tree to sort the elements in the collection so that the elements in the TreeSet can be traversed in sorted order.

5. Mapping table (MAP)

The mapping table is used to store key-value pairs, and Java uses the map interface to define the operation of the mapping table, noting that the map interface does not inherit from the collection interface. The mapping table is implemented by the HashMap class and the TreeMap class.

HashMap, similar to HashSet, is implemented using the structure of a hash table, except that the key-value pairs stored in the list are the Map.entry<k,v> class representations in Java. When you add a key value pair to the HashMap <k,v>, hash the K, select the bucket that holds <k,v>, if the bucket does not have a key-value pair corresponding to K, insert it directly, otherwise replace the old value with the new V.

Similar to TreeMap and TreeSet, the elements in the mapping table are sorted by key.

The mapping table can return 3 views: Keyset, value set, key-value pair set, returned by the following three methods:

Set<k> KeySet ();

Collection<v> values ();

Set<map.entry<k,v>> EntrySet ();

Note that the set object returned above is neither HashSet nor treeset, but rather an object of another class that implements the set interface, which masks some of the features that are not supposed to be on the view.

6. Linkedhashset and Linkedhashmap

Linkedhashset and Linkedhashmap can remember the order in which the elements are inserted, and they can be traversed in the order in which they were inserted, essentially creating a linked list on all elements of the hashtable structure to hold the order relationship between the elements.

Linkedhashmap can also choose to remember the order in which elements are accessed, as long as the parameter accessorder is set to True when the Linkedhashmap object is created. Each time a get or put on an element causes the element to end up in the linked list where the order relationship is saved, the element's actual position in the bucket does not change. When you use iterators to iterate over elements in Linkedhashmap, the preceding element is the "least recently used" element, so linkedhashmap can be used to implement a cache structure.

7. Vectors and ArrayList, Hashtable and HashMap

The vectors and Hashtable (note not Hashtable) classes are the legacy of the previous version.

Like the vector class and the ArrayList class, the vector class methods are synchronous and therefore thread-safe, hashtable classes and HashMap classes are similar, and Hashtable classes are thread-safe.

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.