1. Do you know big-o notation? Can you give examples of different data structures? The big O notation describes the size or performance of the algorithm in the worst-case scenario as the number of elements in the data structure increases.
The big O notation can also be used to describe other behaviors, such as memory consumption. Because collection classes are really data structures, we generally use the big O notation to choose the best implementation based on time, memory, and performance. The big O notation gives a good indication of the performance of large amounts of data.
2. What is a Java Priority Queue? PriorityQueue is an unbounded queue based on the priority heap whose elements are sorted in natural order. At creation time, we can provide it with a comparator that sorts the elements. Priorityqueues do not allow null values because they have no natural order, or they do not have any associated comparators. Finally, PriorityQueue is not thread-safe, with order (log(n)) in and out of queues.
What does the 3.Comparable and Comparator interface do? List the differences. Java provides a Comparable interface that contains only a compareTo() method. This method can sort two objects individually. Specifically, it returns negative numbers, 0, and positive numbers to indicate that the input object is less than, equal to, or greater than an existing object.
Java provides a Comparator interface that contains the compare() and equals() methods. The compare() method is used to sort two input arguments, returning a negative number, 0, with a positive number indicating that the first argument is less than, equal to, or greater than the second argument. The equals() method takes an object as a parameter that determines whether the input parameters are equal to the comparator. This method returns true only if the input parameter is a comparator and the sorting result of the input parameter is the same as the current comparator.
4. What’s the difference between ArrayList and LinkedList? ArrayList and LinkedList both implement the List interface, and they have the following differences:
ArrayList is an index-based data interface, with arrays at its base. It can randomly access elements in order of O(1) time. In contrast, LinkedList stores its data as a list of elements, each linked to its preceding and following element, in which case the time to find an element is O(n).
Compared to ArrayList, LinkedList inserts, adds, and deletes are much faster because there is no need to recalculate or update the index as an array does when an element is added anywhere in the collection.
LinkedList takes up more memory than ArrayList because LinkedList stores two references for each node, one to the previous element and one to the next.
Also see ArrayList vs. LinkedList.
What’s the difference between an Array and an ArrayList? When should you use Array instead of ArrayList? Here are the differences between Array and ArrayList:
Array can contain both primitive and object types, and ArrayList can contain only object types.
The Array size is fixed, whereas the size of an ArrayList changes dynamically.
ArrayList provides more methods and features, such as addAll(), removeAll(), iterator(), and so on.
For primitive type data, collections use automatic boxing to reduce the coding effort. However, this approach is relatively slow when dealing with basic data types of fixed size.
6. What is the difference between HashMap and Hashtable? Both HashMap and Hashtable implement the Map interface, so many features are very similar. However, they have the following differences:
A HashMap allows keys and values to be null, whereas a Hashtable does not.
Hashtable is synchronous, HashMap is not. Therefore, HashMap is better suited for single-threaded environments, while Hashtable is better suited for multi-threaded environments.
A HashMap provides a set of keys to which iterations can be applied, so a HashMap fails quickly. Hashtable, on the other hand, provides Enumeration of keys.
Hashtable is generally thought of as a legacy class.
7. Why are the hashCode() and equals() methods important? Java’s HashMap uses the hashCode() and equals() methods to determine the index of key-value pairs, which are also used when retrieving values based on keys. If these two methods are not implemented correctly, two different keys may have the same hash value and, therefore, may be considered equal by the collection. Also, these two methods are used to find duplicate elements. So the implementation of these two methods is critical to the accuracy and correctness of the HashMap.
8. How does HashMap in Java work? A HashMap in Java stores elements as key-value pairs. A HashMap requires a hash function that uses the hashCode() and equals() methods to add and retrieve elements to/from the collection. When the put() method is called, the HashMap computes the hash value of the key and stores the key-value pair on the appropriate index in the collection. If the key already exists, the value will be updated to the new value.
Some of the important features of HashMap are its capacity, load factor and threshold resizing.
9. What is the difference between fail-fast and fail-safe? Iterator’s security failure is based on making a copy of the underlying collection, so it is not affected by changes made on the source collection. All collection classes under the java.util package fail quickly, while all classes under the java.util. Concurrent package fail safely. Iterators that fail quickly are thrown
ConcurrentModificationException abnormalities, iterators and safety failure never throw this exception.
10. What is the difference between Iterator and ListIterator? Here are the differences:
Iterator can be used to iterate over sets and lists, but ListIterator can only be used to iterate over lists.
Iterator can traverse only forward over sets; ListIterator can traverse both forward and backward.
ListIterator implements the Iterator interface and includes other functions such as adding elements, replacing elements, retrieving the previous and next element indexes, and so on. Jianglogo0521: Scan qr code or manually search wechat official account