Original: https://dzone.com/articles/top-java-collection-interview-questions-for-2021
Author: Sonia Mathias
Translation: Zhu Kunrong
In Java, a collection is a framework that provides storage and manipulation of bulk objects. The "collection framework" is defined in JDK1.2, and it provides all collection classes and interfaces. The two main interfaces in the Java collection class are the Collection interface (java.util.Collection) and the Map interface (java.util.Map). The interfaces provided by the Java collection framework include Set, List, Queue, and Deque, and the classes provided include ArrayList, Vector, LinkedList, HashSet, PriorityQueue, TreeSet, and LinkedHashSet.
Need for a separate collection framework
If we do not use the collection framework, the standard method for grouping Java objects is Arrays, Vectors or HashTable. None of them have a common interface. Their implementations are all individually defined and have no relation to each other. Therefore, it is difficult to remember all the different methods, syntax, and creation of functions.
For example, if we want to add an element to Vector, we will use the addElement() method, and to add an element to Hashtable, we will use the put() method.
Benefits of using the collection framework
- Reduce programming burden: A developer can focus on the best use of the collection instead of focusing on the design of the collection. This is good for achieving abstraction.
- Improve programming speed: The collection provides a high-performance implementation of a data structure, which can increase speed.
- Since Java is already a widely used language, organizations large and small are using it. Preparing basic and advanced Java interview questions for yourself can be good for the interview.
Let's take a look at some of the most frequently asked questions in java interviews.
The most asked questions for junior developers
Question 1: What is a framework in Java?
Answer: A framework is a collection of classes and objects that provide scaffolding functions. Ideal object-oriented design should have a framework that provides the same operations for the same types of tasks in the collection.
Question 2: Define the collection framework of Java.
Answer: Java's collection framework is a collection of interfaces and classes that provide efficient methods for saving and processing data. The interfaces provided by the Java collection framework are Set, List, Queue, Deque, and the classes provided include ArrayList, Vector, LinkedList, HashSet, PriorityQueue, TreeSet and LinkedHashSet.
Question 3: The difference between ArrayList and Vector in the Java collection framework.
answer:
ArrayList
- Not synchronized
- It can expand its size by 50% of its array size
- It is not thread safe
- It is not a legacy
Vector: - It is synchronized
- It can double its size
- It is thread safe
- It is a legacy
- Question 4: The difference between Iterator and Enumeration.
answer:
Iterator - It can traverse legacy and non-legacy classes
- It is slower than Enumeration
- It can perform remove operations when traversing the collection
- It is fail-fast
Enumeration
- It can only traverse the legacy elements
- It is faster than Iterator
- It can only execute traverse on the collection
- It is not fail-fast
Question 5: What is the difference between LinkedList and ArrayList?
answer:
ArrayList
- This class implements the list interface
- This class uses a dynamic array to store elements
- The best complexity of insertion and removal operations is O(1), and the worst complexity is O(n). Query operations (such as storing elements of a specific index) will take O(1) time.
- ArrayList is better at storing and querying data.
LinkedList
- This class implements the list interface and the deque interface.
- This type of storage element uses a double-linked list.
- Insertion and removal operations provide O(1) performance. Query operations (such as querying an element of a specific index) require O(n) time.
- LinkedList performs better when manipulating stored data.
- Question 6: Explain the difference between the poll() and remove() methods of the Queue interface.
Answer: Both methods return and remove the content at the head of the queue. They only behave differently when the queue is empty; remove() will throw an exception and poll() will return null.
Question 7: Comparable is different from Comparator.
answer:
Comparable
- It provides the compareTo() method used by the sorted elements
- It belongs to the java.lang package
- The sorting logic must be in the same class as the objects we want to sort
- It provides a separate sorting sequence.
- The actual class has been changed
Comparator
- It provides a compare() method for sorting elements
- It is in the java.util package
- The sorting logic must be in different classes in order to write sorting methods according to different attributes of different objects
- It provides a variety of sorting sequences
- The actual class has not been changed
Question 8: What is the definition of Stack in computer memory?
Answer: Stack is a special area in computer memory that is used to store temporary variables created by functions. In the stack, variables are declared, stored and initialized at runtime.
Question 9: List the collection view of the map interface.
answer:
Collection view (Collection view) method can make the Map in the following
Three ways are regarded as a collection:
- Key set view: Set collection of all keys saved in the Map.
- Value collection view: The collection of all the values saved in the Map. This set is not a Set, because different primary keys can be mapped to the same value.
- Entry collection view: Set of key-value pairs in the Map. The Map interface provides a small embedded interface Map.Entry, and the elements are stored in this Set.
Question 10: Define EnumSet.
Answer: It is possible to use the Set implementation of enum enumeration type. All elements must belong to a specific enum type. It is not synchronized. NULL keys are not allowed.
Question 11: What method can make the collection thread-safe?
Answer: These methods are:
- Collections.synchronizedList(list);
- Collections.synchronizedMap(map);
- Collections.synchronizedSet(set);
- Question 12: The difference between Queue and Deque
answer:
Queue - One-way queue
- The elements in the queue are all at the same end when they are added or deleted
- Less versatile
Deque - Deque
- Elements in the queue can be added from the end of the queue or added and deleted from both ends.
- More versatile
Question 13: The difference between hashmap and hashtable
answer:
Hashmap
- Not synchronized, not thread safe
- Inherited the AbstractMap class
- One null key and multiple null values are allowed.
- Can be traversed by iterator
Hashtable - Synchronized, thread safe
- Inherited the Dictionary class
- No empty key or value allowed
- Can be traversed by enumerator and iterator
Question 14: Define Iterator.
Answer: Iterator() is an interface that provides methods for traversing collections. It provides a universal way to traverse the elements in the collection and implements the iterator design pattern.
Question 15: What is a navigable map?
Answer: The NavigableMap interface, a member of the Java collection framework, belongs to the java.util package. It is a sub-interface of SortedMap and provides convenient navigation methods such as lowerKey, floorKey, ceilingKey and higherKey. It also provides a way to create a sub-map from an existing map.
Question 16: What is the peek() of the queue interface?
Answer: Peek() returns the head of the queue. It does not remove any elements. When the queue is empty, it returns null.
To be continued
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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。