To sort the objects of user-defined classes, a comparator interface is utilized. A comparator object can compare two items of the same type.This interface may be found in java.util package and includes two methods: compare(Object obj1,Object obj2) equals (Object element). It supports different sorting sequences, which means you can sort the items based on any […]
August 6, 2022 | Java | No comments
The Comparable interface is used to compare an object of the same class with an instance of that class; it also enables data ordering for user-defined class objects. The class has to implement the java.lang.Comparable interface to compare its instance, it provides the compareTo() method, which accepts an object of that class as an argument. Its sorting algorithm is determined by the type of object used by the interface. If the object type is a string, it is sorted lexicographically. Else, If the object type is a wrapper class object, such as an integer […]
August 5, 2022 | Java | No comments
The Collections class belongs to the Java Collections Framework. The package java.util.Collections is the package containing the Collections class. The Collections class mainly uses static methods that operate on or return collections. If the collection or object supplied to the methods is null, all the methods in this class throw the NullPointerException. Declaration: Collections Methods: […]
August 5, 2022 | Java | No comments
Along with the AbstractMap Class, the TreeMap class in Java is used to implement the Map interface and NavigableMap. Depending on which constructor is used, the map is sorted based on the natural ordering of its keys or by a Comparator specified at map creation time. TreeMap class declaration: where, K: It is the type of […]
August 2, 2022 | Java | No comments
The LinkedHashSet class is an ordered HashSet with a doubly-linked List across all entries. This class is used when the iteration order must be maintained. The order of the elements in a HashSet is unpredictable, whereas a LinkedHashSet allows us to iterate through them in the order in which they were inserted. Declaration of LinkedHashSet: […]
July 30, 2022 | Java | No comments
The Java HashSet class is used to create a collection that stores data in a hash table. It derives from AbstractSet and implements the Set interface. This class allows for the null element. The class also provides constant time performance for fundamental operations such as add, delete, contains, and size, assuming the hash function properly […]
July 29, 2022 | Java | No comments
The Collection framework is found in java.util package includes Linked List. This class is an implementation of the LinkedList data structure, a linear data structure where each element is a separate object with a data part and address part and is not kept in a single location. Pointers and addresses are used to connect the […]
July 24, 2022 | Java | No comments
The java.util package contains the collection foundation component ArrayList. In Java, it offers us dynamic arrays. Although it might be slower than conventional arrays, it can be useful in programs that require a lot of array manipulation. The java.util package contains this class. Java ArrayList class Declaration: crucial things to keep in mind: Duplicate elements […]
July 23, 2022 | Java | No comments
You will frequently wish to repeat the collection’s pieces. You might want to show each element, for instance. Using an iterator—an object that implements either the Iterator or the ListIterator interface—is the simplest way to accomplish this. You can cycle over a collection using an iterator, adding or removing elements as you go. ListIterator is […]
July 22, 2022 | Java | No comments
The Java Collections Framework includes the Collections class in the Java.util.Collections package. The static methods that work with collections or return collections are essentially utilized with the collections class. If the collection or object supplied to the methods is null, all the methods of this class raise the NullPointerException. Syntax: Collections class fields: Following are the […]
July 22, 2022 | Java | No comments