Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

51.

What All Collection Classes Are Inherently Thread-safe?

Answer»

In the initial COLLECTION classes like Vector, HashTable and Stack all the methods were synchronized to make these classes thread SAFE

Later implementations starting from Java 1.2 were not synchronized. 

Classes in java.util.concurrent package like CONCURRENTHASHMAP, CopyOnWriteArrayList also PROVIDES thread safety but with a different implementation that doesn't require MAKING all the methods synchronized.

In the initial Collection classes like Vector, HashTable and Stack all the methods were synchronized to make these classes thread safe. 

Later implementations starting from Java 1.2 were not synchronized. 

Classes in java.util.concurrent package like ConcurrentHashMap, CopyOnWriteArrayList also provides thread safety but with a different implementation that doesn't require making all the methods synchronized.

52.

Which Collection Classes Implement Random Access Interface?

Answer»

In Java.util package the CLASSES that IMPLEMENT random access INTERFACE are - ARRAYLIST, CopyOnWriteArrayList, Stack, Vector.

In Java.util package the classes that implement random access interface are - ArrayList, CopyOnWriteArrayList, Stack, Vector.

53.

What Is Randomaccess Interface?

Answer»

RandomAccess INTERFACE is a marker interface used by LIST IMPLEMENTATIONS to indicate that they support fast (generally constant TIME) random ACCESS

RandomAccess interface is a marker interface used by List implementations to indicate that they support fast (generally constant time) random access. 

54.

What Is Foreach Statement Added In Java 8?

Answer»

Java 8 has added a functional style lopping to the COLLECTIONS. Real ADVANTAGE of this loop is when it is used on a stream with the chain of functional methods. If you have a Map<STRING, String> then it can be looped using ForEach statement like this - 

Set<Map.Entry<String, String>> valueSet = cityMap.entrySet();

valueSet.forEach((a)->System.out.println("Key is " + a.getKey() + " Value is " + a.getValue()));

Java 8 has added a functional style lopping to the Collections. Real advantage of this loop is when it is used on a stream with the chain of functional methods. If you have a Map<String, String> then it can be looped using ForEach statement like this - 

Set<Map.Entry<String, String>> valueSet = cityMap.entrySet();

valueSet.forEach((a)->System.out.println("Key is " + a.getKey() + " Value is " + a.getValue()));

55.

What Are The Changes In Collections Framework In Java 8?

Answer»

There are several changes in Collections Framework in Java 8 mostly influenced by the inclusion of LAMBDA expression in Java 8 -

  • STREAM API - Stream can be obtained for Collection via the stream() and parallelStream() methods;

    As exp if you have a list of cities in a List and you want to remove the duplicate cities from that list it can be done as
    cityList = cityList.stream().distinct().collect(Collectors.toList());
  • ForEach loop which can be used with Collection. Spliterators which are helpful in PARALLEL processing where several threads can itearate/process part of the collection.
  • New methods are added to Collections like replaceAll, getOrDefault, putIfAbsent in Map.
  • HashMap, LinkedHashMap and ConcurrentHashMap.implementation is changed to reduce hash collisions. Instead of linked list a balanced tree is used to store the ENTRIES after a CERTAIN threshold is reached.

There are several changes in Collections Framework in Java 8 mostly influenced by the inclusion of Lambda expression in Java 8 -

56.

What Is A Diamond Operator?

Answer»

Diamond operator let the compiler infer the TYPE arguments for the generic classes. It is added in Java 7. 

As Example - Before JDK7 if we had to define a Map using STRING as both Key and VALUE we had to WRITE it like this -

Map<String, String&GT; cityMap = new HashMap<String, String>();

In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (<>) known as diamond operator.

Map<String, String> cityMap = new HashMap<>();

Diamond operator let the compiler infer the type arguments for the generic classes. It is added in Java 7. 

As Example - Before JDK7 if we had to define a Map using String as both Key and Value we had to write it like this -

Map<String, String> cityMap = new HashMap<String, String>();

In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (<>) known as diamond operator.

Map<String, String> cityMap = new HashMap<>();

57.

What Is For-each Style Loop?

Answer»

Another feature which was added to COLLECTION with JDK 5 is for-each style loop. Any collection which wants to be the TARGET of the "for-each loop" STATEMENT has to implement iterable interface.

Using for-each loop is easier than constructing the iterator to ITERATE over the collection and can be used in most of the cases RATHER than using the iterator loop.

If you have a list of Strings, that can be iterated using for-each loop like this.

for(String city : cityList){
System.out.println("Name " + city);
}

Another feature which was added to Collection with JDK 5 is for-each style loop. Any collection which wants to be the target of the "for-each loop" statement has to implement iterable interface.

