Comparator interface in java

Comparator interface: Comparator interface is defined in java.util package. It has two methods named compare(Object obj1,Object obj2) and equals(Object element). Note: In the case of the Comparator interface, we can sort the elements based on multiple properties. Suppose we have Student class elements with name, class, and rollNo as properties then by using the Comparator interface we can … Read more

Comparable interface in java

Comparable interface: Comparable interface is defined in java.lang package. It has only one method namedcompareTo(Object o). It is used to implement the natural ordering of collection elements. String and wrapper classes implement Comparable interface. Note: In the case of Comparable Interface, we can sort the elements based on a single property only. Suppose we have Student class elements with name, class, … Read more

Sorting in collection framework

Sorting: Sorting is a process of arranging data in an increasing or decreasing order. The elements of the String objects, the Wrapper class objects, and the User-defined class objects can be sorted. For sorting the elements of a collection, static methods are provided by the Collections class. As we discussed earlier that TreeSet and TreeMap … Read more

ArrayDeque in java

ArrayDeque: ArrayDeque extends AbstractCollection and implements Deque. ArrayDeque are not thread safe. It not allows the null elements.  Note: ArrayDeque are faster than Stack and LinkedList.  ArrayDeque example: ArrayDequeTest.java import java.util.ArrayDeque; import java.util.Deque; import java.util.Iterator;   /** * This class is used to show the ArrayDeque functionality. * @author w3schools */ public class ArrayDequeTest { … Read more

PriorityQueue in java

Queue Interface To order the element, the Java Queue interface uses FIFO(First In First Out) method, i.e., the first element is removed first and the last element is removed at last. Queue Interface declaration: public interface Queue<E> extends Collection<E> Queue Interface Methods: Method Description boolean add(object) It will insert the specified element into this queue and … Read more

Abstract classes in collection framework

1. AbstractCollection: AbstractCollection implements most of the Collection interface. 2. AbstractList:   AbstractList extends AbstractCollection and implements most of the List interface. 3. AbstractSequentialList: AbstractSequentialList extends AbstractList for use by a collection that uses sequential rather than random access of its elements. 4. AbstractSet: AbstractSet extends AbstractCollection and implements most of the Set interface. 5. … Read more

TreeMap in java

TreeMap: TreeMap extends AbstractMap class and implements the NavigableMap interface. It contains the elements in key-value pair form. It maintains ascending order for its elements. It is not allowed duplicate keys. Note: A TreeMap can have only one null key but can have multiple null values. It is non-synchronized. TreeMap class declaration: public class TreeMap<K,V> extends … Read more

LinkedHashMap in java

LinkedHashMap: LinkedHashMap extends the HashMap class and implements the Map interface. It contains the elements in key-value pair form. It maintains insertion order for its elements. It does not allow duplicate keys. A LinkedHashMap can have only one null key and multiple null values. Its initial default capacity is 16 with a load factor of … Read more

HashMap in java

HashMap: HashMap extends AbstractMap class and implements the Map interface. It contains the elements in key-value pair form. It does not maintain any order for its elements. It is not allowed duplicate keys. A HashMap can have only one null key and multiple null values. No order is maintained by the HashMap class. It is … Read more

LinkedList in java

The LinkedList class extends AbstractSequentialList and implements the List and Deque interface. It uses a linked list data structure to store elements. It can contain duplicate elements. It is not synchronized. Note: It does not provide a random access facility. No shifting needs to occur in the Java LinkedList class, thus manipulation is fast. Doubly … Read more