2019.6.22 set (a)

1. Please describe the List, Map, Set three interfaces while the access elements, what are the differences?

Inspection point: List

Reference answer:

List index to access a particular element, the element can be repeated.

Set not store duplicate elements (with the object equals () method to distinguish whether to repeat elements).

Map save key-value pair (key-value pair) mapping, mapping relationship can be one or many.

When the container has Set Map implementation version based on two hash tree storage and sorting access time complexity is O (1) based on the theoretical version stored hash, the version of the tree sort Based insert or delete elements would constitute a sort tree in accordance with the key element or elements (key) and to re-ordering to achieve the effect.

2. forth ArrayList, Vector, and storage performance characteristics LinkedList

Inspection point: ArrayList

Reference answer:

ArrayList and Vector are using an array mode to store data, the data is greater than the number of elements in the array so as to increase the actual storage and insert elements, which allow the element directly indexed by serial number, but to insert the element array elements involved in memory operations like move, so the index data fast and slow insert data,

Vector method of modifying the addition of synchronized, so Vector is thread-safe container, but ArrayList worse than the performance, it is already a legacy container in Java.

LinkedList using doubly linked list implemented storage (by reference to the associated memory cells scattered additional memory to form a linear structure it can be indexed by serial number, which compared to a continuous chain storage array storage, memory utilization higher), indexed by serial number before the data needs to traverse to or after, but only before and after the record of this can insert the data item, it is inserted faster . Vector belong legacy container (container earlier versions of Java provided in addition, Hashtable, Dictionary, BitSet, Stack , Properties are the legacy container), is no longer recommended, but due to the ArrayList and LinkedListed are non-thread-safe, If you encounter the same container multiple threads operating scenarios, the tools can synchronizedList method Collections will convert it into thread-safe container before use (this is the application for decorating mode, existing objects will pass another the constructor of a class of objects to create a new enhancement implementation).

3. Please judge List, Set, Map whether inherited from the Collection interface?

Inspection point: collection interfaces

Reference answer:

List, Set Shi, Map not.

Map key-value mapping containers, and Set List with a clear distinction, the fragmented and stored Set of elements does not permit duplicates (mathematics set too), the container is a linear structure List for-value index case access elements.

4. Please talk about common set of classes, and the main methods you know?

Inspection point: collection

Reference answer:

The most common type is the set of List and Map.

List specific implementation comprises ArrayList and Vector, which is a list of variable size, more suitable to build, operate and store any type of object list element.

List applies to the case of access elements by numeric index.

Map provides a more general method of storage elements. Map for storing collections of elements (referred to as a "key" and "value"), wherein each key is mapped to a value.

5. Collection illustrate the differences and the Collections.

Inspection point: collection

Reference answer:

Collection is a set of interfaces parent class, inheritance and his main interface Set and List.
The Collections is help for a class set of classes, he offers a series of static methods to achieve a variety of search collection, sorting, and so thread-safe operations .

6. Please explain the difference between ArrayList and LinkedList?

Inspection point: ArrayList

Reference answer:

ArrayList and LinkedList implement the List interface, they have the following differences: 
ArrayList data interface is based on an index, it is the underlying array . It can be a random access to the elements (1) time complexity O. Correspondingly, LinkedList is a list of elements stored in the form of its data, each element and a previous element and link it back together, in this case, seek time an element of complexity is O ( n). 
With respect ArrayList, LinkedList of insertion, addition, deletion faster because when an element is added to the set at any position when not required so as to recalculate or update the index array size. 
ArrayList LinkedList than accounted for more memory , because for each LinkedList node stores the two reference points to a previous element, an element directed downward.

7. Please explain the difference between HashMap and Hashtable? 

Inspection point: collection

Reference answer:

HashMap and Hashtable implement the Map interface, so many features are very similar. However, they have the following differences: 
HashMap allows keys and values are null , while Hashtable does not allow key or value is null. 
Hashtable is synchronized , whereas HashMap is not. Therefore, HashMap more suitable for single-threaded environment, while Hashtable is suitable for multi-threaded environment. 
HashMap provides a collection of available applications iteration key, therefore, HashMap fail-fast. On the other hand, Hashtable provides enumeration of keys (Enumeration). 
Hashtable is generally considered a legacy class.

8. Please tell us the difference between fast failure (fail-fast) and the security failure (fail-safe) of?

Inspection point: collection

Reference answer:

Iterator safety is based on a failure to make copies of the underlying collection, therefore, it is not the impact of the changes source set. java.util package following all the collections are fail-fast, and java.util.concurrent package all of the following classes are safe to fail. Fail-fast iterator will throw ConcurrentModificationException exception, fail safe iterator will never throw this exception.

