Java Collection Framework (common data structures)

Source: Internet
Author: User
Tags shallow copy

Before Java 2, Java provided the ad hoc class. For example: vectors, stacks, dictionaries (Dictionary), hash tables (Hashtable) These classes (data structures) are used to store and manipulate groups of objects. Although these classes are useful, they lack a central, unified theme. A collection framework is a uniform standard architecture that is defined for representing and manipulating collections. In addition to collections, the framework (framework) also defines several map interfaces and classes. A key/value pair is stored in the map. Although maps are not collections, they are fully integrated in the collection.

All the collection frameworks include the following:

  • interface: is an abstract data type that represents a collection. The interface allows the collection to manipulate the details of its representatives independently. In object-oriented languages, interfaces usually form a hierarchy.
  • implementation (Class): is the specific implementation of the collection interface. Essentially, they are reusable data structures.
  • algorithm: is a useful calculation for the execution of methods in an object that implements a collection interface, for example: Search and sort. These algorithms are called polymorphic, because the same approach can have different implementations on similar interfaces.

The classes and interfaces of the collection framework are in the Java.util package.

is a simplified collection framework diagram:

Collection interface

The entire collection framework is designed around a standard set of interfaces. You can use the standard implementations of these interfaces directly, such as LinkedList, HashSet, and TreeSet, and you can implement your own collections via these interfaces. The specific interface and its overview are as follows:

