About the Java collection framework interview questions (with answers)

  • 2020-04-01 04:30:29
  • OfStack

1. What is the Java collections framework? What are some of the advantages of a collection framework?

There are collections in each programming language, and the original Java version contained several collection classes: Vector, Stack, HashTable, and Array. With the widespread use of collections, Java1.2 proposes a collection framework that encompasses all collection interfaces, implementations, and algorithms. Java has a long history of using generic and concurrent collection classes while still being thread-safe. It also includes blocking interfaces and their implementation in Java and send packages. Some of the advantages of the collection framework are as follows:

(1) reduce development costs by using core collection classes instead of implementing our own collection classes.

(2) code quality improves with the use of rigorously tested collection framework classes.

(3) code maintenance costs can be reduced by using the collection classes shipped with the JDK.

(4) reusability and operability.

2. What are the advantages of generics in the collection framework?

Java1.5 introduces generics, which are heavily used by all collection interfaces and implementations. Generics allow us to provide an object type for a collection that we can hold, so if you add any element of another type, it will fail in the compilation. This avoids a ClassCastException at run time, because you will get an error message at compile time. Generics also keep the code clean, and we don't need to use explicit conversions and the instanceOf operator. It also benefits the runtime because there are no type checking bytecode instructions.

3. What are the basic interfaces of the Java collection framework?

Collection is the root interface of the Collection hierarchy. A collection represents a set of objects that are its elements. The Java platform does not provide any direct implementation of this interface.

A Set is a Set that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent the set, like a deck of CARDS.

A List is an ordered collection that can contain repeating elements. You can access any element through its index. A List is more like an array of dynamically varying lengths.

A Map is an object that maps a key to a value. A Map cannot contain duplicate keys: each key can Map at most one value.

Some other interfaces are Queue, Dequeue, SortedSet, SortedMap, and ListIterator.

4. Why does the Collection not inherit from the Cloneable and Serializable interfaces?

The Collection interface specifies a set of objects that are its elements. How these elements are maintained depends on the specific implementation of the Collection. For example, some Collection implementations, such as List, allow repeating elements, while others, such as Set, do not. Many Collection implementations have a common clone method. However, it doesn't make sense to put it in all implementations of the collection. This is because a Collection is an abstract representation. The important thing is implementation.

The semantics and meaning of cloning or serialization come into play when working with concrete implementations. So, the implementation should decide how to clone or serialize it, or whether it can be cloned or serialized.

Authorizing cloning and serialization in all implementations ultimately results in less flexibility and more restrictions. The specific implementation should determine whether it can be cloned and serialized.

5. Why does the Map interface not inherit from the Collection interface?

Although the Map interface and its implementation are part of the collections framework, maps are not collections and collections are not maps. Therefore, Map inheritance collections make no sense, and vice versa.

If Map inherits the Collection interface, where does the element go? A Map contains key-value pairs, which provide a way to extract a collection of key or value lists, but it does not fit the "set of objects" specification.

6. What is an Iterator?

The Iterator interface provides an interface to traverse any Collection. Iterator instances can be obtained from a Collection using iterator methods. Iterators replace Enumeration in the Java collections framework. Iterators allow the caller to remove elements during iteration.

7. What is the difference between the Enumeration and Iterator interfaces?

Enumeration is twice as fast as Iterator and USES less memory. Enumeration is very basic, and it meets the basic needs. However, compared to Enumeration, Iterator is safer because it prevents other threads from modifying the collection while it is being traversed.

Iterators replace Enumeration in the Java collections framework. Iterators allow callers to remove elements from a collection, which Enumeration cannot. The iterator method name has been improved to make its functionality clearer.

8. Why aren't there methods like iterator.add () to add elements to the collection?

The semantics are unclear, and it is known that Iterator's protocol does not guarantee the order of iterations. Note, however, that ListIterator does not provide an add operation, which ensures the order of the iterations.

9. Why doesn't the iterator have a way to get the next element directly without moving the cursor?

It can be implemented at the top level of the current Iterator, but it's used so little that if you add it to the interface, it doesn't make sense for each inheritance to implement it.

10. What's the difference between Iterater and ListIterator?

(1) we can use Iterator to traverse Set and List collections, while ListIterator can only traverse lists.

(2) Iterator can only traverse forward, while LIstIterator can traverse in both directions.

(3) ListIterator inherits from 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.

11. What are the different ways to traverse a List?


List<String> strList = new ArrayList<>();
//Use the for-each loop
for(String obj : strList){
 System.out.println(obj);
}
//using iterator
Iterator<String> it = strList.iterator();
while(it.hasNext()){
 String obj = it.next();
 System.out.println(obj);
}

Using an iterator is more thread-safe because it ensures that it will be thrown when the currently traversed collection element is changed ConcurrentModificationException .

12. What do you learn from the iterator fail-fast property?

Each time we try to get the next element, the Iterator fail-fast attribute checks for any changes in the current collection structure. If you find any changes, it throw ConcurrentModificationException. All implementations of Iterator in the Collection are designed as fail-fast (except for concurrent Collection classes such as ConcurrentHashMap and CopyOnWriteArrayList).

13. What's the difference between fail-fast and fail-safe?

Iterator's fail-fast property works with the current collection, so it is unaffected by any changes in the collection. All collection classes in the java.utill package are designed to be fail-fast, while the collection classes in java.util.concurrent are fail-safe. Fail - fast iterator throw ConcurrentModificationException and Fail - safe never throw ConcurrentModificationException iterator.