9. Tell us about the difference between Iterator and ListIterator of?

Inspection point: iterator

Reference answer:

Iterator and ListIterator difference is: 
Iterator used to iterate Set and List collections, but can only be used to traverse ListIterator List
Iterator of the collection can only be traversed forward, ListIterator both before to be backward. 
ListIterator implements Iterator interface, and include other features, such as: adding elements to replace elements, obtaining an index before and after an element, and so on.

10. Please briefly explain what is an iterator?

Inspection point: JAVA iterator

Reference answer:

Iterator provides a unified interface to uniform traversal set of elements, Collection Iterable interface interfaces, 
each set Iterator interface instance returned by implementing Iterable interface iterator () method, then iterate the operation elements of the set. 
Caveat It is: collection method can not remove elements it iterates elements, otherwise it will throw an exception ConcurrentModificationException but can be deleted by iterator interface remove () method.

11. Please explain why the collection class does not implement Cloneable and Serializable interface?

Inspection point: JAVA collection

Reference answer:

Clone (Cloning) or a sequence of (serialization) semantics and meaning associated with a specific implementation. Therefore, it should be determined by the specific implementation classes is how to clone or serialized.
Implements Serializable sequence of effects: the state of the object stored in the storage medium can be rewritten so as to create an identical copy later; by value sent from one object to another application domain from a application domain. 
Implements Serializable role is the object can be stored into a byte stream, can then be recovered. So you think if your object is not serialized, how to network transmission it? To have the network transmission into a byte stream, so in a distributed application, you have to achieve serialization. If you do not need a distributed application, then we need to achieve to achieve serialization.

12. Please describe the basic interface Java collections framework of what?

Inspection point: JAVA collection

Reference answer:

Collections interface specifies a set of objects, called elements.

Each collection class specific interface implementation class can choose its own way to save and sort elements. Some collections allow duplicate keys, some do not. 
Java collection classes provides a set of well-designed support for the operation of a set of objects interfaces and classes. Java collection classes inside the most basic interfaces: 
Collection: on behalf of a group of objects, each object is its children. 
Set: does not contain duplicate elements Collection. 
List: There collection order and may contain repeated elements. 
Map: The key may be (key) is mapped to the target value (value), the key can not be repeated.

13. Please explain ConcurrentHashMap principle?

Inspection point: JAVA memory model

Reference answer:

ConcurrentHashMap class contains two internal static type HashEntry and Segment . Encapsulation mapping table is used to key HashEntry / value pairs; Segment serve as locks for the role of each of the plurality of buckets Segment objects daemon entire hash map. Each barrel is linked by a number of objects up HashEntry list. A ConcurrentHashMap example comprises an array of a plurality of Segment objects. HashEntry used to encapsulate the hash mapping table pairs. In HashEntry class, key, hash and next fields are declared final type, value type field is declared volatile.

1

2

3

4

5

6

7

8

9

10

11

12

13

static final class HashEntry<K,V> {

       final K key;                   // 声明 key 为 final 型

       final int hash;                 // 声明 hash 值为 final 型

       volatile V value;                // 声明 value 为 volatile 型

       final HashEntry<K,V> next;      // 声明 next 为 final 型

  

       HashEntry(K key, int hash, HashEntry<K,V> next, V value) {

           this.key = key;

           this.hash = hash;

           this.next = next;

           this.value = value;

       }

}

In ConcurrentHashMap, when the hash generated if the "bump" will be a "split link method" process "collision": the "collision" HashEntry objects into a linked list. Since HashEntry next field for the final model, the new node can only be inserted in the header of the linked list. The figure is a configuration diagram of the inserted sequence A, B, C HashEntry objects in a three empty barrel in:

Figure 1. Insert a schematic structural diagram of the tub after three nodes:

NOTE: Since only be inserted in the header, so the opposite order of the list and insert node.

Segment ReentrantLock class inherits from the class, so that the Segment object can act as a lock. Each Segment objects to which daemon (member objects table) contains a number of buckets.

14. Please explain TreeMap?

Investigation Point: key-value set

Reference answer:

TreeMap is an ordered key-value collection , based on NavigableMap achieve red-black tree (Red-Black tree) is . The natural order of their mapping of sorted keys, or sorted according to a Comparator provided at map creation time, depending on the method of construction used.
TreeMap features:
the root is black 
Each node can only be red or black
each leaf node (NIL node, an empty node) is black. 
If a node is red, then it is two children are black, that can not be two red nodes appear on the path.
From any node to all leaves of each path contains the same number of black nodes.

15. Please indicate whether ArrayList would be out of bounds?

Test sites: a collection of

Reference answer:

ArrayList is implemented based on dynamic array data structure, and is based LinkedList linked list data structure