name Overview
Collection Collection is the most basic set interface, and a Collection represents a group of Object,java that do not provide classes that inherit directly from Collection, providing only inherited Yu interfaces (such as list and set).
List The list interface is an ordered collection that allows you to precisely control where each element is inserted, and to access the elements in the list by index (where the element is positioned in the list, like an array's small tag), and the same elements are allowed.
Set The set has exactly the same interface as the Collection, except that the set does not hold duplicate elements , but behaves differently.
SortedSet Inherit from set that holds an ordered collection.
Map Maps a unique key to a value.
Map.entry Describes an element in a map (a key/value pair). is the inner class of a map.
SortedMap Inherit from map so that key remains in ascending order.
Enumeration This is a traditional interface and a defined method through which you can enumerate (get one at a time) an element in the collection of objects. This traditional interface has been superseded by iterators.

See also: online documentation-jdk-zh.

Collection Implementation Class
collection type Description
ArrayList An index sequence that can grow and shrink dynamically
LinkedList An ordered sequence that can efficiently insert and delete operations at any location
Arraydeque A double-ended queue implemented with a loop array
HashSet An unordered collection with no repeating elements
TreeSet An ordered set
Enumset A set containing the value of an enumeration type
Linkedhashset A set of elements that can remember the insertion order of an element
Priorityqueue A collection that allows for efficient deletion of the smallest element
HashMap A data structure for storing key/value associations
TreeMap A mapping table with ordered order of key values
Enummap A mapping table with key values that are enumerated types
Weakhashmap A mapping table whose value can be reclaimed by the garbage collector
Linkedhashmap A mapping table that remembers the order in which key/value items are added
Identityhashmap A mapping table that uses = = instead of comparing key values with equals

There is also a set of classes whose names begin with abstract, for example, Abstractqueue, which are designed for class library implementations to implement their own data structures.

Common data structures
  • Vectors (vector)

    The vector class implements a dynamic array. And ArrayList are similar, but vectors are accessed synchronously (synchronization takes a lot of time, it is recommended to use ArrayList when synchronization is not required), and vectors contain many traditional methods that do not belong to the collection framework. Vectors are primarily used in cases where the size of an array is not known beforehand, or if only an array of size can be changed. It is important to note that the data in the vector is converted to object objects, which are forced into the original type when the element is taken out.

    The vector class supports 4 methods of construction:

    Common methods:

    Method Description
    Boolean Add (E E) Adds the specified element to the end of this vector.
    void Add (int index, E Element) Inserts the specified element at the specified position in this vector.
    E Remove (int index) Removes the element at the specified position in this vector.
    Boolean remove (Object o) Removes the first occurrence of the specified element in this vector and does not contain the element remains unchanged.
    e Set (int index, e Element) Replaces the element at the specified position in this vector with the specified element.
    void Setelementat (E obj, int index) Sets this vector to the specified object at the component at index.
    E get (int index) Returns the element at the specified position in the vector.
    int indexOf (Object o) Returns the index of the specified element for the first occurrence in this vector, not containing the return-1.
    Boolean IsEmpty () Tests whether this vector does not contain components.
    void Clear () Removes all elements from this vector.
    int capacity () Returns the current capacity of this vector.
    Boolean contains (Object o) Returns true if this vector contains the specified element.
    void setSize (int newSize) Sets the size of this vector.
    void TrimToSize () Fine-tune the capacity of this vector so that it is equal to the current size of the vector.

    Vector also defines a number of other methods, which can be seen in the Java vector class.

  • Hash table (Hashtable)

    Hashtable (indeed lowercase t) is part of the original Java.util and is a dictionary concrete implementation. However, the Java 2 refactoring Hashtable implements the map interface, so Hashtable is now integrated into the collection framework . It is similar to the HashMap class, but it supports synchronization (it is also recommended to use HashMap when synchronization is not required). Like HashMap, Hashtable stores key/value pairs in a hash table. When using a hash table, specify the object to use as the key, and the value to link to the key. The key is then hashed and the resulting hash code is used as the index of the value stored in the table.

    The Hashtable defines four construction methods:

    If the load factor of the hash table is 0.75, the capacity is automatically increased to twice times the original capacity when the hash table's capacity is used by 75%. Typically, the default load factor (0.75) seeks a tradeoff between time and space costs, which, while reducing the space overhead, increases the time to find an entry.

    Common methods:

    Method Description
    V Put (K key, V value) Maps the specified key to the specified value in this hash table.
    V Remove (Object key) Removes the key and its corresponding value from the hash table.
    V get (Object key) Returns the value that the specified key is mapped to, or null if it is not included.
    void Clear () Empty the hash table so that it does not contain any keys.
    Boolean Containsvalue (Object value) Returns true if this Hashtable maps one or more keys to this value.
    Boolean IsEmpty () Tests if this hashtable does not have a key mapped to a value.
    int size () Returns the number of keys in this hash table.
    void Rehash () Increase the capacity of this hashtable and reorganize it internally to more efficiently accommodate and access its elements.

    Some other methods are also defined in HashTable, which can be seen in the Java HashTable interface.

  • The
  • stack

    Stack is a subclass of vector that implements a standard last-in, first-out stack. The stack defines only the default constructor, which is used to create an empty stack. The stack, in addition to all the methods defined by the vector, defines some of its own methods, as follows:

    method Description
    Boolean empty () test stack is empty.
    object Peek () View the object at the top of the stack, but not remove it from the stack.
    object Pop () removes the object at the top of the stack and returns the pair as the value of this function.
    object Push (object element) presses the item onto the top of the stack.
    int Search (object element) returns the position of the object in the stack, with a base of 1.

    But deque  interface and its implementation provide a more complete and consistent set of LIFO stack operations, which should take precedence over this set, rather than this Class.

    Note: The Queue in Java is an interface, Deque is its subinterface, LinkedList and Priorityqueue are its implementation classes, and Arraydeque is the implementation class of the Deque interface. Specific methods can be viewed: JDK 1.6 Online Chinese manual.

  • Linked list (LinkedList)

    LinkedList is a double-ended linked list, and the data stored in the node is treated as an object. Since any class is an indirect subclass of the object class, you can use any one object as the data in a linked table node. It is important to note that the Get () method returns a type of object that is to be converted back to the original type.
    Construction Method:

    Common methods:

    Method Description
    Boolean Add (E E) Adds the specified element to the end of this list.
    void Add (int index, E Element) Inserts the specified element in the location specified in this list.
    E Remove (int index) Removes the element at the specified position in this list.
    E Remove () Gets and removes the header of this list (the first element).
    Boolean remove (Object o) Removes the first occurrence of the specified element and does not include the change.
    e Set (int index, e Element) Replaces the element at the specified position in this list with the specified element.
    E get (int index) Returns the element at the specified position in this list. inefficient , not random access.
    int indexOf (Object o) Returns the index of the specified element that first appears in this list, or 1 if it does not exist.
    Boolean contains (Object o) Returns true if at least one of the specified elements is included.
    int size () Returns the number of elements in this list.
    void Clear () Removes all elements from this list.
    Object Clone () Returns a shallow copy of this linkedlist. (The elements themselves are not duplicated.) )
    Object[] ToArray () This method returns a new array, which the caller can modify arbitrarily.

    LinkedList also implements the Deque interface, the list interface and the queue interface, there are methods to support it as a stack, queue or double-ended queue, do not expand here, the specific method can be viewed: JDK 1.6 Online Chinese manual.

https://www.cnblogs.com/mingyueanyao/p/7260301.html

Java Collection Framework (common data structures)

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.