1
Original: https://dzone.com/articles/top-java-collection-interview-questions-for-2021
Author: Sonia Mathias
Translation: Zhu Kunrong

Frequently asked questions for experienced developers

Question 1: What is CopyOnWriteArrayList?

Answer: This class is in the java.util.concurrent package and implements the list interface. It has a built-in clone of the internal ArrayList to implement all operations and modifications. It is safe and will never throw ConcurrentModificationException during iteration.

Question 2: When will ConcurrentModificationException occur?

Answer: When the data set is in use, trying to modify it will throw java.util.ConcurrentModificationException, that is, when we are iterating the data set, modify it.

Question 3: Why is ConcurrentHashmap better than Hash table or Synchronized maps?

Answer: ConcurrentHashMap can be safely used in a multi-threaded environment. Its performance is better than the two mentioned in the question, because only a small part of ConcurrentHashMap will be locked, while Hashtable and SynchronizedMap will all be locked.

Question 4: Explain the diamond operator (that is, <>).

Answer: The diamond operator helps the compiler to collect the parameter types of generalized classes. In Java SE, developers can replace the parameterized constructor with an empty parameter set (<>), which is the diamond operator.

Question 5: What do you need to do when you want to use a custom object in the collection class as the primary key?

Answer: If you use a custom object as the primary key in the Map, you need to override the equals() and hashCode() methods. On the other hand, if you use a custom object in a sorted collection, you must ensure that your equals() method is consistent with the compareTo() method.

Question 6: Explain the iterator and collection collections.

Answer: Iterator can only move and read the next element through the next() method, and remove() is the same. Collection, you can add, iterate, delete and clear the entire data structure through add(), iterator(), remove() and clear(). At the same time it also has some boolean methods. Since Iterator has fewer operations compared to collection, it is faster.

Question 7: List some features of HashSet.

Answer: HashSet implements the Set interface. The underlying data structure is Hashtable. Null elements are allowed, duplicate elements are not allowed. Objects are inserted based on their hashcode.

Question 8: Explain the difference between fail-fast and fail-safe

answer:
Fail-fast

  • Use native collection to traverse
  • The collection cannot be modified while the iteration is in progress
  • ConcurrentModicationException will be thrown

Fail-safe

  • Use a copy of the native collection for traversal
  • The collection can be modified during the iteration
  • No exception will be thrown

Question 9: The difference between ArrayBlockingQueue and LinkedBlockingQueue

answer:
ArrayBlockingQueue

  • Bounded blocking queue stores elements in an array
  • Higher throughput
  • Use single-lock double algorithm

LinkedBlockingQueue

  • Optional bounded blocking queue implemented based on linked nodes
  • Lower throughput
  • Use the two-lock queue algorithm

Question 10: What about Java's TreeMap? Which data structure do you prefer to use: HashMap or TreeMap?

Answer: TreeMap is a branch navigation map based on red-black trees. It uses the red-black tree algorithm to sort the object keys of the TreeMap. We choose according to our needs. TreeMap is sorted and HashMap is faster. Therefore, if our elements are ordered, we use TreeMap, otherwise we use HashMap.


This article is from Zhu Kunrong's WeChat public account "Malt Bread", the public account id "darkjune_think"
Developer/Science Fiction Enthusiast/Hardcore Host Player/Amateur Translator
Please specify if reprinted.

Weibo: Zhu Kunrong
Station B: https://space.bilibili.com/23185593/

Communication Email: zhukunrong@yeah.net


祝坤荣
1k 声望1.5k 粉丝

科幻影迷,书虫,硬核玩家,译者