Using for-each loop is easier than constructing the iterator to iterate over the collection and can be used in most of the cases rather than using the iterator loop.

If you have a list of Strings, that can be iterated using for-each loop like this.

for(String city : cityList){
System.out.println("Name " + city);
}

58.

How Generics Changed The Collection Framework?

Answer»

With JDK5 the COLLECTION FRAMEWORK was reengineered to add generics. That was done to add "Type Safety" to the collections. Prior to JDK5 (and generics) elements were stored in collections as Object references, which brought the danger of ACCIDENTALLY storing incompatible types in Collection because for Collections everything was Object reference. Which would have resulted in run time error when trying to retrieve elements and casting them to the desired type. 

With the introduction of generics now we can explicitly tell which data type is to be stored in the collections, which helps in avoiding the run time error. As Collection will throw error at COMPILE time itself for incompatible data type. As exp.

Map<String, String> cityTemperatureMap = NEW LinkedHashMap<String, String>();

Here we are explicitly stating that this LinkedHashMap can only store string as both key and value.

With JDK5 the Collection framework was reengineered to add generics. That was done to add "Type Safety" to the collections. Prior to JDK5 (and generics) elements were stored in collections as Object references, which brought the danger of accidentally storing incompatible types in Collection because for Collections everything was Object reference. Which would have resulted in run time error when trying to retrieve elements and casting them to the desired type. 

With the introduction of generics now we can explicitly tell which data type is to be stored in the collections, which helps in avoiding the run time error. As Collection will throw error at compile time itself for incompatible data type. As exp.

Map<String, String> cityTemperatureMap = new LinkedHashMap<String, String>();

Here we are explicitly stating that this LinkedHashMap can only store string as both key and value.

59.

Name The Core Collection Classes?

Answer»
  • AbstractCollection - Implements most of the Collection interface.
  • AbstractList - Extends AbstractCollection and implements LIST interface.
  • AbstractSequentialList - Extends AbstractList to provide sequential access.
  • ArrayList - Extends AbstractList to provide dynamic array implementation.
  • LinkedList - Extends AbstractSequentialList to implement a linked list.
  • AbstractQueue - Extends AbstractCollection and implements Queue interface.
  • ARRAYDEQUE - Resizable-array implementation of the Deque interface.
  • PriorityQueue - Extends AbstractQueue to provide a priority-based queue.
  • AbstractSet - Extends AbstractCollection and implements Set interface.
  • HashSet - Extends AbstractSet backed by a HASHTABLE (ACTUALLY a HashMap instance).
  • LinkedHashSet - Extends HashSet to provide insertion order interation.
  • TreeSet - Extends AbstractSet (and implements NavigableSet interface) to provide sorted set.
  • EnumSet - Extends AbstractSet for USE with Enum elements.

60.

Describe The Collection Framework Hierarchy?

Answer»

At the root of the COLLECTIONS framework is Collection interface, it must be implemented by any class that defines a collection. This interface declares the core methods that every collection will have, if any class doesn't implement any of the METHOD then it can THROW UnsupportedOperationException.

Then there are List and Set interfaces that extend Collection interface and provided some of its own behaviour that will be further implemented by the classes that implement List and Set interfaces respectively. 

There is also a Queue interface that extends collection to provide behaviour of a queue. On the other HAND there is MAP interface which provides core methods for the Map implementations.

At the root of the Collections framework is Collection interface, it must be implemented by any class that defines a collection. This interface declares the core methods that every collection will have, if any class doesn't implement any of the method then it can throw UnsupportedOperationException.

Then there are List and Set interfaces that extend Collection interface and provided some of its own behaviour that will be further implemented by the classes that implement List and Set interfaces respectively. 

There is also a Queue interface that extends collection to provide behaviour of a queue. On the other hand there is Map interface which provides core methods for the Map implementations.

61.

What Is Java Collections Framework? What Are The Benefits Of Using Collections Framework?

Answer»

JAVA Collections Framework provides a standard way to handle a group of objects. Benefits of the Collections Framework are -

  • High performance, implementation of the Collection classes (ArrayList, LinkedList, HASHSET etc.) are very EFFICIENT and used as-is in most of the cases.
  • Reduced effort as framework already provides several implementations for most of the scenarios that can be used as-is.
  • Provides INTEROPERABILITY, as exp. if you are using List interface any implementation that implements List interface can be swapped with the EXISTING one.
  • If you want to extend any collection that can be easily done using the standard interfaces provided with in the Collections frameworks.

Java Collections Framework provides a standard way to handle a group of objects. Benefits of the Collections Framework are -