Java data structures

In the recent interview, I encountered a question about what data structures Java has. Now I will summarize

 

Several data types in Java:

Linear lists, linked lists, and hash tables are commonly used data structures. When developing Java, JDK has provided us with a series of corresponding classes to implement basic data structures. These classes are in the java.util package. This article attempts to explain to the reader the role of each class and how to use these classes correctly through a simple description.

 

Collection
├List
│├LinkedList
│├ArrayList
│└Vector
└Stack└Set 

 

└SortedSet

 

 

 

LinkedList

 

 

List

ArrayList

 

 

 

Vector

Stack

Collection

 

 

 

 

 

 

 

 

Set

SortedSet

 

 


Map
├Hashtable
├HashMap
└WeakHashMap

 

 

 

 

HashTable

Map

HashMap

 

WeakHashMap

 

Collection interface

          Collection is the most basic interface of collection. A Collection represents a group of Objects, that is, the elements of Collection. Some Collections allow the same elements but some do not, some can be sorted and some cannot. The Java SDK does not provide classes that directly inherit from Collection. The classes provided by Java jdk are all sub-interfaces that inherit from Collection, "List and Set".

 

       All classes that implement the Collection interface must provide two standard constructors: the parameterless constructor is used to create an empty Collection, and the constructor with a Collection parameter is used to create a new Collection, the new Collection and the passed entered

Collection has the same elements. The latter constructor allows the user to copy a Collection.

 

How to iterate through each element in the Collection? Regardless of the actual type of the Collection, it supports an iterator() method that returns an iterator that can be used to access each element in the Collection one by one. Typical usage is as follows:

    Iterator it = collection.iterator(); // get an iterator
    while(it.hasNext()) {
      Object obj = it.next(); // get the next element
    }

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

 


I am growing and improving
several data structures in JAVA every day.

Linear tables, linked lists, and hash tables are commonly used data structures. When developing Java, JDK has provided us with a series of corresponding classes to Implement basic data structures. These classes are in the java.util package. This article attempts to explain to the reader the role of each class and how to use these classes correctly through a simple description.

Collection
├List
│├LinkedList
│├ArrayList
│└Vector
│ └Stack
└Set

└SortedSet

 
   

 
   

LinkedList
   

 

 
   

List
   

ArrayList
   

 

 
   

 
   

Vector
   

Stack

Collection
   

 
   

 
   

 

 
   

 
   

 
   

 

 
   

Set
   

SortedSet
   

 


Map
├Hashtable
├HashMap
└WeakHashMap

 
   

HashTable

Map
   

HashMap

 
   

WeakHashMap



Collection interface
  Collection is the most basic collection interface. A Collection represents a group of Objects, that is, the elements of Collection. Some Collections allow the same elements while others do not. Some sort and others don't. The Java SDK does not provide classes that inherit directly from Collection. The classes provided by the Java SDK are all "sub-interfaces" that inherit from Collection, such as List and Set.

  All classes that implement the Collection interface must provide two standard constructors: the parameterless constructor is used to create an empty Collection, and the constructor with a Collection parameter is used to create a new Collection, the new Collection and the passed The incoming Collection has the same elements. The latter constructor allows the user to copy a Collection.

  How to iterate through each element in the Collection? Regardless of the actual type of the Collection, it supports an iterator() method that returns an iterator that can be used to access each element in the Collection one by one. Typical usage is as follows:

    Iterator it = collection.iterator(); // get an iterator
    while(it.hasNext()) {
      Object obj = it.next(); // get the next element
    }

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

Main method:
boolean add(Object o) adds an object to the collection
boolean remove(Object o) removes the specified object
int size() returns the number of elements in the current collection
boolean contains(Object o) finds whether there is a specified object in the
collection boolean isEmpty() determines whether the collection is empty
Iterator iterator() returns an iterator
boolean containsAll(Collection c) finds whether there is an element in the collection c
boolean addAll( Collection c) Adds all elements in collection c to the collection
void clear() removes all elements in the collection
void removeAll(Collection c) removes c from the collection that also has elements in the collection
void retainAll(Collection c) removes the collection from the collection elements not included in c

 

List interface

      The List interface is an ordered Collection. Using this interface, you can precisely control where each element is inserted. Users can use the index to access List elements, similar to java arrays.

 

          Unlike Set, which will be mentioned below, List is allowed to have the same elements.

  In addition to the iterator() method that is necessary for the Collection interface, List also provides a listIterator() method that returns a ListIterator interface. Compared with the standard Iterator interface, ListIterator has some add() and other methods that allow adding, Delete, set elements, and traverse forward or backward.

        Common classes that implement the List interface are LinkedList, ArrayList, Vector and Stack.

Main method:
void add(int index, Object element) adds an object at the specified position
boolean addAll(int index, Collection c) adds the element of the collection c to the specified position
Object get(int index) returns the specified position in the List Element
int indexOf(Object o) returns the position of the first occurrence of element o.
Object removeint(int index) deletes the element at the specified position
Object set(int index, Object element) replaces the element at position index with element element, and returns the replaced element Elements

 

