The Comparable interface, present in java.lang package provides a method compareTo(T o), which is used to compare one object with the other.It returns a value that is negative, zero, or positive depending upon whether the argument is less than, equal to, or greater than the other object.The ordering specified by this interface is said to be the nat...

Read More

You implement the Comparable interface and you establish the natural ordering of the objects. Now, what if there is a need of comparing the objects based on some other attribute of the object ? Let’s take the example of the person class that we have used in our previous example. What if in a particular scenario there is a necessity of ordering pers...

Read More

In the previous post we have discussed about Comparable and Comparator interface in Java and how they can be used.you can check it here. In this post we will take a look at some examples of using these interfaces to sort Arrays and Collections.We will use the following Person class for the example. public class Person implements Comparable { p...

Read More

In the previous post we have discussed about sorting an array using Comparable interface in Java. In this post we will take a look at how we can sort and ArrayList using Comparable.We will use the following Person class for the example. public class Person implements Comparable { private Integer personId; private String personName; pub...

Read More

In this post we will see how to sort an Array of Person Objects using Comparator.We will use the following Person class for the example. public class Person implements Comparable { private Integer personId; private String personName; public Integer getPersonId() { return personId; } public void setPersonId(Integer perso...

Read More

In the previous post we have discussed about sorting an array using Comparator interface in Java. In this post we will take a look at how we can sort and ArrayListusing Comparator.We will use the following Person class for the example. public class Person implements Comparable { private Integer personId; private String personName; publ...

Read More

FOLLOW US ON LinkedIn



Explore Tutu'rself