An overview of common basic Java data structures

  • 2020-10-23 20:58:33
  • OfStack

Java data structure is a subject that studies the operation objects of computers and their relationships and operations in non-numerical computation programming problems. The types most commonly used in Java data structures are as follows:

Map interface

Note that Map does not inherit the Collection interface and that Map provides mapping from key to value. 1 Map cannot contain the same key, and each key can map only 1 value.

The Map interface provides views of three collections, and the contents of Map can be treated as a set of key, a set of value, or a set of ES23en-ES24en mappings.

List interface

List is an ordered Collection that allows users to access elements in List using an index (the position of the element in List, similar to array subscript), which is similar to an array in Java.

Unlike Set mentioned below, List allows the same elements.

Collection interface

Two standard constructors: the no-argument constructor is used to create an empty Collection; The constructor with one Collection argument is used to create a new Collection

How to traverse:


Iteratorit=collection.iterator();// To obtain 1 An iterative son 

while(it.hasNext()){

Objectobj=it.next();// Get under 1 An element 

}

The two interfaces derived from the Collection interface are List and Set.

ArrayList class

ArrayList implements variable-size arrays. It allows all elements, including null. ArrayList is not synchronized.

Hashtable class

Hashtable inherits the Map interface and implements a hash table for key-ES73en mapping. Any object that is not empty (ES74en-ES75en) can act as key or value.

Adding data using put(key,value) and fetching data using get(key) are two basic operations with a constant time overhead. Hashtable adjusts performance with both initialcapacity and loadfactor parameters. The default, loadfactor0.75, achieves a good balance between time and space. Increasing loadfactor saves space but the corresponding lookup time will increase, which will affect operations like get and put.

A simple example of using Hashtable is to place 1, 2, and 3 in Hashtable, whose key is "one", "two", and "three" respectively:


Hashtablenumbers=newHashtable();

numbers.put( " one " ,newInteger(1));

numbers.put( " two " ,newInteger(2));

numbers.put( " three " ,newInteger(3));

To extract a number, such as 2, use the corresponding key:


Integern=(Integer)numbers.get( " two " );

Since an object that is key will determine the location of its corresponding value by calculating its hash function, any object that is key must implement the hashCode and equals methods. hashCode and equals method Object inherited from the root class, if you use a custom class as key, be very careful, as defined by the hash function, if two objects are the same, namely obj1. equals (obj2) = = true, then their hashCode must be the same, but if two objects are different, they hashCode not 1 is different, if two different objects of the same hashCode, this phenomenon is known as the conflict, Conflicts can make working with the hash table time-consuming, so try to define the hashCode() method to speed up the operation of the hash table.

If the same object has a different hashCode and the operation on the hash table has unexpected results (the expected get method returns null), there is only one thing to keep in mind to avoid this problem: copy both equals and hashCode methods, not just one.

Hashtable is synchronous.

Stack class

Stack inherits from Vector and implements a lifo stack. Stack provides five additional methods to enable Vector to be used as a stack. The basic push and pop methods, as well as peek methods, get the elements at the top of the stack, empty methods test whether the stack is empty, and search methods detect the location of an element on the stack. Stack was just created with an empty stack.

Set interface

Set is an Collection without repeating elements, that is, any two elements e1 and e2 have e1.equals(e2)==false, and Set has at most one null element.

Obviously, the Set constructor has a constraint that the Collection parameter passed in cannot contain duplicate elements.

Note: Mutable objects (MutableObject) must be handled with care. If a variable element in Set changes its state and causes ES174en. equals(Object)==true, this will cause 1 problem.

WeakHashMap class

WeakHashMap is an improved VERSION of HashMap, which applies a "weak reference" to key. If an key is no longer referenced externally, then it can be recycled by GC.

Vector class

Vector is very similar to ArrayList, but Vector is synchronous.

HashMap class

HashMap is similar to Hashtable, except that HashMap is asynchronous and allows null, nullvalue and nullkey. But when HashMap is treated as Collection (the values() method returns Collection), its iterator operation time is proportional to the capacity of HashMap. Therefore, do not set HashMap's initialization capacity too high or loadfactor too low if the performance of the iteration operations is important.

LinkedList class

The null element is allowed.

In addition LinkedList provides additional get, remove, insert methods at the head or tail of LinkedList. These operations enable LinkedList to be used as a stack (stack), a queue (queue), or a two-way queue (deque).

Note that LinkedList has no synchronized methods. If multiple threads are accessing an List at the same time, they must implement their own access synchronization. One solution is to construct a synchronous List when List is created:


Listlist=Collections.synchronizedList(newLinkedList(...));

conclusion

The above is the full content of this article, this site has more details and specific introduction about data structure. I hope it helps.


Related articles: