Comparable and Comparator both are interfaces and can be used to sort collection elements. There are many differences between Comparable and Comparator interfaces that are mentioned as follows: Comparable in Java Comparator in Java Comparable interface is used to sort the objects with natural ordering. Comparator in Java is used to sort attributes of different objects.…
Category: OCA
HashMap in Java
Here is the list of methods available in HashMap class. I have also covered examples using these methods at the end of this post. void clear(): It removes all the key and value pairs from the specified Map. Object clone(): It returns a copy of all the mappings of a map and used for cloning them…
Comparator interface in Java
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., we can sort the elements on the basis of any data member. This interface is part of java.util package. We can use…
Comparable Interface in Java.
Java Comparable interface used to sort a array or list of objects based on their natural order. Natural ordering of elements is imposed by implementing it’s compareTo() method in the objects.This interface is found in java.lang package and contains only one method named compareTo(Object). This ordering is referred to as the class’s natural ordering, and the class’s compareTo() method is referred to as its natural comparison method.…
Different variables in Java.
In this blogpost we will discuss the different types of variables in Java. But before that lets see the naming conventions which are followed/allowed in Java programming language. Variables naming cannot contain white spaces, for example: int num ber = 100; (invalid) Variable name can begin with special characters such as $ and _. As per the java…
Different Access modifiers in Java: private vs protected vs default vs public
There are two types of modifiers in Java: access modifiers and non-access modifiers. The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it. There are four types of Java access modifiers: Private: The…