14. At the time of iteration a collection, how to avoid ConcurrentModificationException?

When traversing a set, we can use the concurrent collection classes to avoid ConcurrentModificationException, such as using CopyOnWriteArrayList, rather than the ArrayList.

15. Why is there no concrete implementation of the Iterator interface?

The Iterator interface defines methods for traversing a collection, but its implementation is the responsibility of the collection implementation class. Each collection class that can return an Iterator for traversal has its own Iterator implementation inner class.

This allows the collection class to select whether the iterator is fail-fast or fail-safe. For example, the ArrayList iterator is fail-fast, while the CopyOnWriteArrayList iterator is fail-safe.

16. What is UnsupportedOperationException?

UnsupportedOperationException Is an exception to indicate that the operation is not supported. Has been widespread use in the JDK classes, in the Java Collections framework. Util. Collections. UnmodifiableCollection will throw in all the add and remove operations this exception.

17. How does a HashMap work in Java?

HashMap stores key-value pairs in the map.entry static inner class implementation. HashMap USES the hash algorithm, and in the put and get methods, it USES the hashCode() and equals() methods. When we call the put method by passing the key-value pair, the HashMap USES the key hashCode() and hash algorithm to find the index that stores the key-value pair. Entry is stored on LinkedList, so if it exists, it USES the equals() method to check if the key passed already exists, if so, it overwrites the value, if not, it creates a new Entry and saves it. When we call the get method by passing the key, it again USES hashCode() to find the index in the array, then USES the equals() method to find the correct Entry, and then returns its value. The following pictures explain the details.

Other important issues with HashMap are capacity, load factor, and threshold adjustment. The default initial capacity of HashMap is 32 and the load factor is 0.75. The threshold is the load factor times the capacity, and whenever we try to add an entry, if the map is larger than the threshold, the HashMap rehashes the contents of the map with more capacity. Capacity is always a power of two, so if you know you need to store a lot of key-value pairs, such as caching data pulled from a database, it's a good idea to initialize a HashMap with the right capacity and load factor.

18. What is the importance of the hashCode() and equals() methods?

HashMap USES the Key object's hashCode() and equals() methods to determine the index of the key-value pair. These methods are also used when we try to get values from a HashMap. If the methods are not implemented correctly, in which case two different keys might produce the same hashCode() and equals() output, the HashMap would assume they were the same and override them instead of storing them in different places. Similarly, all collection classes that are not allowed to store duplicate data use hashCode() and equals() to find duplicates, so it's important to implement them correctly. The implementation of equals() and hashCode() should follow the following rules:

(1) if o1.equals(o2), then o1.hashcode () == o2.hashcode () is always true.

(2) if o1.hashcode () == o2.hashcode (), it does not mean that o1.equals(o2) will be true.

19. Can we use any class as the Map key?

We can use any class as a Map key, but before we use them, we need to consider the following:

(1) if the class overrides the equals() method, it should also override the hashCode() method.

(2) all instances of the class need to follow rules related to equals() and hashCode(). Please refer to the above mentioned rules.

(3) if a class does not use equals(), you should not use it in hashCode().

(4) the best practice for user-defined 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() will not change in the future, which will solve the problem of mutable.

For example, I have a class MyKey that I use in a HashMap.


//The name parameter passed to MyKey is used in equals() and hashCode()
MyKey key = new MyKey('Pankaj'); //assume hashCode=1234
myHashMap.put(key, 'Value');
//The following code changes the key's hashCode() and equals() values
key.setName('Amit'); //assume new hashCode=7890
//Null is returned because the HashMap tries to find the key that stores the same index, but the key has been changed, the match fails, and null is returned
myHashMap.get(new MyKey('Pankaj'));

That's why strings and integers are heavily used as keys for HashMap.

20. What different views of collections does the Map interface provide?

The Map interface provides three collection views:

(1) Set keyset() : Returns a Set view of all keys contained in the map. Collections are supported by maps, and map changes are reflected in the collection, and vice versa. When an iterator is traversing a collection, if the map is modified (in addition to the iterator's own removal operation), the iterator's result becomes undefined. Collections support element removal through the Remove, set.remove, removeAll, retainAll, and clear operations of the Iterator, removing the corresponding map from the map. It does not support the add and addAll operations.

(2) Collection values() : Returns a Collection view of all values contained in a map. This collection is supported by a map, and changes to the map are reflected in the collection, and vice versa. When an iterator is traversing a collection, if the map is modified (in addition to the iterator's own removal operation), the iterator's result becomes undefined. Collections support element removal through the Remove, set.remove, removeAll, retainAll, and clear operations of the Iterator, removing the corresponding map from the map. It does not support the add and addAll operations.

(3) Set< Map. Entry< K, V> > EntrySet () : Returns a collection view of all maps contained by a map clock. This collection is supported by a map, and changes to the map are reflected in the collection and vice versa. When an iterator is traversing a collection, the iterator's result becomes undefined if the map is modified (in addition to the iterator's own removal operation and the setValue of the entry returned by the iterator). Collections support element removal through the Remove, set.remove, removeAll, retainAll, and clear operations of the Iterator, removing the corresponding map from the map. It does not support the add and addAll operations.

This is about the Java set framework interview topic, is there a certain understanding of the Java set framework, the next to share the next article for you (link: #).


Related articles: