hereral/Clara-Training-Data huggingface dataset:
Question: What are the Methods of Java ListIterator Interface?

Answer: Method: void add(E e)
Description: This method inserts the specified element into the list.

Method: boolean hasNext()
Description: This method returns true if the list iterator has more elements while traversing the list in the forward direction.

Method: E next()
Description: This method returns the next element in the list and advances the cursor position.

Method: int nextIndex()
Description: This method returns the index of the element that would be returned by a subsequent call to next()

Method: boolean hasPrevious()
Description: This method returns true if this list iterator has more elements while traversing the list in the reverse direction.

Method: E previous()
Description: This method returns the previous element in the list and moves the cursor position backward.

Method: E previousIndex()
Description: This method returns the index of the element that would be returned by a subsequent call to previous().

Method: void remove()
Description: This method removes the last element from the list that was returned by next() or previous() methods

Method: void set(E e)
Description: This method replaces the last element returned by next() or previous() methods with the specified element.
Question: Can you provide an example of ListIterator Interface?

Answer: import java.util.*;  
public class ListIteratorExample1{  
public static void main(String args[]){  
List<String> al=new ArrayList<String>();    
        al.add("Amit");    
        al.add("Vijay");    
        al.add("Kumar");    
        al.add(1,"Sachin");    
        ListIterator<String> itr=al.listIterator();    
        System.out.println("Traversing elements in forward direction");    
        while(itr.hasNext()){    
              
        System.out.println("index:"+itr.nextIndex()+" value:"+itr.next());    
        }    
        System.out.println("Traversing elements in backward direction");    
        while(itr.hasPrevious()){    
          
        System.out.println("index:"+itr.previousIndex()+" value:"+itr.previous());    
        }    
}  
}  

Output:
Traversing elements in forward direction
index:0 value:Amit
index:1 value:Sachin
index:2 value:Vijay
index:3 value:Kumar
Traversing elements in backward direction
index:3 value:Kumar
index:2 value:Vijay
index:1 value:Sachin
index:0 value:Amit
Question: Can you provide an example of List: Book?

Answer: Let's see an example of List where we are adding the Books.

import java.util.*;  
class Book {  
int id;  
String name,author,publisher;  
int quantity;  
public Book(int id, String name, String author, String publisher, int quantity) {  
    this.id = id;  
    this.name = name;  
    this.author = author;  
    this.publisher = publisher;  
    this.quantity = quantity;  
}  
}  
public class ListExample5 {  
public static void main(String[] args) {  
    //Creating list of Books  
    List<Book> list=new ArrayList<Book>();  
    //Creating Books  
    Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);  
    Book b2=new Book(102,"Data Communications and Networking","Forouzan","Mc Graw Hill",4);  
    Book b3=new Book(103,"Operating System","Galvin","Wiley",6);  
    //Adding Books to list  
    list.add(b1);  
    list.add(b2);  
    list.add(b3);  
    //Traversing list  
    for(Book b:list){  
    System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);  
    }  
}  
}  

Output:
101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications and Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6
Question: What are the important points about Java HashSet?

Answer: Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet class and implements Set interface.

The important points about Java HashSet class are:

-HashSet stores the elements by using a mechanism called hashing.
-HashSet contains unique elements only.
-HashSet allows null value.
-HashSet class is non synchronized.
-HashSet doesn't maintain the insertion order. Here, elements are inserted on the basis of their hashcode.
-HashSet is the best approach for search operations.
-The initial default capacity of HashSet is 16, and the load factor is 0.75.
Question: What is the Difference between List and Set?

Answer: A list can contain duplicate elements whereas Set contains unique elements only.
Question: What are the Constructors of the Java HashSet class?

Answer: Sn:1
Constructor: HashSet()
Description: It is used to construct a default HashSet.

Sn:2
Constructor: HashSet(int capacity)
Description: It is used to initialize the capacity of the hash set to the given integer value capacity. The capacity grows automatically as elements are added to the HashSet.

Sn:3
Constructor: HashSet(int capacity, float loadFactor)
Description: It is used to initialize the capacity of the hash set to the given integer value capacity and the specified load factor.

Sn:4
Constructor: HashSet(Collection<? extends E> c)
Description: It is used to initialize the hash set by using the elements of the collection c.
Question: What are the Methods of Java HashSet class?

Answer: Various methods of Java HashSet class are as follows:

Sn:1
Method and Type: boolean
Method:  add(E e)
Description: It is used to add the specified element to this set if it is not already present.

Sn:2
Method and Type: void
Method:  clear()
Description: It is used to remove all of the elements from the set.

Sn:3
Method and Type: object
Method:  clone()
Description: It is used to return a shallow copy of this HashSet instance: the elements themselves are not cloned.

Sn:4
Method and Type: boolean
Method:  contains(Object o)
Description: It is used to return true if this set contains the specified element.

Sn:5 
Method and Type: boolean
Method:  isEmpty()
Description: It is used to return true if this set contains no elements.

Sn:6
Method and Type: Iterator<E>
Method:  iterator()
Description: It is used to return an iterator over the elements in this set.

Sn:7
Method and Type: boolean
Method:  remove(Object o)
Description: It is used to remove the specified element from this set if it is present.

Sn:8
Method and Type: int
Method:  size()
Description: It is used to return the number of elements in the set.

Sn:9
Method and Type: Spliterator<E>
Method:  spliterator()
Description: It is used to create a late-binding and fail-fast Spliterator over the elements in the set.
Question: Can you provide an example of Java HashSet?

Answer: Let's see a simple example of HashSet. Notice, the elements iterate in an unordered collection.

import java.util.*;  
class HashSet1{  
 public static void main(String args[]){  
  //Creating HashSet and adding elements  
    HashSet<String> set=new HashSet();  
           set.add("One");    
           set.add("Two");    
           set.add("Three");   
           set.add("Four");  
           set.add("Five");  
           Iterator<String> i=set.iterator();  
           while(i.hasNext())  
           {  
           System.out.println(i.next());  
           }  
 }  
}  

Five
One
Four
Two
Three
Question: What is the important points about the Java LinkedHashSet Class?

Answer: Java LinkedHashSet class is a Hashtable and Linked list implementation of the Set interface. 

It inherits the HashSet class and implements the Set interface.

The important points about the Java LinkedHashSet class are:
-Java LinkedHashSet class contains unique elements only like HashSet.
-Java LinkedHashSet class provides all optional set operations and permits null elements.
-Java LinkedHashSet class is non-synchronized.
-Java LinkedHashSet class maintains insertion order.
Question: What is the class declaration for LinkedHashSet class?

Answer: Let's see the declaration for java.util.LinkedHashSet class.

public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable
Question: What are the Constructors of the Java LinkedHashSet class?

Answer: Constructor: HashSet()
Description: It is used to construct a default HashSet.

Constructor: HashSet(Collection c)
Description: It is used to initialize the hash set by using the elements of the collection c.

Constructor: LinkedHashSet(int capacity)
Description: It is used to initialize the capacity of the linked hash set to the given integer value capacity.

Constructor: LinkedHashSet(int capacity, float fillRatio)
Description: It is used to initialize both the capacity and the fill ratio (also called load capacity) of the hash set from its argument.
Question: Can you provide an example of Java LinkedHashSet?

Answer: Let's see a simple example of the Java LinkedHashSet class. Here you can notice that the elements iterate in insertion order.

FileName: LinkedHashSet1.java

import java.util.*;  
class LinkedHashSet1{  
 public static void main(String args[]){  
 //Creating HashSet and adding elements  
        LinkedHashSet<String> set=new LinkedHashSet();  
               set.add("One");    
               set.add("Two");    
               set.add("Three");   
               set.add("Four");  
               set.add("Five");  
               Iterator<String> i=set.iterator();  
               while(i.hasNext())  
               {  
               System.out.println(i.next());  
               }  
 }  
}  

Output:
One
Two
Three
Four
Five
Question: What are the important points about the Java TreeSet class?

Answer: Java TreeSet class implements the Set interface that uses a tree for storage. It inherits AbstractSet class and implements the NavigableSet interface. The objects of the TreeSet class are stored in ascending order.

The important points about the Java TreeSet class are:

-Java TreeSet class contains unique elements only like HashSet.
-Java TreeSet class access and retrieval times are quiet fast.
-Java TreeSet class doesn't allow null element.
-Java TreeSet class is non synchronized.
-Java TreeSet class maintains ascending order.
-Java TreeSet class contains unique elements only like HashSet.
-Java TreeSet class access and retrieval times are quite fast.
-Java TreeSet class doesn't allow null elements.
-Java TreeSet class is non-synchronized.
-Java TreeSet class maintains ascending order.
-The TreeSet can only allow those generic types that are comparable. For example The Comparable interface is being implemented by the StringBuffer class.
Question: What is Internal Working of The TreeSet Class?

Answer: TreeSet is being implemented using a binary search tree, which is self-balancing just like a Red-Black Tree. Therefore, operations such as a search, remove, and add consume O(log(N)) time. The reason behind this is there in the self-balancing tree. It is there to ensure that the tree height never exceeds O(log(N)) for all of the mentioned operations. Therefore, it is one of the efficient data structures in order to keep the large data that is sorted and also to do operations on it.
Question: What is Synchronization of The TreeSet Class?

Answer: As already mentioned above, the TreeSet class is not synchronized. It means if more than one thread concurrently accesses a tree set, and one of the accessing threads modify it, then the synchronization must be done manually. It is usually done by doing some object synchronization that encapsulates the set. However, in the case where no such object is found, then the set must be wrapped with the help of the Collections.synchronizedSet() method. It is advised to use the method during creation time in order to avoid the unsynchronized access of the set. The following code snippet shows the same.

TreeSet treeSet = new TreeSet();   
Set syncrSet = Collections.synchronziedSet(treeSet);
Question: What is the class declaration for TreeSet Class?