The LinkedList class
  LinkedList implements the List interface, allowing null elements. In addition, LinkedList provides additional get, remove, and insert methods at the head or tail of LinkedList. These operations allow LinkedList to be used as a stack, queue or deque.

  Note that LinkedList has no synchronized methods. If multiple threads access a List at the same time, you must implement access synchronization yourself. A workaround is to construct a synchronized List when the List is created:

    List list = Collections.synchronizedList(new LinkedList(...)); The

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

  The running time of size, isEmpty, get, set methods is constant. However, the overhead of the add method is amortized constant, and adding n elements takes O(n) time. Other methods run linearly.

  Each ArrayList instance has a capacity (Capacity), which is the size of the array used to store elements. This capacity can be automatically increased as new elements are added, but the growth algorithm is not defined. When a large number of elements need to be inserted, the ensureCapacity method can be called before insertion to increase the capacity of the ArrayList to improve the insertion efficiency.

  Like LinkedList, ArrayList is also unsynchronized.

Main method:
Boolean add(Object o) adds the specified element to the end of the list
Boolean add(int index, Object element) adds the specified element to the specified position in the list
Boolean addAll(Collection c) adds the specified collection to the end of the list
Boolean addAll(int index, Collection c) adds the specified collection at the specified position in the list
Boolean clear() deletes all elements
in the list Boolean clone() returns a copy of the list instance
Boolean contains(Object o) Determines whether the list contains elements
Boolean ensureCapacity(int m) Increases the capacity of the list, if necessary, the list can hold m elements
Object get(int index) Returns the element at the specified position in the list
Int indexOf(Object elem) finds the subscript of the specified element in the list
Int size() returns the number of elements in the current list

The Vector class
  Vector is very similar to ArrayList, but Vector is synchronized. The Iterator created by Vector has the same interface as the Iterator created by ArrayList. However, because Vector is synchronized, when an Iterator is created and is being used, another thread changes the state of the Vector (for example, adding or removing some element), a ConcurrentModificationException will be thrown when the method of the Iterator is called, so this exception must be caught.

The Stack class
  Stack inherits from Vector and implements a LIFO stack. Stack provides 5 additional methods that allow Vector to be used as a stack. The basic push and pop methods, as well as the peek method, get the element at the top of the stack, the empty method tests whether the stack is empty, and the search method checks the position of an element on the stack. The stack is an empty stack just after it is created.

Set interface
  Set is a Collection that does not contain duplicate elements, that is, any two elements e1 and e2 have e1.equals(e2)=false, and Set has at most one null element.

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

  Note: Mutable Objects must be handled with care. If a mutable element in a Set changes its state and Object.equals(Object)=true will cause some problems,

Map interface
  Please note that Map does not inherit the Collection interface, Map provides key to value mapping. A Map cannot contain the same key, and each key can only map one value. The Map interface provides three sets of views. The contents of the Map can be regarded as a set of key sets, a set of value sets, or a set of key-value mappings.

Main methods:
boolean equals(Object o) compare objects
boolean remove(Object o) delete an object
put(Object key, Object value) add key and value

Hashtable class
  Hashtable inherits the Map interface and implements a hash table with key-value mapping. Any non-null object can be used as key or value.

  Use put(key, value) to add data and get(key) to retrieve data. The time overhead of these two basic operations is constant.
Hashtable adjusts performance through two parameters, initial capacity and load factor. Usually the default load factor of 0.75 achieves a better balance between time and space. Increasing the load factor can save space but the corresponding lookup time will increase, which affects operations like get and put.

  A simple example of using Hashtable is as follows, put 1, 2, and 3 into Hashtable, and their keys are "one", "two", "three":
    Hashtable numbers = new Hashtable();
    numbers.put("one" , new Integer(1));
    numbers.put("two", new Integer(2));
    numbers.put("three", new Integer(3));

  To take out a number, such as 2, use the corresponding key:
    Integer n = (Integer)numbers .get("two");
    System.out.println("two = " + n);

  Since the object as the key will determine the position of the value corresponding to it by calculating its hash function, any object as the key will Both must implement the hashCode and equals methods. The hashCode and equals methods are inherited from the root class Object. If you use a custom class as the key, be very careful. According to the definition of the hash function, if the two objects are the same, that is, obj1.equals(obj2)=true, then Their hashCode must be the same, but if the two objects are different, their hashCode is not necessarily different. If the hashCode of two different objects is the same, this phenomenon is called conflict, and the conflict will increase the time overhead of operating the hash table. So try to define a good hashCode() method to speed up the operation of the hash table.

  If the same object has different hashCode, the operation of the hash table will have unexpected results (the expected get method returns null). To avoid this problem, you only need to keep one thing in mind: to override the equals method and the hashCode method at the same time, And don't just write one of them.

  Hashtable is synchronous.

HashMap class
  HashMap is similar to Hashtable, the difference is that HashMap is asynchronous and allows null, i.e. null value and null key. , but when HashMap is regarded as a Collection (the values() method can return a Collection), the time overhead of its iteration sub-operation is proportional to the capacity of HashMap. Therefore, if the performance of the iterative operation is very important, do not set the initialization capacity of the HashMap too high, or the load factor is too low.

WeakHashMap class
  WeakHashMap is an improved HashMap, which implements a "weak reference" to the key. If a key is no longer externally referenced, the key can be recycled by GC.

Summary
  If operations such as stacks and queues are involved, you should consider using List. If you need to insert and delete elements quickly, you should use LinkedList. If you need to access elements quickly and randomly, you should use ArrayList.

  If the program is in a single-threaded environment, or the access is only performed in one thread, consider the asynchronous class, which is more efficient. If multiple threads may operate a class at the same time, the synchronized class should be used.

  Special attention should be paid to the operation of the hash table, and the equals and hashCode methods should be correctly overridden for the key object.

  Try to return the interface instead of the actual type, such as returning List instead of ArrayList, so that if you need to replace ArrayList with LinkedList in the future, the client code does not need to be changed. This is programming for abstraction

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326661380&siteId=291194637