Java collections interview questions Set 6
Categories: Java 8(JDK1.8)
What is the difference between HashSet and HashMap?
The differences between the HashSet and HashMap are listed below.
a) HashSet contains only values whereas HashMap includes the entry (key, value). HashSet can be iterated, but HashMap needs to convert into Set to be iterated.
b) HashSet implements Set interface whereas HashMap implements the Map interface
c) HashSet cannot have any duplicate value whereas HashMap can contain duplicate values with unique keys.
d) HashSet contains the only single number of null value whereas HashMap can hold a single null key with n number of null values.
What is the difference between HashMap and TreeMap?
The differences between the HashMap and TreeMap are given below.
a) HashMap maintains no order, but TreeMap maintains ascending order.
b) HashMap is implemented by hash table whereas TreeMap is implemented by a Tree structure.
c) HashMap can be sorted by Key or value whereas TreeMap can be sorted by Key.
d) HashMap may contain a null key with multiple null values whereas TreeMap cannot hold a null key but can have multiple null values.
What is the difference between HashMap and Hashtable?
No.HashMapHashtable
1)HashMap is not synchronized.Hashtable is synchronized.
2)HashMap can contain one null key and multiple null values.Hashtable cannot contain any null key or null value.
3)HashMap is not ?thread-safe,? so it is useful for non-threaded applications.Hashtable is thread-safe, and it can be shared between various threads.
4)4) HashMap inherits the AbstractMap classHashtable inherits the Dictionary class.
What is the difference between Collection and Collections?
The differences between the Collection and Collections are given below.
a) The Collection is an interface whereas Collections is a class.
b) The Collection interface provides the standard functionality of data structure to List, Set, and Queue. However, Collections class is to sort and synchronize the collection elements.
c) The Collection interface provides the methods that can be used for data structure whereas Collections class provides the static methods which can be used for various operation on a collection.
What is the difference between Comparable and Comparator?
No.ComparableComparator
1)Comparable provides only one sort of sequence.The Comparator provides multiple sorts of sequences.
2)It provides one method named compareTo().It provides one method named compare().
3)It is found in java.lang package.It is located in java.util package.
4)If we implement the Comparable interface, The actual class is modified.The actual class is not changed.
What do you understand by BlockingQueue?
BlockingQueue is an interface which extends the Queue interface. It provides concurrency in the operations like retrieval, insertion, deletion. While retrieval of any element, it waits for the queue to be non-empty. While storing the elements, it waits for the available space. BlockingQueue cannot contain null elements, and implementation of BlockingQueue is thread-safe.
What is the advantage of Properties file?
If you change the value in the properties file, you don't need to recompile the java class. So, it makes the application easy to manage. It is used to store information which is to be changed frequently. Consider the following example.
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args)throws Exception{
FileReader reader=new FileReader("db.properties");
Properties p=new Properties();
p.load(reader);
System.out.println(p.getProperty("user"));
System.out.println(p.getProperty("password"));
}
}
Why we override equals() method?
The equals method is used to check whether two objects are the same or not. It needs to be overridden if we want to check the objects based on the property.
For example, Employee is a class that has 3 data members: id, name, and salary. However, we want to check the equality of employee object by the salary. Then, we need to override the equals() method.
How to synchronize List, Set and Map elements?
Yes, Collections class provides methods to make List, Set or Map elements as synchronized:
a) public static List synchronizedList(List l){}
b) public static Set synchronizedSet(Set s){}
c) public static SortedSet synchronizedSortedSet(SortedSet s){}
d) public static Map synchronizedMap(Map m){}
e) public static SortedMap synchronizedSortedMap(SortedMap m){}
What is the advantage of the generic collection?
There are three main advantages of using the generic collection.
a) If we use the generic class, we don't need typecasting.
b) It is type-safe and checked at compile time.
c) Generic confirms the stability of the code by making it bug detectable at compile time.