Answer: Let's see the declaration for java.util.TreeSet class.

public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializable
Question: What are the Constructors of the Java TreeSet Class?

Answer: Constructor: TreeSet()
Description: It is used to construct an empty tree set that will be sorted in ascending order according to the natural order of the tree set.

Constructor: TreeSet(Collection<? extends E> c)
Description: It is used to build a new tree set that contains the elements of the collection c.

Constructor: TreeSet(Comparator<? super E> comparator)
Description: It is used to construct an empty tree set that will be sorted according to given comparator.

Constructor: TreeSet(SortedSet<E> s)
Description: It is used to build a TreeSet that contains the elements of the given SortedSe
Question: Can you provide an example of Java TreeSet?

Answer: Let's see a simple example of Java TreeSet.

FileName: TreeSet1.java

import java.util.*;  
class TreeSet1{  
 public static void main(String args[]){  
  //Creating and adding elements  
  TreeSet<String> al=new TreeSet<String>();  
  al.add("Ravi");  
  al.add("Vijay");  
  al.add("Ravi");  
  al.add("Ajay");  
  //Traversing elements  
  Iterator<String> itr=al.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  
Output:
Ajay
Ravi
Vijay
Question: What is Java Queue Interface?

Answer: The interface Queue is available in the java.util package and does extend the Collection interface. It is used to keep the elements that are processed in the First In First Out (FIFO) manner. It is an ordered list of objects, where insertion of elements occurs at the end of the list, and removal of elements occur at the beginning of the list.

Being an interface, the queue requires, for the declaration, a concrete class, and the most common classes are the LinkedList and PriorityQueue in Java. Implementations done by these classes are not thread safe. If it is required to have a thread safe implementation, PriorityBlockingQueue is an available option.
Question: What is the class declaration for Queue Interface?
Answer: public interface Queue<E> extends Collection<E>
Question: What are the Methods of Java Queue Interface?

Answer: Method:   boolean add(object)
Description: It is used to insert the specified element into this queue and return true upon success.

Method:   boolean offer(object)
Description: It is used to insert the specified element into this queue.

Method:   Object remove()
Description: It is used to retrieves and removes the head of this queue.

Method:   Object poll()
Description: It is used to retrieves and removes the head of this queue, or returns null if this queue is empty.

Method:   Object element()
Description: It is used to retrieves, but does not remove, the head of this queue.

Method:   Object peek()
Description: It is used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
Question: What are the following important  features of a Queue?

Answer: The following are some important features of a queue.

-As discussed earlier, FIFO concept is used for insertion and deletion of elements from a queue.
-The Java Queue provides support for all of the methods of the Collection interface including deletion, insertion, etc.
-PriorityQueue, ArrayBlockingQueue and LinkedList are the implementations that are used most frequently.
-The NullPointerException is raised, if any null operation is done on the BlockingQueues.
-Those Queues that are present in the util package are known as Unbounded Queues.
-Those Queues that are present in the util.concurrent package are known as bounded Queues.
-All Queues barring the Deques facilitates removal and insertion at the head and tail of the queue; respectively. In fact, deques support element insertion and removal at both ends.
Question: What is the class declaration for PriorityQueue?

Answer: Let's see the declaration for java.util.PriorityQueue class.
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
Question: Can you provide an example of Java PriorityQueue?

Answer: FileName: TestCollection12.java

import java.util.*;  
class TestCollection12{  
public static void main(String args[]){  
PriorityQueue<String> queue=new PriorityQueue<String>();  
queue.add("Amit");  
queue.add("Vijay");  
queue.add("Karan");  
queue.add("Jai");  
queue.add("Rahul");  
System.out.println("head:"+queue.element());  
System.out.println("head:"+queue.peek());  
System.out.println("iterating the queue elements:");  
Iterator itr=queue.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
queue.remove();  
queue.poll();  
System.out.println("after removing two elements:");  
Iterator<String> itr2=queue.iterator();  
while(itr2.hasNext()){  
System.out.println(itr2.next());  
}  
}  
}  

Output:
head:Amit
       head:Amit
       iterating the queue elements:
       Amit
       Jai
       Karan
       Vijay
       Rahul
       after removing two elements:
       Karan
       Rahul
       Vijay
Question: What is Java Deque Interface?

Answer: The interface called Deque is present in java.util package. It is the subtype of the interface queue. The Deque supports the addition as well as the removal of elements from both ends of the data structure. Therefore, a deque can be used as a stack or a queue. We know that the stack supports the Last In First Out (LIFO) operation, and the operation First In First Out is supported by a queue. As a deque supports both, either of the mentioned operations can be performed on it. Deque is an acronym for "double ended queue".
Question: What is the class declaration for Deque Interface?


Answer: public interface Deque<E> extends Queue<E>
Question: What are the Methods of Java Deque Interface?

Answer: Method:   boolean add(object)
Description: It is used to insert the specified element into this deque and return true upon success.

Method:   boolean offer(object)
Description: It is used to insert the specified element into this deque.

Method:   Object remove()
Description: It is used to retrieve and removes the head of this deque.

Method:   Object poll()
Description: It is used to retrieve and removes the head of this deque, or returns null if this deque is empty.

Method:   Object element()
Description: It is used to retrieve, but does not remove, the head of this deque.

Method:   Object peek()
Description: It is used to retrieve, but does not remove, the head of this deque, or returns null if this deque is empty.

Method:   Object peekFirst()
Description: The method returns the head element of the deque. The method does not remove any element from the deque. Null is returned by this method, when the deque is empty.

Method:   Object peekLast()
Description: The method returns the last element of the deque. The method does not remove any element from the deque. Null is returned by this method, when the deque is empty.

Method:   Boolean offerFirst(e)
Description: Inserts the element e at the front of the queue. If the insertion is successful, true is returned; otherwise, false.

Method:   Object offerLast(e)
Description: Inserts the element e at the tail of the queue. If the insertion is successful, true is returned; otherwise, false.
Question: What are the important points about ArrayDeque class?

Answer: We know that it is not possible to create an object of an interface in Java. Therefore, for instantiation, we need a class that implements the Deque interface, and that class is ArrayDeque. It grows and shrinks as per usage. It also inherits the AbstractCollection class.

The important points about ArrayDeque class are:

-Unlike Queue, we can add or remove elements from both sides.
-Null elements are not allowed in the ArrayDeque.
-ArrayDeque is not thread safe, in the absence of external synchronization.
-ArrayDeque has no capacity restrictions.
-ArrayDeque is faster than LinkedList and Stack.
Question: What is ArrayDeque Hierarchy?

Answer: The hierarchy of ArrayDeque class is given in the figure displayed at the right side of the page.
Question: What is the class declaration for ArrayDeque class?

Answer: Let's see the declaration for java.util.ArrayDeque class.

public class ArrayDeque<E> extends AbstractCollection<E> implements Deque<E>, Cloneable, Serializable
Question: Can you provide an example Java ArrayDeque?

Answer: FileName: ArrayDequeExample.java

import java.util.*;  
public class ArrayDequeExample {  
   public static void main(String[] args) {  
   //Creating Deque and adding elements  
   Deque<String> deque = new ArrayDeque<String>();  
   deque.add("Ravi");    
   deque.add("Vijay");     
   deque.add("Ajay");    
   //Traversing elements  
   for (String str : deque) {  
   System.out.println(str);  
   }  
   }  
}  
Output:

Ravi
Vijay
Ajay
Question: What is Java Map Interface?

Answer: A map contains values on the basis of key, i.e. key and value pair. Each key and value pair is known as an entry. A Map contains unique keys.

A Map is useful if you have to search, update or delete elements on the basis of a key.
Question: What is Map.Entry Interface?

Answer: Entry is the subinterface of Map. So we will be accessed it by Map.Entry name. It returns a collection-view of the map, whose elements are of this class. It provides methods to get key and value.
Question: What are the Methods of Map.Entry interface?

Answer: Method:   K getKey()
Description: It is used to obtain a key.

Method:   V getValue()
Description: It is used to obtain value.

Method:   int hashCode()
Description: It is used to obtain hashCode.

Method:   V setValue(V value)
Description: It is used to replace the value corresponding to this entry with the specified value.

Method:   boolean equals(Object o)
Description: It is used to compare the specified object with the other existing objects.

Method:   static <K extends Comparable<? super K>,V> Comparator<Map.Entry<K,V>> comparingByKey()
Description: It returns a comparator that compare the objects in natural order on key.

Method:   static <K,V> Comparator<Map.Entry<K,V>> comparingByKey(Comparator<? super K> cmp)
Description: It returns a comparator that compare the objects by key using the given Comparator.

Method:  static <K,V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() 
Description: It returns a comparator that compare the objects in natural order on value.

Method:  static <K,V> Comparator<Map.Entry<K,V>> comparingByValue(Comparator<? super V> cmp) 
Description: It returns a comparator that compare the objects by value using the given Comparator.
Question: Can you provide an example of Java Map: Non-Generic (Old Style)?

Answer: 
//Non-generic  
import java.util.*;  
public class MapExample1 {  
public static void main(String[] args) {  
    Map map=new HashMap();  
    //Adding elements to map  
    map.put(1,"Amit");  
    map.put(5,"Rahul");  
    map.put(2,"Jai");  
    map.put(6,"Amit");  
    //Traversing Map  
    Set set=map.entrySet();//Converting to Set so that we can traverse  
    Iterator itr=set.iterator();  
    while(itr.hasNext()){  
        //Converting to Map.Entry so that we can get key and value separately  
        Map.Entry entry=(Map.Entry)itr.next();  
        System.out.println(entry.getKey()+" "+entry.getValue());  
    }  
}  
}  

Output:
1 Amit
2 Jai
5 Rahul
6 Amit
Question: Can you provide an example of Java Map: Generic (New Style)?

Answer: 
import java.util.*;  
class MapExample2{  
 public static void main(String args[]){  
  Map<Integer,String> map=new HashMap<Integer,String>();  
  map.put(100,"Amit");  
  map.put(101,"Vijay");  
  map.put(102,"Rahul");  
  //Elements can traverse in any order  
  for(Map.Entry m:map.entrySet()){  
   System.out.println(m.getKey()+" "+m.getValue());  
  }  
 }  
}  

Output:
102 Rahul
100 Amit
101 Vijay
Question: What is Java HashMap?

Answer: Java HashMap class implements the Map interface which allows us to store key and value pair, where keys should be unique. If you try to insert the duplicate key, it will replace the element of the corresponding key. It is easy to perform operations using the key index like updation, deletion, etc. HashMap class is found in the java.util package.

HashMap in Java is like the legacy Hashtable class, but it is not synchronized. It allows us to store the null elements as well, but there should be only one null key. Since Java 5, it is denoted as HashMap<K,V>, where K stands for key and V for value. It inherits the AbstractMap class and implements the Map interface.
Points to remember
-Java HashMap contains values based on the key.
-Java HashMap contains only unique keys.
-Java HashMap may have one null key and multiple null values.
-Java HashMap is non synchronized.
-Java HashMap maintains no order.
-The initial default capacity of Java HashMap class is 16 with a load factor of 0.75.
Question: What is the class declaration for HashMap class?

Answer: Let's see the declaration for java.util.HashMap class.

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
Question: What is HashMap class Parameters?

Answer: Let's see the Parameters for java.util.HashMap class.

-K: It is the type of keys maintained by this map.
-V: It is the type of mapped values.
Question: What are the Constructors of the Java HashMap?

Answer: Constructor:    HashMap()
Description: It is used to construct a default HashMap.

Constructor:   HashMap(Map<? extends K,? extends V> m)
Description: It is used to initialize the hash map by using the elements of the given Map object m.

Constructor:   HashMap(int capacity)
Description: It is used to initializes the capacity of the hash map to the given integer value, capacity.

Constructor:   HashMap(int capacity, float loadFactor)
Description: It is used to initialize both the capacity and load factor of the hash map by using its arguments.
Question: Can you provide an example of Java HashMap?

Answer: Let's see a simple example of HashMap to store key and value pair.

import java.util.*;  
public class HashMapExample1{  
 public static void main(String args[]){  
   HashMap<Integer,String> map=new HashMap<Integer,String>();//Creating HashMap    
   map.put(1,"Mango");  //Put elements in Map  
   map.put(2,"Apple");    
   map.put(3,"Banana");   
   map.put(4,"Grapes");   
       
   System.out.println("Iterating Hashmap...");  
   for(Map.Entry m : map.entrySet()){    
    System.out.println(m.getKey()+" "+m.getValue());    
   }  
}  
}  
Iterating Hashmap...
1 Mango
2 Apple
3 Banana
4 Grapes

In this example, we are storing Integer as the key and String as the value, so we are using HashMap<Integer,String> as the type. The put() method inserts the elements in the map.

To get the key and value elements, we should call the getKey() and getValue() methods. The Map.Entry interface contains the getKey() and getValue() methods. But, we should call the entrySet() method of Map interface to get the instance of Map.Entry
Question: What is HashMap?

Answer: HashMap is a part of the Java collection framework. It uses a technique called Hashing. It implements the map interface. It stores the data in the pair of Key and Value. HashMap contains an array of the nodes, and the node is represented as a class. It uses an array and LinkedList data structure internally for storing Key and Value. There are four fields in HashMap.
Question: What is Java LinkedHashMap class?

Answer: Java LinkedHashMap class is Hashtable and Linked list implementation of the Map interface, with predictable iteration order. It inherits HashMap class and implements the Map interface.
Points to remember
-Java LinkedHashMap contains values based on the key.
–Java LinkedHashMap contains unique elements.
Java LinkedHashMap may have one null key and multiple null values.
-Java LinkedHashMap is non synchronized.
-Java LinkedHashMap maintains insertion order.
-The initial default capacity of Java HashMap class is 16 with a load factor of 0.75.
Question: What is the class declaration for LinkedHashMap?

Answer: Let's see the declaration for java.util.LinkedHashMap class.

public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>
Question: What is LinkedHashMap class Parameters?

Answer: Let's see the Parameters for java.util.LinkedHashMap class.
-K: It is the type of keys maintained by this map.
-V: It is the type of mapped values.
Question: What are the Constructors of the Java LinkedHashMap class?

Answer: Constructor: LinkedHashMap()
Description:It is used to construct a default LinkedHashMap.

Constructor:LinkedHashMap(int capacity)
Description:It is used to initialize a LinkedHashMap with the given capacity.

Constructor:LinkedHashMap(int capacity, float loadFactor)
Description:It is used to initialize both the capacity and the load factor.

Constructor:LinkedHashMap(int capacity, float loadFactor, boolean accessOrder)
Description:It is used to initialize both the capacity and the load factor with specified ordering mode.

Constructor:LinkedHashMap(Map<? extends K,? extends V> m)
Description:It is used to initialize the LinkedHashMap with the elements from the given Map class m.
Question: What are the Methods of Java LinkedHashMap class?

Answer: Method:   V get(Object key)
Description: It returns the value to which the specified key is mapped.

Method:   void clear()
Description: It removes all the key-value pairs from a map.

Method:   boolean containsValue(Object value)
Description: It returns true if the map maps one or more keys to the specified value.

Method:   Set<Map.Entry<K,V>> entrySet()
Description: It returns a Set view of the mappings contained in the map.

Method:  void forEach(BiConsumer<? super K,? super V> action) 
Description: It performs the given action for each entry in the map until all entries have been processed or the action throws an exception.

Method:   V getOrDefault(Object key, V defaultValue)
Description: It returns the value to which the specified key is mapped or defaultValue if this map contains no mapping for the key.

Method:   Set<K> keySet()
Description: It returns a Set view of the keys contained in the map

Method: protected boolean removeEldestEntry(Map.Entry<K,V> eldest)  
Description: It returns true on removing its eldest entry.

Method:   void replaceAll(BiFunction<? super K,? super V,? extends V> function)
Description: It replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.

Method:   Collection<V> values()
Description: It returns a Collection view of the values contained in this map.
Question: Can you provide an example of Java LinkedHashMap?

Answer: import java.util.*;  
class LinkedHashMap1{  
 public static void main(String args[]){  
   
  LinkedHashMap<Integer,String> hm=new LinkedHashMap<Integer,String>();  
  
  hm.put(100,"Amit");  
  hm.put(101,"Vijay");  
  hm.put(102,"Rahul");  
  
for(Map.Entry m:hm.entrySet()){  
   System.out.println(m.getKey()+" "+m.getValue());  
  }  
 }  
}  

Output:100 Amit
       101 Vijay
       102 Rahul
Question: What is Java TreeMap class?

Answer: Java TreeMap class is a red-black tree based implementation. It provides an efficient means of storing key-value pairs in sorted order.

The important points about Java TreeMap class are:

-Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
-Java TreeMap contains only unique elements.
-Java TreeMap cannot have a null key but can have multiple null values.
-Java TreeMap is non synchronized.
-Java TreeMap maintains ascending order.
Question: What is the class declaration for TreeMap class?

Answer: Let's see the declaration for java.util.TreeMap class.

public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, Serializable
Question: What is TreeMap class Parameters?

Answer: Let's see the Parameters for java.util.TreeMap class.
-K: It is the type of keys maintained by this map.
-V: It is the type of mapped values.
Question: What are the Constructors of the Java TreeMap class?

Answer: Constructor:TreeMap()
Description:It is used to construct an empty tree map that will be sorted using the natural order of its key.

Constructor:TreeMap(Comparator<? super K> comparator)
Description:It is used to construct an empty tree-based map that will be sorted using the comparator comp.

Constructor:TreeMap(Map<? extends K,? extends V> m)
Description:It is used to initialize a treemap with the entries from m, which will be sorted using the natural order of the keys.

Constructor:TreeMap(SortedMap<K,? extends V> m)
Description:It is used to initialize a treemap with the entries from the SortedMap sm, which will be sorted in the same order as sm.
Question: Can you provide an example of Java TreeMap?

Answer: import java.util.*;  
class TreeMap1{  
 public static void main(String args[]){  
   TreeMap<Integer,String> map=new TreeMap<Integer,String>();    
      map.put(100,"Amit");    
      map.put(102,"Ravi");    
      map.put(101,"Vijay");    
      map.put(103,"Rahul");    
        
      for(Map.Entry m:map.entrySet()){    
       System.out.println(m.getKey()+" "+m.getValue());    
      }    
 }  
}  
Output:100 Amit
       101 Vijay
       102 Ravi
       103 Rahul
Question: What is Java Hashtable class?

Answer: Java Hashtable class implements a hashtable, which maps keys to values. It inherits Dictionary class and implements the Map interface.
Points to remember
-A Hashtable is an array of a list. Each list is known as a bucket. The position of the bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key.
-Java Hashtable class contains unique elements.
-Java Hashtable class doesn't allow null key or value.
-Java Hashtable class is synchronized.
-The initial default capacity of Hashtable class is 11 whereas loadFactor is 0.75.
Question: What is the class declaration for Hashtable class?

Answer: Let's see the declaration for java.util.Hashtable class.

public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable
Question: What is Hashtable class Parameters?

Answer: Let's see the Parameters for java.util.Hashtable class.

-K: It is the type of keys maintained by this map.
-V: It is the type of mapped values.
Question: What are the Constructors of the Java Hashtable class?

Answer: Constructor:Hashtable()
Description:It creates an empty hashtable having the initial default capacity and load factor.

Constructor:Hashtable(int capacity)
Description:It accepts an integer parameter and creates a hash table that contains a specified initial capacity.

Constructor:Hashtable(int capacity, float loadFactor)
Description:It is used to create a hash table having the specified initial capacity and loadFactor.

Constructor:Hashtable(Map<? extends K,? extends V> t)
Description:It creates a new hash table with the same mappings as the given Map.
Question: Can you provide an example of Java Hashtable?

Answer: import java.util.*;  
class Hashtable1{  
 public static void main(String args[]){  
  Hashtable<Integer,String> hm=new Hashtable<Integer,String>();  
  
  hm.put(100,"Amit");  
  hm.put(102,"Ravi");  
  hm.put(101,"Vijay");  
  hm.put(103,"Rahul");  
  
  for(Map.Entry m:hm.entrySet()){  
   System.out.println(m.getKey()+" "+m.getValue());  
  }  
 }  
}  

Output:
103 Rahul
102 Ravi
101 Vijay
100 Amit
Question: What are the Difference between HashMap and Hashtable?

Answer: HashMap and Hashtable both are used to store data in key and value form. Both are using hashing technique to store unique keys.

But there are many differences between HashMap and Hashtable classes that are given below.
1) HashMap is non synchronized. It is not-thread safe and can't be shared between many threads without proper synchronization code while Hashtable is synchronized. It is thread-safe and can be shared with many threads.
2) HashMap allows one null key and multiple null values while Hashtable doesn't allow any null key or value.
3) HashMap is a new class introduced in JDK 1.2 while Hashtable is a legacy class.
4) HashMap is fast while Hashtable is slow.
5) We can make the HashMap as synchronized by calling this code
Map m = Collections.synchronizedMap(hashMap); while Hashtable is internally synchronized and can't be unsynchronized.
6) HashMap is traversed by Iterator while Hashtable is traversed by Enumerator and Iterator. 
7) Iterator in HashMap is fail-fast while Enumerator in Hashtable is not fail-fast.
8) HashMap inherits AbstractMap class while Hashtable inherits Dictionary class.
Question: What is Java EnumSet class?

