Core Java
How does a Hashtable internally maintain the key-value pairs?
Nov 26th
TreeMap actually implements the SortedMap interface which extends the Map interface. In a TreeMap the data will be sorted in ascending order of keys according to the natural order for the key’s class, or by the comparator provided at creation time. TreeMap is based on the Red-Black tree data structure.
What is a KeySet View ?
Nov 26th
KeySet is a set returned by the keySet() method of the Map Interface, It is a set that contains all the keys present in the Map.
What is an EntrySet View ?
Nov 26th
Entry Set view is a set that is returned by the entrySet() method in the map and contains Objects of type Map. Entry each of which has both Key and Value.
What Are the different Collection Views That Maps Provide?
Nov 26th
Values Collection View is a collection returned by the values() method of the Map Interface, It contains all the objects present as values in the map.
How do you sort an ArrayList (or any list) of user-defined objects ?
Nov 26th
Create an implementation of the java.lang.Comparable interface that knows how to order your objects and pass it to java.util.Collections.sort(List, Comparator).
What is the Comparable interface ?
Nov 26th
The Comparable interface is used to sort collections and arrays of objects using the Collections.sort() and java.utils.Arrays.sort() methods respectively. The objects of the class implementing the Comparable interface can be ordered.
The Comparable interface in the generic form is written as follows:
interface Comparable<T>
where T is the name of the type parameter.
All classes implementing the Comparable interface must implement the compareTo() method that has the return type as an integer. The signature of the compareTo() method is as follows:
int i = object1.compareTo(object2)
- If object1 < object2: The value of i returned will be negative.
- If object1 > object2: The value of i returned will be More >
What is static block?
Nov 26th
Static block which exactly executed exactly once when the class is first loaded into JVM. Before going to the main method the static block will execute.
What are the differences between the Comparable and Comparator interfaces ?
Nov 26th
Comparable
Comparato
It uses the compareTo() method.int objectOne.compareTo(objectTwo). t uses the compare() method.
int compare(ObjOne, ObjTwo) It is necessary to modify the class whose instance is going to be sorted. A separate class can be created in order to sort the instances. Only one sort sequence can be created. Many sort sequences can be created. It is frequently used by the API classes. It used by third-party classes to sort instances.