Why Comparable and Comparator are useful?

In Java, Comparable and Comparator interfaces are useful for sorting the collection of objects. Java has some methods that are useful for sorting primitive types array or Wrapper classes array or list. So, first of all, we will see how we can sort our Primitive data type array and wrapper class array: Output: [1, 2, … Read more

Difference between comparable and comparator

How Comparator useful over comparable Let’s see how Comparator is useful over comparable, Let’s discuss them with different examples. comparator vs comparable Changes in Main Class: As you know Comparable must be implemented in your class for which you want to perform Sorting. So, you need to change your class. For more details on the … Read more

Comparator interface

Here is the table content of the article will we will cover this topic.1. What is the Comparator interface in Java?2. Why do we use Comparator?3. How to use it?4. How is comparable useful over Comparator?5. How is Comparator useful over comparable?6. Why Comparable and Comparator are useful?7. Difference between comparable and comparator? What is … Read more

Comparable java interface

Here is the table content of the article will we will cover this topic1. What is the Comparable java interface?2. Why do we use comparable interface?3. How to use it?4. How is comparable useful over Comparator?5. How is Comparator useful over comparable?6. Why Comparable and Comparator are useful?7. Difference between comparable and comparator? What is … Read more

Difference between hashmap and treemap

We have read about the HashMap and TreeMap in java in recent posts. Both implement the Map interface and provide implementation to store the object based on key and value. Both are used to store the collection of objects based on key-value pairs. But both have some differences and similarities. In this post, we will … Read more

putAll(Map map) method

putAll(Map map): This Java Map putAll method is used to copy all elements of specified map in current TreeMap. It copies all the mapping(Key and value pairs) of specified Map to current TreeMap Where, map is specified Map which you want to copy.return Void, it doesn’t return anything. Output: Names from list = JAVA Names … Read more

isEmpty() method

isEmpty(): This isEmpty Java string method is used to check whether the TreeSet contains any element or not. Basically, it checks the size of elements.  return true, if TreeSet doesn’t contains any element. return false, if TreeSet contains any element. return type:  Its return type is boolean. It can return either true or false. Output: … Read more

put(K key, V value) method

put(K key, V value): This method is used to set the value with the corresponding key in TreeMap. If the TreeMap already contained the key, then the old value is replaced by new Value. Where, K is the key with which the specified value(V) is to be associated in TreeMap.V is the value to be … Read more

lowerKey(K key) method

lowerKey(K key): It returns the key whose key strictly less than the given key, or null if there is no such key.throws ClassCastException,  if specified key toKey is incompatible for TreeMap.NullPointerException, if toKey is null and TreeMap uses natural ordering, or its comparator does not permit null keys. It returns null if TreeMap is empty. Output: … Read more