Answer: Java EnumSet class is the specialized Set implementation for use with enum types. It inherits AbstractSet class and implements the Set interface.
Question: What is the class declaration for EnumSet class?

Answer: Let's see the declaration for java.util.EnumSet class.

public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E> implements Cloneable, Serializable
Question: What are the Methods of Java EnumSet class?

Answer: Method:   static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType)
Description:   It is used to create an enum set containing all of the elements in the specified element type.

Method:   static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c)
Description: It is used to create an enum set initialized from the specified collection.

Method:   static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType)
Description: It is used to create an empty enum set with the specified element type.

Method:   static <E extends Enum<E>> EnumSet<E> of(E e)
Description: It is used to create an enum set initially containing the specified element.

Method:   static <E extends Enum<E>> EnumSet<E> range(E from, E to)
Description: It is used to create an enum set initially containing the specified elements.

Method:   EnumSet<E> clone()
Description: It is used to return a copy of this set.
Question: Can you provide an example of Java EnumSet?

Answer: import java.util.*;  
enum days {  
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY  
}  
public class EnumSetExample {  
  public static void main(String[] args) {  
    Set<days> set = EnumSet.of(days.TUESDAY, days.WEDNESDAY);  
    // Traversing elements  
    Iterator<days> iter = set.iterator();  
    while (iter.hasNext())  
      System.out.println(iter.next());  
  }  
}  

