Interview Questions Collection (2) Container

container

 

18. What are the java containers?

 

Catalogue of commonly used containers:

 

19. What is the difference between Collection and Collections?

 

  • java.util.Collection is a collection interface (a top-level interface of the collection class). It provides a common interface method for basic operations on collection objects. The Collection interface has many concrete implementations in the Java class library. The meaning of the Collection interface is to provide a maximum unified operation mode for various specific collections. Its direct inheritance interfaces are List and Set.

  • Collections is a tool / help class for collections, which provides a series of static methods for sorting, searching, and thread-safety operations on elements in the collection.

 

 

20. What is the difference between List, Set and Map?

 

 

 

21. What is the difference between HashMap and Hashtable?

 

  • hashMap removes the contains method of HashTable, but adds the containsValue () and containsKey () methods.

  • HashTable is synchronous, and HashMap is asynchronous, so hashTable is more efficient.

  • HashMap allows empty keys, but hashTable does not.

 

22. How to decide whether to use HashMap or TreeMap?

 

For operations such as inserting, deleting, and locating elements in the Map, HashMap is the best choice. However, if you need to traverse an ordered set of keys, TreeMap is a better choice. Based on the size of your collection, it may be faster to add elements to the HashMap. Change the map to a TreeMap for ordered key traversal.

 

23. What is the implementation principle of HashMap?

 

HashMap overview: HashMap is an asynchronous implementation of the Map interface based on hash tables. This implementation provides all optional mapping operations and allows the use of null values ​​and null keys. This class does not guarantee the order of the mapping, in particular it does not guarantee that the order will not change forever. 

Data structure of HashMap: In the Java programming language, there are two basic structures, one is an array and the other is an analog pointer (reference). All data structures can be constructed using these two basic structures. No exception. HashMap is actually a "linked list hash" data structure, which is a combination of array and linked list.

When we put an element in the Hashmap, first recalculate the hash value according to the hashcode of the key, eradicate the hash value to get the position (subscript) of this element in the array, if the array already stores other elements at that position, then in The element at this position will be stored in the form of a linked list, the newly added is placed at the head of the chain, and the first added is placed at the end of the chain. If there is no element at that position in the array, the element will be directly placed at that position in the array.

It should be noted that the implementation of HashMap is optimized in Jdk 1.8. When the node data in the linked list exceeds eight, the linked list will be converted into a red-black tree to improve query efficiency, from the original O (n) to O (logn)

24. Tell us about the realization principle of HashSet?

 

  • The bottom layer of HashSet is realized by HashMap

  • The value of HashSet is stored on the key of HashMap

  • The value of HashMap is unified as PRESENT

 

25. What is the difference between ArrayList and LinkedList?

 

The most obvious difference is that the underlying data structure of ArrrayList is an array, supporting random access, while the underlying data structure of LinkedList is a doubly circular linked list, which does not support random access. To access an element using subscripts, the time complexity of ArrayList is O (1), while LinkedList is O (n).

26. How to convert between array and list?

 

  • List into an array: call the toArray method of ArrayList.

  • Array conversion into List: call the asList method of Arrays.

27. What is the difference between ArrayList and Vector?

 

  • Vector is synchronous, while ArrayList is not. However, if you seek to make changes to the list during iteration, you should use CopyOnWriteArrayList. 

  • ArrayList is faster than Vector, it will not be overloaded because of synchronization. 

  • ArrayList is more versatile, because we can use the Collections utility class to easily get synchronized lists and read-only lists.

 

28. What is the difference between Array and ArrayList?

 

  • Array can hold basic types and objects, while ArrayList can only hold objects. 

  • Array is a specified size, and ArrayList size is fixed. 

  • Array does not provide the versatility of ArrayList, such as addAll, removeAll, and iterator.

 

29. What is the difference between poll () and remove () in Queue?

 

Both poll () and remove () take an element from the queue, but poll () will return empty when the element fails to get, but will throw an exception when remove () fails.

30. Which collection classes are thread safe?

 

  • vector: There is more synchronization mechanism (thread safety) than arraylist, because of low efficiency, it is not recommended to use now. In web applications, especially the front page, efficiency (page response speed) is often given priority.

  • statck: Stack class, first-in, first-out.

  • hashtable: It is more thread safe than hashmap.

  • enumeration: Enumeration, equivalent to iterator.

 

31. What is Iterator?

 

Iterator is a design pattern, it is an object, it can traverse and select objects in the sequence, and developers do not need to understand the underlying structure of the sequence. Iterators are often referred to as "lightweight" objects because of the low cost of creating it.

 

32. How to use Iterator? What are the characteristics?

 

The Iterator function in Java is relatively simple, and can only move in one direction:

  

(1) Using the method iterator () requires the container to return an Iterator. The first time Iterator's next () method is called, it returns the first element of the sequence. Note: The iterator () method is the java.lang.Iterable interface, inherited by Collection.

  

(2) Use next () to get the next element in the sequence.

  

(3) Use hasNext () to check if there are still elements in the sequence.

  

(4) Use remove () to delete the newly returned element of the iterator.

  

Iterator is the simplest implementation of Java iterator. ListIterator designed for List has more functions. It can traverse List from two directions, and it can insert and delete elements from List.

33. What is the difference between Iterator and ListIterator?

 

  • Iterator can be used to traverse Set and List collection, but ListIterator can only be used to traverse List. 

  • Iterator can only traverse the collection forward, ListIterator can be both forward and backward. 

  • ListIterator implements the Iterator interface and contains other functions, such as: adding elements, replacing elements, obtaining the index of the previous and next elements, and so on.

34. How to ensure that a collection cannot be modified ?

So, how do we ensure that a collection cannot be modified? First of all, we need to be clear that collections (map, set, list ...) are all reference types, so if we modify with final, the contents of the collection can still be modified.

We can do an experiment:

As you can see: we defined a map collection with the final keyword. At this time, we pass a value to the collection. The first key-value pair is 1,1; after we modify it, we can change the value of the key 1 to 100 Explain that we can modify the value of the map collection.

What should we do to ensure that the collection is not modified?
We can use the unmodifiableMap method under the Collections package. The map returned by this method cannot be modified. He will report a java.lang.UnsupportedOperationException error.

Similarly: the Collections package also provides methods for the list and set collections.
Collections.unmodifiableList (List)
Collections.unmodifiableSet (Set)

(Finish)

Published 9 original articles · liked 0 · visits 247

Guess you like

Origin blog.csdn.net/Fabri/article/details/105175000