For random access get and set, ArrayList LinkedList superior, because LinkedList to move the pointer;

ArrayList concurrent add () array subscript bounds may appear abnormal .

16. Please explain concurrenthashmap What are the advantages and the difference between 1.7 and 1.8?

Test sites: a collection of

Reference answer:

Concurrenthashmap thread-safe , and 1.7 is by way of Segment + HashEntry in jdk1.7 carried out to achieve, lock added Segment above. 1.7size calculation is to use an unlocked mode, calculating the number of consecutive elements, calculated up to 3 times: 1, twice before and after the results if the same, then the number of elements calculated is accurate; 2, if the front and rear the two results are different, each Segment to be locked, a recalculation of the number of elements;

1.8 Segment bloated design abandoned, replaced by the introduction Node + CAS + Synchronized be implemented to ensure that concurrency-safe, the number of 1.8 using a volatile variable baseCount recording element, when inserting new data or deleting data, baseCount updated by addCount () method, and by accumulating the number baseCount CounterCell array, to obtain the total number of elements;

17. Please explain the underlying implementation of TreeMap?

Test sites: a collection of

Reference answer:

TreeMap is the realization of a red-black tree data structure, also said to be a sort of self-balancing binary tree, so that you can ensure that when the need to quickly retrieve the specified node.

Red-black tree insertion, deletion, are traversed time complexity O (lgN), so that the performance is lower than the hash table. However, the hash table can not provide an orderly output key-value pairs, as is the red-black tree insertion sort, the output can be ordered in accordance with the size of the value of the bond. Red-black tree properties:

Property 1: Each node is either red or black.

Property 2: the root is always black.

Property 3: All leaf nodes are empty nodes (ie null), and black.

Property 4: two children each red node are black. (Not on the path from the root to each leaf has two consecutive red nodes)

Nature 5: a path from any node to each of its sub-tree leaf node contains the same number of black nodes.

18. Please explain what ConcurrentHashMap lock in place plus?

Test sites: a collection of

Reference answer:

Plus in each Segment above.

19. Please explain why HashMap capacity of the n-th power of 2?

Test sites: a collection of

Reference answer:

Default load factor is 0.75, 2 ^ n hash is to make more uniform, for example, in the extreme case the array a hash index, then a hashmap will (1) complex is degraded O O (n) a.

20. Please briefly explain the difference between ArrayList and LinkedList and indicate if the elements have been added in the end of the list, with a high efficiency which way?

Test sites: a collection of

Reference answer:

ArrayList uses an array of arrays to achieve, to find efficient than LinkedList. LinkedList implemented using a doubly linked list, insert and delete efficiency is higher than ArrayList. Has been added at the end of the list of elements, LinkedList higher efficiency.

21. If hashMap the key is a custom class, how to do?

Test sites: a collection of

Reference answer:

Use HashMap, if the key is a custom class, you must override the hashcode () and equals ().

22. Please explain how to achieve specific hashMap?

Test sites: a collection of

Reference answer:

Hashmap array-based implementation, the length of the key is hashcode & array to obtain a position in the array, such as the current array has elements, the array current element next point element to be inserted, so to resolve hash collision, forming a zippered structure. When put in a multithreaded situation, it will form a ring leading to an infinite loop. Typically the array length 2n, numbered from 0, the hashcode & (2n-1), (2n-1) a are each 1, which would allow a uniform hash. Note that, the introduction of the HashMap do red-black tree structure optimization JDK1.8 version, when the number of list elements is equal to 8, is converted into a tree structure list; 6 if the number of elements in the list is less than equal to the tub, the tree structure is reduced to a linked list. Since the average length of the red-black tree lookup is log (n), when a length of 8, the average length of 3, if the list continue to be used, the average length of 8/2 = 4, this has the necessary conversion of the tree. If the chain length is less than or equal 6,6 / 2 = 3, although the speed is fast, but the time into the spanning tree and not too short. 6 and 8 have a choice, there is a difference between the intermediate 7 can effectively prevent frequent switching trees and linked lists. Hypothetically, if the number of the list is designed to be converted into more than eight chain tree structure, linked list is less than the number of converter 8 into a tree structure list, if a HashMap kept inserted, deleted elements, the number of list hovering around 8 to frequently occurring tree turn linked list tree turn, would be very inefficient.

23. Please explain the difference between you and Map of ConcurrentHashMap?

Test sites: a collection of

Reference answer:

hashmap is thread safe, put the time in multiple threads, it will form a ring leading to an infinite loop. CoucurrentHashMap are thread-safe, the lock mechanism using segmentation to reduce the lock granularity.

Guess you like

Origin blog.csdn.net/qq_31194443/article/details/93306445
set
set