Common Java data structures and characteristics, usage scenarios

Common data structures and characteristics of Java

Common data structures in Java are mainly divided into two main interfaces: Collection and Map. The data structure in the program is a data structure class that inherits these interfaces.

Collection interface :

  1. List interface inheritance: implementation of ArrayList, Vector, LinkedList, etc.;

  2. Set interface inheritance: HashSet, LinkedHashSet, TreeSet and other implementations.

Map interface : HashMap, SortedMap inheritance, LinkedHashMap, TreeMap, etc. implementation.

Several commonly used classes and their characteristics:

ArrayList

  • Data structure: Object array is used at the bottom of ArrayList;
  • Storage structure: the continuous storage structure on the physical storage unit;
  • Whether the thread is safe: the thread is not safe; ArrayList is asynchronous, and the method is not locked;
  • Features: It is convenient to find, but when adding and deleting operations, there must be an operation to move the position, so ArrayList is suitable for storage and data storage with frequent query operations.

LinkedList

  • Data structure: The bottom layer of LinkedList uses a two-way circular linked list data structure;
  • Storage mode: non-contiguous, non-sequential storage structure on the physical storage unit;
  • Whether the thread is safe: the thread is not safe;
  • Features: Storage of each element consumes more space than ArrayList, and because of its storage structure, it is not very convenient to query it. It is necessary to traverse each node, and then find the node's successor node, which is not suitable for storage and requires a large number of query operations. Data storage, but insertion is more convenient than ArrayList, no transposition operation is required, only the predecessor and successor of the pointer need to be changed, the addition and deletion operations are very fast, and no extra resources are consumed.

List summary

  • All Lists can only accommodate a single table composed of objects of different types, not Key-Value key-value pairs. For example: [tom,1,c]
  • All Lists can have the same elements, for example, Vector can have [tom,koo,too,koo]
  • All lists can have null elements, such as [tom,null,1]
  • Array-based List (Vector, ArrayList) is suitable for query, and LinkedList is suitable for adding and deleting operations

Vector

  • Data structure: The bottom layer of Vector uses an Object array;
  • Storage structure: the continuous storage structure on the physical storage unit;
  • Is the thread safe: Vector is thread safe, and the methods of the Vector class are all locked;
  • Features: Vector is thread-safe, and a Vector object can be accessed by multiple threads. But when a thread accesses it, ensuring thread safety will consume a certain amount of resources. Therefore, a thread access does not need to consider the thread safety issue. It is recommended to use ArrayList.

TreeSet

  • Data structure: the underlying data structure is a binary tree;
  • Whether the thread is safe: thread safety is not guaranteed;
  • Features: Orderly, and no repeating elements. You can specify an order, and then sort the elements in ascending order.

HashSet

  • Data structure: linked list and red-black tree (after jdk1.8);
  • Whether the thread is safe: thread safety is not guaranteed;
  • Features: The elements have no order (because the bottom layer uses HashMap, the element degree in the HashMap itself has no order), the elements cannot be repeated, and the contained elements cannot be accessed randomly. It can only be used to achieve one-way traversal, multiple iterative access, and the The order may be different.

Set summary

  • The basis of Set implementation is Map (HashMap);
  • The elements in the Set cannot be repeated. If you use the add(Object obj) method to add an existing object, the previous object will be overwritten.

HashMap

  • Data structure: linked list and red-black tree (after jdk1.8);
  • Whether the thread is safe: not thread safe;
  • Features: Null can be used as the primary key, but there can be only one, and multiple Values ​​can be Null; it is suitable for inserting, deleting and positioning elements in the Map.

TreeMap

  • Data structure: tree;
  • Whether the thread is safe: not thread safe;
  • Features: Ordered, suitable for traversing keys in natural order or custom order.

LinkedHashMap

  • Data structure: HashMap+LinkedList;
  • Whether the thread is safe: not thread safe;
  • Features: orderly, both Key and Value are allowed to be empty, Key repeats will cover, and Value allows repeats.

What is the fastest query in Java?

According to the array index, finding the elements of the array is the fastest operation in the world in Java!

The difference between array and ArrayList:

  • Array can store a set of data with the same data type. No operation algorithm is provided. To use the array, you need to write your own algorithm, and use the special algorithm of array coding, which has the
    best performance! The ArrayList can also store a set of data. It is also an array inside. It also provides an operation algorithm and is easy to use. The general algorithm has the advantage of being easy to use, and the
    algorithm is tested and very reliable. But if you expect high performance, use arrays, if you expect high development efficiency, use Arraylist.

Use of Vector, ArrayList and LinkedList

In most cases, ArrayList is the best in terms of performance, but LinkedList will perform better when elements in the collection need to be inserted and deleted frequently, but none of the three performances can compare to arrays. and so:

  • If you can use an array (the element type is fixed, the length of the array is fixed), please try to use an array instead of List;
  • If there is no frequent delete and insert operations, and there is no need to consider multi-threading, ArrayList is preferred;
  • If used under multi-threaded conditions, Vector can be considered;
  • If you need to delete and insert frequently, LinkedList is useful;
  • If you don't know anything, use ArrayList.

Guess you like

Origin blog.csdn.net/qq_33626996/article/details/111193241