Output:
TUESDAY
WEDNESDAY
Question: What is Java EnumMap class?

Answer: Java EnumMap class is the specialized Map implementation for enum keys. It inherits Enum and AbstractMap classes.
Question: What is the class declaration for EnumMap class?

Answer: Let's see the declaration for java.util.EnumMap class.

public class EnumMap<K extends Enum<K>,V> extends AbstractMap<K,V> implements Serializable, Cloneable
Question: What is EnumMap class Parameters?

Answer: Let's see the Parameters for java.util.EnumMap class.

-K: It is the type of keys maintained by this map.
-V: It is the type of mapped values.
Question: What are the Constructors of the Java EnumMap class?

Answer: Constructor: EnumMap(Class<K> keyType)
Description: It is used to create an empty enum map with the specified key type.

Constructor: EnumMap(EnumMap<K,? extends V> m)
Description: It is used to create an enum map with the same key type as the specified enum map.

Constructor: EnumMap(Map<K,? extends V> m)
Description:It is used to create an enum map initialized from the specified map.
Question: What are the Methods of Java EnumMap class?

Answer: 
Sn:1
Method: clear()
Description: It is used to clear all the mapping from the map.

Sn:2
Method:clone()   
Description: It is used to copy the mapped value of one map to another map.

Sn:3
Method:containsKey()   
Description:It is used to check whether a specified key is present in this map or not. 

