Soon to this year’s gold three silver four, is a good season for job hopping, ready to jump students are ready to face several, this small make up for you to prepare a selection of Java set interview questions, quick to check the leak fill the gap.

JavaOOP, Java collection container, Java Exceptions, concurrent programming, Java reflection, Java serialization, JVM, Redis, Spring MVC, MyBatis, MySQL database, message middleware MQ, Dubbo, Linux, ZooKeeper, distributed & data structure and algorithm and other 25 special technical points, are xiaobian in each big factory summed up the interview question, there are a lot of fans rely on this PDF to win many big factory offer, Today, I am here to summarize and share with you! 【 Finished 】

Full version of Java interview questions address: 2021 latest interview questions collection collection.

The serial number project content link
1 The middleware Java Middleware Interview Questions (Latest version 2021) Juejin. Cn/post / 694870…
2 Micro service Java Micro Service Test Questions (updated version 2021) Juejin. Cn/post / 694906…
3 Concurrent programming Java Concurrent Programming Interview Questions (Updated 2021) Juejin. Cn/post / 695053…
4 Java based Java Basics Test Questions (2021 latest edition) Juejin. Cn/post / 695062…
5 Spring Boot Spring Boot Interview Questions (Updated 2021) Juejin. Cn/post / 695137…
6 Redis Redis Interview Questions (Updated 2021) Juejin. Cn/post / 695166…
7 Spring MVC Spring MVC Interview Questions (updated 2021) Juejin. Cn/post / 695166…
8 Spring Cloud Spring Cloud Interview Questions (Updated 2021) Juejin. Cn/post / 695245…
9 MySQL optimization MySQL optimized Interview Questions (2021 update) Juejin. Cn/post / 695246…
10 JVM JVM Performance Tuning Interview Questions (Latest version 2021) Juejin. Cn/post / 695246…
11 Linux Linux Interview Questions (Latest edition, 2021) Juejin. Cn/post / 695287…
12 Mybatis Mybatis Interview Questions (latest version 2021) Juejin. Cn/post / 695287…
13 Network programming TCP, UDP, Socket, Http Network Programming Juejin. Cn/post / 695287…
14 Design patterns Design Mode Interview Questions (latest version 2021) Juejin. Cn/post / 695544…
15 Big data 100 Big Data Interview Questions (the latest version of 2021) Juejin. Cn/post / 695544…
16 Tomcat Tomcat Interview Questions (latest version 2021) Juejin. Cn/post / 695570…
17 multithreading Multi-threaded Interview Questions (Latest edition 2021) Juejin. Cn/editor/draf…
18 Nginx Nginx_BIO_NIO_AIO Interview Questions (Updated 2021) Juejin. Cn/editor/draf…
19 memcache Memcache Interview Questions (Updated 2021) Juejin. Cn/post / 695608…
20 Java exception Java Exception Interview Questions (latest edition, 2021) Juejin. Cn/post / 695644…
21 The Java virtual machine Java Virtual Machine Interview Questions (Latest version 2021) Juejin. Cn/post / 695658…
22 Java collection Java Collection Interview Questions (Latest edition 2021) Juejin. Cn/post / 695684…
23 Git Common Commands Git Common Commands (updated 2021) Juejin. Cn/post / 695692…
24 Elasticsearch Elasticsearch Interview Questions (Updated 2021) Juejin. Cn/post / 695840…
25 Dubbo Dubbo Interview Questions (Latest edition 2021) Juejin. Cn/post / 695842…

Overview of collection containers

1. What is a set

  • A collection is a container for data, specifically a container for data object references
  • Collection classes store references to objects, not the objects themselves
  • There are three types of collection types: set, list, and Map.

2. Features of collections

The characteristics of a set are as follows:

  • A collection is a container for storing objects, objects are used to encapsulate data, and more objects need to be centrally managed.
  • The size of the object compared to the array is uncertain. Because the set is of variable length. Arrays need to be sized in advance

3. The difference between collections and arrays

  • Arrays are fixed length; Set of variable length.
  • Arrays can store either basic or reference data types. Collections can only store reference data types.
  • The elements stored in an array must be of the same data type; Collections can store objects of different data types.

4. Benefits of using the collections framework

  1. Self-increasing capacity;
  2. Provide high-performance data structure and algorithm, make coding easier, improve the speed and quality of the program;
  3. Collections can be easily extended or rewritten to improve code reusability and operability.
  4. By using the collection classes that come with the JDK, you can reduce the cost of code maintenance and learning new apis.

