Some container classes in Java

Posted by balacay on Mon, 03 Jan 2022 13:11:19 +0100

1. Map

1.1 Rookie tutorial: Java utill. Map

1.2 Map is an interface

public interface Map<K,V> {
	int size();
	boolean isEmpty();
	Set<K> keySet();  //returns a Set view of the keys contained in this map
	Collection<V> values(); //returns a Collection view of the values contained in this map
	Set<Map.Entry<K,V>> entrySet(); //returns a Set view of the mappings contained in this map
	interface Entry<K, V>{

1.3 common subclasses of map

1) HashMap: stored out of order; It is a new operation class (introduced after JDK1.2); key cannot be repeated; Asynchronous processing mode is adopted, with higher performance; It is a non thread safe operation class.
2) Hashtable: stored out of order; It is an old operation class (launched in JDK1.0); key cannot be repeated; Synchronous processing mode is adopted, with low performance; It belongs to thread safe operation class.
3) TreeMap: a set of maps that can be sorted, sorted by the key in the set; Key cannot be repeated.
4) WeakHashMap: a weakly referenced Map collection. When some contents in the collection are no longer used, useless data can be removed and gc can be used for recycling.
5) IdentityHashMap: key is a collection of maps that can be repeated.

1.4 K and V in map < K, V >

Both K and V should be reference types, such as Integer and long, but not value types, such as int and long.
The specific reason is that the generic type is erased. If it is a value type, once the generic information is erased, there is no way to insert the forced transformation code, because the forced transformation between int, long and Object is not supported. (see in-depth understanding of the third edition of Java virtual machine P372).

2. Set

2.1 java.util.Set is an interface

public interface Set<E> extends Collection<E> {

2.2 common subclasses of set

The underlying data structure is a hash table, which is thread unsafe and efficient. It allows null values to be stored and elements are out of order
The uniqueness of the element depends on overriding the hashCode() method and the equals() method. It cannot be guaranteed without rewriting.
Integer and String override the hashCode() and equals() methods by default

3. List

3.1 java.util.List is an interface

public interface List<E> extends Collection<E> {

3.2 common subclasses of list

  1. LinkedList
    The underlying data structure of LinkedList is implemented through its internal class Node. LinkedList is a two-way linked list.
    It can be operated as a stack, queue, or double ended queue.
public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable,
	private static class Node<E> {
	        E item;
	        Node<E> next;
	        Node<E> prev;
	        Node(Node<E> prev, E element, Node<E> next) {
	            this.item = element;
	   = next;
	            this.prev = prev;
  1. ArrayList
    Dynamic array. ArrayList uses array as the underlying data structure, which is characterized by fast query speed but slow structure modification.
    The operation in ArrayList is not thread safe, so it is recommended to use it in single thread.

4. Stack

4.1 Rookie tutorial: Stack
4.2 Stack inherits from Vector, because Vector is implemented through array, that is, Stack is also implemented through array.

public class Stack<E> extends Vector<E> {

4.3 use of stack
The Stack class [^ 3] is not recommended by Java officials

Stack<Integer> stack1 = new Stack<>();

Other declaration methods of stack:

Deque<Integer> inStack = new LinkedList<>();
Deque<Integer> inStack = new ArrayDeque<>();  //Java officially recommends the use of the declaration stack

5. Queue

Queue<Integer> queue = new LinkedList<>();

reference resources:
1. Java class set – Map interface, HashMap, IdentityHashMap, SortedMap
2. Generics
3. What are the bad designs of Java?

Topics: Java