Sn:4
Method:containsValue()   
Description: It is used to check whether one or more key is associated with a given value or not.

Sn:5
Method:entrySet()
Description:It is used to create a set of elements contained in the EnumMap. 

Sn:6
Method:equals()
Description: It is used to compare two maps for equality.

Sn:7
Method: get()  
Description: It is used to get the mapped value of the specified key.

Sn:8
Method:   hashCode()
Description: It is used to get the hashcode value of the EnumMap.

Sn:9
Method:keySet()   
Description: It is used to get the set view of the keys contained in the map.

Sn:10
Method:  size() 
Description: It is used to get the size of the EnumMap.

Sn:11
Method:Values()   
Description: It is used to create a collection view of the values contained in this map.

Sn:12
Method:  put() 
Description: It is used to associate the given value with the given key in this EnumMap

Sn:13
Method:  putAll() 
Description: It is used to copy all the mappings from one EnumMap to a new EnumMap.

Sn:14
Method:  remove() 
Description: It is used to remove the mapping for the given key from EnumMap if the given key is present.
Question: Can you provide an example of Java EnumMap?

Answer: 
import java.util.*;  
public class EnumMapExample {  
   // create an enum  
   public enum Days {  
   Monday, Tuesday, Wednesday, Thursday  
   };  
   public static void main(String[] args) {  
   //create and populate enum map  
   EnumMap<Days, String> map = new EnumMap<Days, String>(Days.class);  
   map.put(Days.Monday, "1");  
   map.put(Days.Tuesday, "2");  
   map.put(Days.Wednesday, "3");  
   map.put(Days.Thursday, "4");  
   // print the map  
   for(Map.Entry m:map.entrySet()){    
       System.out.println(m.getKey()+" "+m.getValue());    
      }   
   }  
}  