5. What are the common collection classes?

The Map and Collection interfaces are the parent interfaces of all collections frameworks:

  • The subinterfaces of the Collection interface include the Set interface and the List interface
  • The implementation classes of the Map interface include HashMap, TreeMap, Hashtable, ConcurrentHashMap, and Properties
  • The implementation classes of Set interface mainly include: HashSet, TreeSet, LinkedHashSet and so on
  • List interface implementation classes are: ArrayList, LinkedList, Stack and Vector, etc

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

7. Set the underlying data structure of the framework

8. Which collection classes are thread-safe?

  • Vector: there is one more synchronized than Arraylist, which is less recommended because it is less efficient.
  • HashTable: it has more synchronized than hashMap and is not recommended.
  • ConcurrentHashMap: Java5 thread safe HashMap implementation that supports high concurrency and throughput. It consists of a Segment array structure and a HashEntry array structure. The Segment array acts as a lock in the ConcurrentHashMap, and the HashEntry is used to store key-value pairs. A ConcurrentHashMap contains an array of segments. The Segment structure is an array and a linked list structure similar to a HashMap. A Segment contains an array of HashEntries. Each HashEntry is an element of a linked list structure. Each Segment guards a element in a HashEntry array. Any change to the data in a HashEntry array must first acquire the Segment lock. (Recommended)

9. “fail-fast” for Java collections?

10. How do I ensure that a collection cannot be modified?

  • You can create a read-only Collection by using the collections.unmodififiAblecollection (Collection C) method, This change of set any operation will throw Java. Lang. UnsupportedOperationException anomalies.
  • The example code is as follows:
List<String> list = new ArrayList<>(); list. add("x"); Collection<String> clist = Collections. unmodifiableCollection(list); clist. add("y"); System.out.println (list.size ()); system.out.println (list.size ());Copy the code

2. Collection interface

The List interface

What is an Iterator?

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

Iterator uses the following code:

List<String> list = new ArrayList<>();
Iterator<String> it = list. iterator();
while(it. hasNext()){
String obj = it. next();
System. out. println(obj);
Copy the code

The characteristics of the Iterator is only one-way traverse, but safer, because it can ensure that, in the current traverse a collection of elements are changed, will be thrown ConcurrentModifificationException anomalies.

13. How to remove elements from a Collection while iterating?

What’s the difference between an Iterator and a ListIterator?

  • An Iterator can iterate over sets and lists, while a ListIterator can only iterate over lists.
  • An Iterator can only traverse one way, while a ListIterator can traverse both ways (forward/back).
  • ListIterator implements the Iterator interface and then adds some additional functionality, such as adding an element, replacing an element, and getting the index position of the preceding or following elements.

15. What are the different ways to traverse a List? How does each approach work? What are the best practices for List traversal in Java?

16. Describe the pros and cons of ArrayList

17. How to achieve the conversion between array and List?

  • Array-to-list: Use arrays.aslist (array) for conversion.
  • List toArray: use the List’s own toArray() method.
  • Example code:
// list to array
List<String> list = new ArrayList<String>();
// array to list
String[] array = new String[]{"123","456"};
Copy the code

18. What’s the difference between ArrayList and LinkedList?

  • Data structure implementation: ArrayList is a data structure implementation of dynamic arrays, while LinkedList is a bidirectional LinkedList data structure implementation.
  • Random access efficiency: ArrayList is more efficient at random access than LinkedList. Because LinkedList is a linear data store, you need to move the pointer to look up from front to back.
  • Add and delete efficiency: LinkedList is more efficient than ArrayList for add and delete operations that are not at the beginning or end, because ArrayList add and delete operations affect the subscripts of other data in the array.
  • Memory footprint: LinkedList takes up more memory than ArrayList, because in addition to the data, the LinkedList node stores two references, one to the previous element and one to the next.
  • Thread safety: ArrayList and LinkedList are not synchronized, that is, they are not thread-safe;
  • In general, ArrayList is more recommended when you need to read the elements of a collection frequently, and LinkedList is more recommended when you need to do a lot of inserts and deletes.
  • A two-way LinkedList, also known as a double-linked list, is a type of LinkedList in which each data node has two Pointers, one to the direct successor and the other to the direct precursor. Therefore, starting from any node in the bidirectional linked list, it is easy to access its predecessors and successors.

What is the difference between an ArrayList and a Vector?

20. Who is faster to insert data? ArrayList, LinkedList, or Vector? ArrayList, Vector, LinkedList

  • The underlying implementation of Both ArrayList and Vector uses arrays to store data. The number of elements in an array is larger than the actual stored data so that elements can be added and inserted. Both allow the elements to be indexed directly by their ordinal number, but inserting an element involves memory operations such as moving an array element, so indexing is fast and inserting is slow.
  • Vector is a thread-safe container because of the addition of the synchronized modifier to its methods, but it performs worse than ArrayList.
  • LinkedList uses bidirectional LinkedList to achieve storage, according to the serial number index data need to carry out forward or backward traversal, but when inserting data only need to record the front and back items of the current item, so LinkedList insertion speed.

21. How to use ArrayList in multithreaded scenarios?

  • ArrayList is not thread-safe, and if you encounter a multithreaded scenario, you can use the Collections’ synchronizedList method to convert it into a thread-safe container for later use. For example, it looks like this:
List<String> synchronizedList = Collections.synchronizedList(list);
for (int i = 0; i < synchronizedList.size(); i++) {
Copy the code

22. Why is ArrayList elementData transient?

  • The array in ArrayList is defined as follows:

private transient Object[] elementData;

  • Look again at the definition of ArrayList:
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable,
Copy the code
  • You can see that ArrayList implements the Serializable interface, which means that ArrayList supports serialization. The transient is used to say that we do not want the elementData array to be serialized, overriding the writeObject implementation:
private void writeObject( s) throws{ *// Write out element count, and any hidden stuff* int expectedModCount = modCount; s.defaultWriteObject(); *// Write out array length* s.writeint(elementData.length); *// Write out all elements in the proper order.* for (int i=0; i<size; i++) s.writeObject(elementData[i]); if (modCount ! = expectedModCount) { throw new ConcurrentModificationException(); }Copy the code
  • For each serialization, we first call the defaultWriteObject() method to serialize the non-transient elements in the ArrayList, and then iterate through elementData to serialize only the elements that have been stored. This accelerates serialization. And reduced the size of the serialized file.

23. The difference between List and Set

The Set interface

24. How does HashSet work?

  • The value of a HashSet is stored on the key of a HashMap. The value of a HashMap is presented. Therefore, the implementation of a HashSet is relatively simple. Basically, this is done by directly calling the related methods of the underlying HashMap, and hashsets do not allow duplicate values.

25. How does HashSet check for duplications? How does a HashSet ensure that the data is not repeatable?

  • When adding () elements to a HashSet, determine whether an element exists by comparing not only the hash value, but also the Equles method of comparison.
  • The add () method in a HashSet uses the Put () method of a HashMap.
  • If K/V is the same in a HashMap, the old V will be overwritten with the new V, and the old V will be returned. So there is no repetition (HashMap compares keys first to Hashcode and then to equals).
  • Here is the source code for the HashSet part:
private static final Object PRESENT = new Object(); private transient HashMap<E,Object> map; public HashSet() { map = new HashMap<>(); } public Boolean add(E E) {return map.put(E,PRESENT)==null; }Copy the code

26. The difference between HashSet and HashMap

3. Map interface

27. What is a Hash algorithm

  • A hash algorithm maps a binary of any length to a smaller binary of fixed length, called a hash value.

28. What is a linked list

29. How does HashMap work?

30. What are the differences between JDK1.7 and JDK1.8 HashMap? The underlying implementation of HashMap

31. What is a red-black tree

So red black trees what is a binary tree

  • A binary tree simply means that each node can be associated with two child nodes

Red and black tree

  • Red – black tree is a special binary search tree. Every node in a red-black tree has a little bit of storage to indicate the color of the node, which can be Red or Black.
  • Every node in a red-black tree is black or red. When the root is black anyway. Every leaf is also black. Note: A leaf is a NIL or NULL leaf! .
  • If a node is red, its children must be black.
  • Each node passes the same number of black nodes to NIL. Make sure that none of the paths is twice as long as the others, so red-black trees are relatively close to equilibrium binary trees!
  • The basic operations of a red-black tree are add and delete. The rotation method is used after adding or removing a red-black tree. Why is that? The reason is very simple. After adding or deleting nodes in a red-black tree, the structure of the red-black tree changes. It may not satisfy the above three properties, so it is no longer a red-black tree, but an ordinary tree. And by rotating and changing color, you can make the tree red and black again. Simply put, the purpose of the rotation and color change is to keep the tree red-black.

32. What is the process of putting a HashMap?

public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); Final V putVal(int hash, K key, V value, Boolean onlyIfAbsent, Boolean evict) {Node<K,V>[] TAB; Node<K,V> p; int n, i; // Step 1: TAB is empty, create / / table uninitialized or length is 0, expansion the if ((TAB = table) = = null | | (n = TAB. Length) = = 0) n = (TAB = the resize ()). The length; // Step 2: // (n-1) & hash to determine which bucket the element is stored in. The bucket is empty and the new node is put into the bucket. (In this case, If ((p = TAB [I = (n-1) &hash]) == null) TAB [I] = newNode(hash, key, value, null); Else {Node<K,V> e; K k; / / steps (3) : the key nodes exist, directly covered the value / / more barrels (nodes) in array the first element in the hash values are equal, the key is equal to the if (p.h ash = = hash && ((k = p.k ey) = = key | | (key! = null && key.equals(k)))) // assign the first element to e, e = p; // Step 4: Determine that the chain is a red-black tree // Hash values are not equal, that is, keys are not equal; // If the current element type is TreeNode, it is a red-black tree, Else if (p instanceof TreeNode) else if (p instanceof TreeNode) e = ((TreeNode<K,V>)p).putTreeval (this, TAB, TAB) hash, key, value); Else {// insert node at the end of the list for (int binCount = 0; ; ++binCount) {if ((e = p.ext) == null) {// insert newNode p.ext = newNode(hash, key, value, hash); null); If (binCount >= treeify_threshold_1) // -1 for 1st // convert the list to treeifyBin(TAB, hash); // Break the loop; } / / judgment chain the key value node in the table and insert the elements of the key values are equal if (e.h ash = = hash && ((k = e.k ey) = = key | | (key! = null && key.equals(k)))) // equals); // Select * from bucket where p = e; // Select * from bucket where p = e; } // Return the new value if (e! = null) {// record e's value V oldValue = e.value; // If (! OnlyIfAbsent | | oldValue = = null) / / replace the old with the new value value e.v alue = value; // Call afterNodeAccess(e) after access; // Return oldValue; }} // Structure change ++modCount; If (++size > threshold) resize(); if (++size > threshold) resize(); // Insert through the insertion line (evict); return null; }Copy the code
  1. Check whether the key-value array tablei is empty or null. Otherwise, run resize() to expand the capacity.
  2. If tablei==null, add a new node and go to ⑥. If tablei is not null, go to ③. If tablei is not null, go to ③.
  3. Check whether the first element of tablei is the same as key. If it is the same, override value, otherwise go to ④.
  4. Determine whether tablei is treeNode, that is, whether tablei is a red-black tree. If it is a red-black tree, insert key-value pairs directly into the tree; otherwise, go to 5.
  5. Traverse tablei to determine whether the length of the list is greater than 8. If it is greater than 8, convert the list into a red-black tree and insert into the red-black tree. Otherwise, insert into the list; During the traversal, if the key has directly overwritten the value, it is necessary.
  6. Check whether the actual number of key pairs exceeds the maximum capacity threshold. If the number exceeds the threshold, expand the capacity.

33. How to expand HashMap capacity?

final Node<K,V>[] resize() { Node<K,V>[] oldTab = table; Int oldCap = (oldTab == null)? 0 : oldTab.length; int oldThr = threshold; int newCap, newThr = 0; If (oldCap > 0) {// If (oldCap >= MAXIMUM_CAPACITY) {// If (oldCap >= MAXIMUM_CAPACITY); Threshold = integer.max_value; return oldTab; } else if (newCap = oldCap << 1) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY) newThr = oldThr << 1; // The old capacity is 0, but the threshold is greater than zero, which means that the cap is passed in. Threshold is initialized to the NTH power of 2 else if (oldThr > 0) // Initial capacity was placed in threshold newCap = oldThr; // Create a map with no parameters. Given the default capacity and threshold 16, So {// zero initial threshold using defaults newCap = DEFAULT_INITIAL_CAPACITY; newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); } // new threshold = newCap * 0.75 if (newThr == 0) {float ft = (float)newCap * loadFactor; newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ? (int)ft : Integer.MAX_VALUE); } threshold = newThr; Table @suppresswarnings ({"rawtypes","unchecked"}) Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; // Create an array of hash buckets. Table = newTab; // Copy the values of the new array to the old hash bucket array // If the original array is not initialized, then the initial chemical of the resize is finished. Otherwise, the element reorder logic is used to spread it evenly if (oldTab! = null) {for (int j = 0; j < oldCap; ++j) { Node<K,V> e; if ((e = oldTab[j]) ! OldTab [j] = null) {oldTab[j] = null; oldTab[j] = null; oldTab[j] = null; If (e.ext ==null) // Use the same hash mapping algorithm to add the element to the new array newTab[e.ash & (newcap-1)] = e; // If e is a TreeNode and e.ext! Else if (e instanceof TreeNode) ((TreeNode<K,V>)e).split(this, newTab, j, oldCap); // e is the header of the list and e.ext! Else {// preserve order // loHead,loTail =null Node<K,V> loHead =null,loTail =null; // Node<K,V> hiHead = null, hiTail = null; // Node<K,V> hiHead = null; Node<K,V> next; Do {next = e.ext; If (e.ash & oldCap) == 0) {if (loTail == null) // initialize head to refer to the current element e, e is not necessarily the first element of the list, LoHead = e; loHead = e; Next = e; // lotail. next = e; // After initialization, loTail and loHead point to the same memory, so when lotail. next refers to the next element, the next reference to the element in the underlying array changes accordingly. Cause lowHead. Next. Next... // Follow the loTail synchronization, so that the lowHead can link to all elements that belong to the list. loTail = e; } else {if (hiTail == null) // initialize head to refer to the current element e, hiHead = e; else = e; hiTail = e; } } while ((e = next) ! = null); // At the end of the traversal, set tail to null and put the list head in the corresponding index of the new array to form a new mapping. if (loTail ! = null) { = null; newTab[j] = loHead; } if (hiTail ! = null) { = null; newTab[j + oldCap] = hiHead; } } } } } return newTab; }Copy the code

34. How does HashMap resolve hash conflicts?

  • A: To solve this problem, we need to know what a hash conflict is, and we need to know what a hash is before we can understand a hash conflict.

35. Can I use any class as a Map key?

You can use any class as a Map key, but before you do, consider the following:

  • If the class overrides the equals() method, it should also override the hashCode() method.
  • All instances of a class need to follow the rules related to equals() and hashCode().
  • If a class does not use equals(), it should not be used in hashCode().
  • The best practice for custom Key classes is to make them immutable so that hashCode() values can be cached for better performance. Immutable classes also ensure that hashCode() and equals() don’t change in the future, which solves the problems associated with mutable.

36. Why are wrapper classes like String and Integer in HashMap appropriate as K?

37. What should I do if I use Object as the Key of a HashMap?

38. Why not use the hash value of hashCode() as the index of the table directly?

39. Why is the length of a HashMap a power of two

What is the difference between a HashMap and a HashTable?

  1. Thread-safe: HashMap is non-thread-safe, HashTable is thread-safe; The methods inside a HashTable are basically synchronized. (Use ConcurrentHashMap if you want to be thread safe);
  2. Efficiency: HashMap is slightly more efficient than HashTable because of thread-safety issues. In addition, HashTable is basically obsolete, so don’t use it in your code; (Use ConcurrentHashMap if you want to be thread safe);
  3. Support for Null keys and Null values: In a HashMap, Null can be used as a key, and there can be only one such key, and there can be one or more keys with Null values. A NullPointerException is thrown whenever a key is put in a HashTable with a NULL.
  4. The difference between the initial capacity size and each expansion capacity size:
  5. If you do not specify an initial size when creating the Hashtable, the default initial size of the Hashtable is 11, and then each time it is expanded, the original capacity is 2n+1. The default initialization size of a HashMap is 16. And then each time you expand it, it doubles.
  6. Given an initial size when created, the Hashtable will use that size, and the HashMap will expand it to a power of two. That is to say, a HashMap always uses a power of two as the size of the hash table, and we’ll see why that is later.
  7. Underlying data structure: Since JDK1.8, HashMap has changed a lot in solving hash conflicts. When the length of the list is larger than the threshold (default is 8), the list is transformed into a red-black tree to reduce the search time. Hashtable has no such mechanism.
  8. Recommended use: As you can see in the class comment for Hashtable, Hashtable is a reserved class and is not recommended. Instead, use HashMap in single-threaded environments, or ConcurrentHashMap if multiple threads are required.

41. What is TreeMap Introduction

  • TreeMap is an ordered set of key-values, which is implemented through red-black trees.
  • TreeMap is based on a red-black tree. The map is sorted by the natural order of its keys, or by the Comparator provided when the map was created, depending on the constructor used.
  • TreeMap is thread asynchronous.

42. How do I decide whether to use HashMap or TreeMap?

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

43. The difference between HashMap and ConcurrentHashMap

  1. ConcurrentHashMap divides the entire bucket array into segments, and locks each of the segments, which are more granular than HashTable’s synchronized locks. HashMap has no locking mechanism. It’s not thread safe. (After JDK1.8, ConcurrentHashMap is implemented in a completely new way, using the CAS algorithm.)
  2. A HashMap allows null key-value pairs, but ConCurrentHashMap does not.

44. What is the difference between ConcurrentHashMap and Hashtable?

3, JDK1.8 ConcurrentHashMap (TreeBin: redblack binary tree Node: linked list Node) :

45. Do you know the underlying implementation of ConcurrentHashMap? How does it work?

  • If the Node is not empty and the Node is not currently moving, a synchronized lock is applied to the Node, and if the hash of the Node is not less than 0, the list is traversed to update the Node or insert a new Node.
if (fh >= 0) {
	binCount = 1;
	for (Node<K,V> e = f;; ++binCount) {
		K ek;
		if (e.hash == hash &&
		((ek = e.key) == key ||
		(ek != null && key.equals(ek)))) {
			oldVal = e.val;
			if (!onlyIfAbsent)
			e.val = value;
		Node<K,V> pred = e;
		if ((e = == null) { = new Node<K,V>(hash, key, value, null);
Copy the code
  1. If the node is a TreeBin type node, it is a red-black tree structure, then use the putTreeVal method to insert the node into the red-black tree; If the binCount is not 0, it indicates that the put operation has affected the data. If the number of linked lists reaches 8, the tree is converted to a red-black tree by using the treeifyBin method. If oldVal is not empty, it indicates that it is an update operation and has no impact on the number of elements, then the old value is returned directly.
  2. If a new node is inserted, the addCount() method tries to update the number of elements baseCount;

Four, auxiliary tools class

46. What is the difference between Array and ArrayList?

  • An Array can store basic data types and objects. An ArrayList can only store objects.
  • Arrays are specified to a fixed size, whereas ArrayList sizes are automatically expanded.
  • Array has fewer built-in methods than ArrayList, such as addAll, removeAll, iteration, and so on.

47. How to convert between Array and List?

  • Array to List: Arrays.aslist (Array);
  • List toArray: toArray() method of List.

What’s the difference between comparable and comparable?

  • The Comparable interface is actually from the Java.lang package and has a compareTo(Object OBj) method for sorting
  • The Comparator interface is actually from the java.util package and has a compare(Object obj1, Object obj2) method for sorting
  • In general, when we want to use a custom sorting method for a collection, we override compareTo or compare. When we want to use two sorting methods for a collection, for example, a song object with a song name and a singer name, We can override the compareTo method and use our own Comparator method, or we can use two comparators to sort by song name and singer name. The second means we can only use the two-parameter version of collections.sort ().

49. What’s the difference between Collection and Collections?

  • Java.util. Collection is a Collection interface (a top-level interface of the Collection class). It provides common interface methods for performing basic operations on collection objects. The Collection interface has many concrete implementations in Java class libraries. The meaning of the Collection interface is to provide the maximum unified operation mode for various specific collections, and its direct inheritance interfaces are List and Set.
  • Collections is a utility/helper class for Collections classes that provides a series of static methods for sorting, searching, and thread-safe operations on the elements of Collections.

50. How do TreeMap and TreeSet compare elements when sorting? How does the sort() method in the Collections utility class compare elements?

What’s the difference between Collection and Collections?

  • Java.util. Collection is a Collection interface (a top-level interface of the Collection class). It provides common interface methods for performing basic operations on collection objects. The Collection interface has many concrete implementations in Java class libraries. The meaning of the Collection interface is to provide the maximum unified operation mode for various specific collections, and its direct inheritance interfaces are List and Set.
  • Collections is a utility/helper class for Collections classes that provides a series of static methods for sorting, searching, and thread-safe operations on the elements of Collections.