(4) Java data Structure--collection class and its data structure induction

Source: Internet
Author: User
Tags delete key set set

Java Collection class and its data structure induction-s Small Me-blog Park
Http://www.cnblogs.com/shidejia/p/6433788.html

The above diagram summarizes the inheritance structure of the Java Collection class, and here are some summary and feature descriptions of the collection classes:

Collection

Collection is an interface, a highly abstracted collection that contains the basic operations of a collection: Add, delete, empty, traverse, empty, get size, protect an element, and so on.
All subclasses of the collection Interface (direct and indirect subclasses ) must implement 2 constructors: Constructors with no parameters and constructors with parameters of collection. A constructor with parameters that can be used to convert the type of collection.

abstractcollection

Abstractcollection is an abstract class that implements functions other than iterator () and size () in collection. Because it implements most of the functions in the Collection interface, it is convenient for other classes to implement Collection, such as ArrayList, LinkedList, and so on, these classes want to implement Collection interface, the majority of interfaces have been implemented by inheriting abstractcollection.

List

List is an interface that inherits from collection.

List is an ordered queue, andeach element in the list has an index. The index value of the first element is 0, and the index value of the subsequent element is +1.

Unlike set,duplicate elements are allowed in the list.

Since list is inherited from the collection interface, it contains all the function interfaces in the collection, and since list is an ordered queue, it also has its own API interface. There are mainly "add, delete, get, modify elements at the specified location" ,"Get sub-queues in list " and so on.

The list has the Listiterator () method, andthe implementation class of the list implements the Listiterator () method, returning a Listiterator object.

Listiterator is an interface that inherits from iterator, which is a queue iterator. Designed to facilitate the List, providing forward /backward traversal. Compared to Iterator, it adds API interfaces for adding, having a previous element, getting the previous element, and so on .

Abstractlist

Abstractlist is an abstract class that inherits from the Abstractcollection and implements the list interface.

It implements a function other than size (), get (int location) in the list. Because it implements most of the functions in the list interface, it facilitates other classes to inherit the list. The iterator () interface is implemented in the Abstractlist abstract class compared to abstractlist and abstractcollection.

ArrayList

ArrayList inherits the Abstractlist abstract class and implements the list interface.

It is an array queue, equivalent to a dynamic array. Its capacity can grow dynamically compared to the array in Java.

ArrayList also implements the Randomaccess (provides random access functionality: The element object can be quickly obtained by the ordinal of the Element), cloneable (can be cloned), Java.io.Serializable (supports serialization, which can be serialized to transmit) these interfaces.

ArrayList is non-thread safe, so ArrayList is used in a single thread, and vectors or copyonwritearraylist can be selected in multiple threads.

LinkedList

LinkedList is a doubly linked list that inherits from Abstractsequentiallist and can be manipulated as a stack, a queue, or a double-ended queue.

LinkedList also implements lists, Deque (double-ended queues), cloneable (can clone), Java.io.Serializable (supports serialization, can be serialized to transmit) and other interfaces. The LinkedList is non-synchronous.

Vector

Vectors are vector queues, which are classes added by the JDK1.0 version. Inherited from the abstractlist, the implementation of the list (support related additions, deletions, modifications, traversal, etc.), randomaccess (random access function), cloneable (can be cloned) these interfaces.

Vectors actually store data through an array. When we construct VECOTR, the default capacity size of the vector is 10 if the default constructor is used.

When the vector capacity is insufficient to accommodate all elements, the vector's capacity increases. If the capacity increase factor is >0, the value of the capacity is increased by the "capacity increase factor"; otherwise, the capacity size is increased by one-fold.

The vector's clone function, which is to clone all the elements into an array. Unlike ArrayList, the operations in a vector are thread-safe.

Stack

Stack is a stack. Its characteristics are: Advanced post-Exit (FILO, first in the last out).

Stack inherits from vectors (vector queues), since vectors are implemented by arrays, which means that stacks are also implemented through arrays, not linked lists.

Set

Set is an interface that inherits from collection. Set is a collection of elements that have no duplicates.

Abstractset

Abstractset is an abstract class that inherits from the Abstractcollection and implements the set interface. It implements most of the functions in the set interface, thus facilitating the implementation of the set interface for other classes.

HashSet

HashSet is a collection of elements without duplicates, inherited from Abstractset, and implements a set interface.

It is implemented by HashMap, and the hashset contains a hashmap-type member variable Map,hashset operation function, which is actually implemented by map. HashMap does not guarantee the order of elements, and HashSet allows for the use of NULL elements. The HashSet is non-synchronous.

TreeSet

TreeSet is an ordered set that is implemented through TreeMap, TreeSet contains a member variable m of a navigablemap type, and M is actually an instance of TreeMap.

TreeMap inherits from Abstractset (with set properties and methods) abstract class, implements the Navigableset (supports a series of navigation methods, such as finding the most match with the specified target), cloneable (can be cloned), Java.io.Serializable (serialization is supported and can be transmitted by serialization.) When writing to the output stream, write "comparer, capacity, all elements", and then read the input stream in turn. Interface

TreeSet is based on the TREEMAP implementation. The elements in TreeSet support 2 sorting methods: Natural sorting or sorting according to the comparator provided when the TreeSet is created. This depends on the construction method used.

The TreeSet provides a guaranteed log (n) time overhead for basic operations (add, remove, and contains).

The TreeSet is non-synchronous, that is, not thread-safe. The iterator returned by its iterator method is fail-fast.

Map

Map is a key-value pair (key-value) mapping interface. The map map cannot contain duplicate keys, and each key can be mapped to at most one value.

The map provides an interface for returning a keyset, a value set, or a key-value mapping relationship set, respectively:

EntrySet () is used to return the set set of key-value sets;

KeySet () is used to return the set set of keyset;

Values () The collection collection of the return value set for the user.

Because the map cannot contain duplicate keys, each key can be mapped to at most one value. Therefore, the key-value set, the keyset is set, and the value set is collection.

Map provides methods such as key-value pair, get value by key, delete key, get capacity size, and so on.

Abstractmap

The Abstractmap class provides a backbone implementation of the map interface to minimize the work required to implement this interface.

SortedMap

SortedMap is an interface that inherits from the map interface. It is an ordered SortedMap key-value mapping.

There are two ways to sort sortedmap: natural sorting or user-specified comparators. All elements that insert an ordered SORTEDMAP must implement the comparable interface (or be accepted by the specified comparer).

Navigablemap

Navigablemap is an interface that inherits from SortedMap. It is a navigable set of key-value pairs with a navigation method that reports the closest match to a given search target.

Navigablemap provides methods for getting "key", "key-value pair", "keyset", "key-value pair set" respectively.

HashMap

HashMap is a hash table that stores content that is a key-value pair (key-value) mapping.

HashMap inherits from Abstractmap, and realizes the map, cloneable, java.io.Serializable interface.

The implementation of the HASHMAP is not synchronous, which means it is not thread-safe. Both its key and value can be null. In addition, the mappings in HashMap are not ordered.

TreeMap

TreeMap implements the SortedMap interface, it can save the record according to the key sort, the default is the key value of ascending order, you can also specify a sort of comparator, when using iterator traversal treemap, the resulting records are ordered.

TreeMap is based on red-black Trees (red-black tree). The mapping is sorted according to the natural order of its keys, or sorted based on the Comparator provided when the mapping was created, depending on the construction method used.

Hashtable

Hashtable similar to HashMap, Hashtable inherits from the dictionary class, implements the map interface, the difference is that it does not allow the record key or the value is empty; compared to HashMap, Hashtable is thread-synchronized, That is, only one thread can write hashtable at any one time, so it also causes the hashtable to be slower to write. And Hashtable can traverse through enumeration.

Linkedhashmap

Linkedhashmap saves the order in which records are inserted, and when traversing linkedhashmap with iterator, the first records must be inserted first. It can also be constructed with parameters, sorted by the number of applications. The traversal will be slower than HashMap, but there is an exception, when the HashMap capacity is large, the actual data is small, the traversal may be slower than Linkedhashmap, because the Linkedhashmap traverse speed is only related to the actual data, and capacity-independent, And HashMap's traverse speed is related to his capacity.

Enumeration and iterator comparison:

(01) Different function interfaces

Enumeration has only 2 function interfaces. With enumeration, we can only read the data of the collection, but not modify the data.

Iterator has only 3 function interfaces. Iterator data can be deleted in addition to the data that can be read from the collection.

Iterator supports the fail-fast mechanism, and enumeration does not support it.

Enumeration is the interface added by JDK 1.0. The functions used to it include vectors, Hashtable, and so on, all of which are added to JDK 1.0, and the purpose of enumeration exists is to provide them with traversal interfaces. The enumeration itself does not support synchronization, and when the vector, Hashtable implements enumeration, synchronization is added.

Iterator is the interface that JDK 1.2 adds, and it also provides traversal interfaces for collections such as HashMap, ArrayList, and so on. Iterator is supported by the FAIL-FAST mechanism: When multiple threads operate on the contents of the same collection, Fail-fast events can occur.

(4) Java data Structure--collection class and its data structure induction

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.