Output:
Monday 1
Tuesday 2
Wednesday 3
Thursday 4
Question: What is Java Collections class?

Answer: Java collection class is used exclusively with static methods that operate on or return collections. It inherits Object class.

The important points about Java Collections class are:

-Java Collection class supports the polymorphic algorithms that operate on collections.
-Java Collection class throws a NullPointerException if the collections or class objects provided to them are null.
Question: What is the class declaration for Collections class?

Answer: Let's see the declaration for java.util.Collections class.

public class Collections extends Object
Question: Can you provide an example of Java Collections?
Answer: import java.util.*;  
public class CollectionsExample {  
    public static void main(String a[]){      
        List<String> list = new ArrayList<String>();  
        list.add("C");  
        list.add("Core Java");  
        list.add("Advance Java");  
        System.out.println("Initial collection value:"+list);  
        Collections.addAll(list, "Servlet","JSP");  
        System.out.println("After adding elements collection value:"+list);  
        String[] strArr = {"C#", ".Net"};  
        Collections.addAll(list, strArr);  
        System.out.println("After adding array collection value:"+list);  
    }  
}  

Output:
Initial collection value:[C, Core Java, Advance Java]
After adding elements collection value:[C, Core Java, Advance Java, Servlet, JSP]
After adding array collection value:[C, Core Java, Advance Java, Servlet, JSP, C#, .Net]
Question: What is Sorting in Collection?

Answer: We can sort the elements of:
-String objects
-Wrapper class objects
-User-defined class objects
Collections class provides static methods for sorting the elements of a collection. If collection elements are of a Set type, we can use TreeSet. However, we cannot sort the elements of List. Collections class provides methods for sorting the elements of List type elements.
Question: What is the Method of Collections class for sorting List elements?
Answer: public void sort(List list): is used to sort the elements of List. List elements must be of the Comparable type.
Question: Can you provide an example to sort string objects?

Answer: import java.util.*;  
class TestSort1{  
public static void main(String args[]){  
  
ArrayList<String> al=new ArrayList<String>();  
al.add("Viru");  
al.add("Saurav");  
al.add("Mukesh");  
al.add("Tahir");  
  
Collections.sort(al);  
Iterator itr=al.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
 }  
}  
}  

