Java interview question set (2) list and Map related knowledge (1.2)

Foreword:

In ordinary writing java programs, in addition to 8 common data types, String objects, there are also collection classes, such as ArrayList, HashMap, etc., which are the most commonly used.

 

1. List interface

  The List interface is the direct interface of Collection. List represents an ordered Collection (container) that maintains the order of elements with a certain insertion order. The user has precise control over where each element in the list is inserted. You can then access elements based on their integer indices, and search for elements in the list. For example: get(1); the collections that implement the List interface mainly include: ArrayList, LinkedList, Vector, Stack.

  1-1:ArrayList 

    ArrayList is a dynamic array and our most used collection. It allows element insertion of any notation rule (including null). Each ArrayList has an initial capacity (10), which represents the size of the array. As the elements of the container continue to increase, the size of the container will also increase automatically. Every time an element is added to the container, a container check will be performed. When it is about to overflow, the expansion operation will be performed. (If we know how many elements are inserted, it is best to specify an initial capacity value to avoid excessive expansion operations and affect program performance.)

  Common operations: size, isEmpty, get, set, iterator and listIterator, all run in constant time.

  ArrayList is good at random access because of the dynamic array based data structure. (Associate the concept of physical memory address)

  (Unsynchronized, multi-threaded access to the same List requires access synchronization)

  1-2:LinkedList

    LinkedList is a doubly linked list. In addition to the basic operations of ArrayList, it also provides get, remove, and insert methods at the head or tail of LinkedList. Since LinkedList is a data structure based on a doubly linked list, all operations must be performed according to this data structure. The operation of indexing in the list will traverse the list from the beginning or the end (from the end close to the specified index), which can be implemented in List at a low cost. Insert and delete operations. (Convenience of physical storage of Lenovo linked list)

    Same as ArrayList, (unsynchronized, multi-threaded access to the same List, need to achieve access synchronization)

  1-3:Vector

    Similar to ArrayList, but Vector is synchronized. Vector is a thread-safe dynamic array. Its operation is almost the same as ArrayList, (but it is rarely used when writing code, and I don't know why.)

  1-4:Stack

    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.

    (But it's rarely used when writing code, and I don't know why.)

 

2. Set interface

  A set is a Collection that does not contain repeating elements. It maintains its own internal sorting and cannot be accessed randomly. Like List, it allows the existence of null, but there is only one. Due to the particularity of the Set interface, the elements passed into the Set collection must be different. Colleagues should pay attention to any variable Object, if e1.equals(e2) == true when operating on elements in the collection, there must be some problems.

  The collections of the Set interface are: EnumSet, HashSet, TreeSet

  :2-1:EnumSet

  When you look at Enum, you know that it is an enumerated Set, and all elements are enumerated types. Because it is internally implemented with HashCode, the internal

  2-2:HashSet

  HashSet can be called the fastest collection, because it is internally implemented by HashCode, and the order of internal elements is determined by the hash code, so the iteration order is unclear.

  2-3:TreeSet

  Based on TreeMap, generate a set that is always in sorted state, which is implemented internally by TreeMap. Either the elements are sorted using their natural ordering, or according to the Comparator provided to create the Set, depending on the constructor used.

 

3. Map interface

  Map is different from List and Set interfaces. It is a collection composed of a series of key-value pairs. It provides key-value mapping and does not inherit Collection. In Map, he guarantees the correspondence between key and value, there will be no same key, and the put method of Map does not allow duplicate keys.

  3-1:HashMap

  At first glance, it is known that it is implemented in a hash table data structure. When searching for an object, its position is calculated by a hash function for fast query. A hash table array (Entry[ ] table) is defined internally, and the element will convert the hash address of the element into the index that exists in the array through the hash conversion function. The elements of the hash address are strung together to form a singly linked list structure.

  3-2:TreeMap

  The key is sorted by a certain ordering rule, and it is internally implemented by the data structure of red-black Tree (red-black tree), which implements the SortedMap interface.

  3-3:HashTable

  It is also a Map of the hash table data structure, thread-safe and suitable for synchronous operations, so the performance is lower than that of HashMap.

 

4. Queue

  Queues are mainly divided into two categories. One is blocking queues. After the queue is full, an exception will be thrown when elements are inserted, mainly including ArrayBlockQueue, PriorityBlockingQueue, and LinkedBlockingQueue. The other kind of queue is a double-ended queue, which supports inserting and removing elements at the head and tail ends, mainly including: ArrayDeque, LinkedBlockingDeque, and LinkedList.

 

5. Differences from each other

  Vector 和 ArrayList

  (1) Vector is thread-synchronized, so it is also thread-safe, while ArrayList is thread-asynchronous and unsafe. Generally, it is efficient to use ArrayList without considering multi-threading.

  (2) When the number of elements in the collection is greater than the length of the current collection array, the growth rate of Vector is 100% of the current array length, while the growth rate of ArrayList is 50%. Considering that the collection data is relatively large, it is better to use Vector.

  (3) Find the data at the specified location, Vector and ArrayList use the same time. The fastest mobile data location is LinkedList.

  (4) ArrayList and Vector use arrays to store data. The number of elements in this array is larger than the actual data stored in order to add and insert elements. Direct serial number index elements are allowed, but the inserted data should be designed to memory operations such as array element movement, so the index Data is fast and data is inserted slowly. Vector uses the synchronized method (thread safety), so its performance is worse than ArrayList. LinkedList uses a doubly linked list for storage. Indexing data by serial number needs to be traversed forward or backward, but when inserting data, only need It is enough to record the preceding and following items of this item, so the number of insertions is faster.

  ArrayList和LinkedList

  1. ArrayList is a data structure based on dynamic arrays, and LinkedList is a data structure based on linked lists.
  2. For random access get and set, ArrayList feels better than LinkedList, because LinkedList has to move the pointer.
  3. For the add and remove operations, LinedList is more dominant, because ArrayList needs to move data.
      This depends on the actual situation. If only a single piece of data is inserted or deleted, the speed of ArrayList is better than that of LinkedList. However, if data is randomly inserted and deleted in batches, the speed of LinkedList is much better than that of ArrayList. Because ArrayList inserts a piece of data, it needs to move the insertion point and all the data after it.

  

  HashMap与TreeMap

      1. HashMap can quickly find its content through hashcode, and all elements in TreeMap maintain a certain fixed order. If you need to get an ordered result, you should use TreeMap (the arrangement order of elements in HashMap is not stable). The arrangement order of elements in HashMap is not fixed).

      2. HashMap can quickly find its content through hashcode, and all elements in TreeMap maintain a certain fixed order. If you need to get an ordered result, you should use TreeMap (the arrangement order of elements in HashMap is not stable). The Collections Framework" provides two general Map implementations: HashMap and TreeMap (TreeMap implements the SortedMap interface).

      3. Insert, delete and locate elements in Map, HashMap is the best choice. But if you want to iterate over keys in natural order or custom order then TreeMap is better. Using a HashMap requires that the added key classes have well-defined implementations of hashCode() and equals(). This TreeMap has no tuning options because the tree is always in equilibrium.

  Hashtable与Hashmap

      1. Historical reasons: Hashtable is based on the old Dictionary class, and HashMap is an implementation of the Map interface introduced in Java 1.2.

      2. Synchronization: Hashtable is thread-safe, that is to say, it is synchronized, while HashMap is thread-insecure and not synchronized.

      3. Value: Only HashMap allows you to use a null value as the key or value of a table entry.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325894028&siteId=291194637