Java Colletions Interview Questions
Java Colletions Interview Questions
Interfaces
Classes
Algorithm
All these classes and interfaces support various operations such as Searching,
Sorting, Insertion, Manipulation, and Deletion which makes the data
manipulation really easy and quick.
3. Describe the Collection hierarchy in Java.
4. List down the primary interfaces provided by Java Collections
Framework?
Below are the major interfaces provided by the Collection Framework:
Known as Universal Java Cursor as it is applicable for all the classes of the
Collection framework
Assignments
Lifetime Access
Explore Curriculum
1Arrays.asList(item)
Whereas an ArrayList can be converted into an Array using the toArray() method
of the ArrayList class.
Syntax:
1List_object.toArray(new String[List_object.size()])
14. How will you reverse an List?
ArrayList can be reversed using the reverse() method of the Collections class.
Syntax:
1public static void reverse(Collection c)
For Example:
1public class ReversingArrayList {
2public static void main(String[] args) {
3List<String> myList = new ArrayList<String>();
4myList.add("AWS");
5myList.add("Java");
6myList.add("Python");
7myList .add("Blockchain");
8System.out.println("Before Reversing");
9System.out.println(myList.toString());
10Collections.reverse(myList);
11System.out.println("After Reversing");
12System.out.println(myList);
13}
14}
15. What do you understand by LinkedList in Java? How many
types of LinkedList does Java support?
LinkedList in Java is a data structure that contains a sequence of links. Here each
link contains a connection to the next link.
Syntax:
1Linkedlist object = new Linkedlist();
Java LinkedList class uses two types of LinkedList to store the elements:
Singly Linked List: In a singly LinkedList, each node in this list stores the
data of the node and a pointer or reference to the next node in the list.
Doubly Linked List: In a doubly LinkedList, it has two references, one to
the next node and another to the previous node.
Vector is synchronized.
Vector contains many legacy methods that are not part of the collections
framework.
Syntax:
1Vector object = new Vector(size,increment);
Next
19. What is a priority queue in Java?
A priority queue in Java is an abstract data type similar to a regular queue or
stack data structure but has a special feature called priority associated with each
element. In this queue, a high priority element is served before a low priority
element irrespective of their insertion order. The PriorityQueue is based on the
priority heap. The elements of the priority queue are ordered according to the
natural ordering, or by a Comparator provided at queue construction time,
depending on which constructor is used.
20. What is the Stack class in Java and what are the various
methods provided by it?
Java Stack class is an important part of the Java Collection framework and is
based on the basic principle of last-in-first-out. In other words, the elements are
added as well as removed from the rear end. The action of adding an element to
a stack is called push while removing an element is referred to as pop. Below are
the various methods provided by this class:
Set – Java Collections Interview Questions
21. What is Set in Java Collections framework and list down its
various implementations?
A Set refers to a collection that cannot contain duplicate elements. It is mainly
used to model the mathematical set abstraction. The Java platform provides
three general-purpose Set implementations which are:
1. HashSet
2. TreeSet
3. LinkedHashSet
22. What is the HashSet class in Java and how does it store
elements?
java.util.HashSet class is a member of the Java collections framework which
inherits the AbstractSet class and implements the Set interface. It implicitly
implements a hashtable for creating and storing a collection of unique elements.
Hashtable is an instance of the HashMap class that uses a hashing mechanism
for storing the information within a HashSet. Hashing is the process of
converting the informational content into a unique value that is more popularly
known as hash code. This hashcode is then used for indexing the data
associated with the key. The entire process of transforming the informational
key into the hashcode is performed internally.
23. Can you add a null element into a TreeSet or HashSet?
In HashSet, only one null element can be added but in TreeSet it can’t be added
as it makes use of NavigableMap for storing the elements. This is because the
NavigableMap is a subtype of SortedMap that doesn’t allow null keys. So, in case
you try to add null elements to a TreeSet, it will throw a NullPointerException.
24. Explain the emptySet() method in the Collections framework?
The Collections.emptySet() is used to return the empty immutable Set while
removing the null elements. The set returned by this method is serializable.
Below is the method declaration of emptySet().
Syntax:
1public static final <T> Set<T> emptySet()
25. What is LinkedHashSet in Java Collections Framework?
A java.util.LinkedHashSet is a subclass of the HashSet class and implements the
Set interface. It is an ordered version of HashSet which maintains a doubly-
linked List across all elements contained within. It preserves the insertion order
and contains only unique elements like its parent class.
Syntax:
1LinkedHashSet<String> hs = new LinkedHashSet<String>();
Map – Java Collections Interview Questions
26. What is Map interface in Java?
The java.util.Map interface in Java stores the elements in the form of keys-values
pairs which is designed for faster lookups. Here every key is unique and maps to
a single value. These key-value pairs are known as the map entries. This
interface includes method signatures for insertion, removal, and retrieval of
elements based on a key. With such methods, it’s a perfect tool to use for key-
value association mapping such as dictionaries.
27. Why Map doesn’t extend the Collection Interface?
The Map interface in Java follows a key/value pair structure whereas the
Collection interface is a collection of objects which are stored in a structured
manner with a specified access mechanism. The main reason Map doesn’t
extend the Collection interface is that the add(E e) method of the Collection
interface doesn’t support the key-value pair like Map interface’s put(K, V)
method. It might not extend the Collection interface but still is an integral part of
the Java Collections framework.
28. List down the different Collection views provided by the Map
interface in the Java Collection framework?
The Map interface provides 3 views of key-value pairs which are:
The class overriding the equals() method must also override the
hashCode() method
The class should adhere to the rules associated with equals() and
hashCode() for all instances
The class field which is not used in the equals() method should not be
used in hashCode() method as well