Mukesh
Saurav
Tahir
Viru
Question: What is Java Comparable interface?

Answer: Java Comparable interface is used to order the objects of the user-defined class. This interface is found in java.lang package and contains only one method named compareTo(Object). It provides a single sorting sequence only, i.e., you can sort the elements on the basis of single data member only. For example, it may be rollno, name, age or anything else.
Question: What is compareTo(Object obj) method?

Answer: public int compareTo(Object obj): It is used to compare the current object with the specified object. It returns

-positive integer, if the current object is greater than the specified object.
-negative integer, if the current object is less than the specified object.
-zero, if the current object is equal to the specified object.
Question: What is Java Comparator interface?

Answer: Java Comparator interface is used to order the objects of a user-defined class.

This interface is found in java.util package and contains 2 methods compare(Object obj1,Object obj2) and equals(Object element).

It provides multiple sorting sequences, i.e., you can sort the elements on the basis of any data member, for example, rollno, name, age or anything else.
Question: What are the Methods of Java Comparator Interface?

Answer: Method:   public int compare(Object obj1, Object obj2)
Description: It compares the first object with the second object.

Method:  public boolean equals(Object obj) 
Description: It is used to compare the current object with the specified object.

Method:   public boolean equals(Object obj)
Description: It is used to compare the current object with the specified object.