JAVA

Difference between Comparable and Comparator in Java

Based on my previous posts related to Comparator and Comparable let’s see the difference. Comparable Comparator Should be used when sorting with one element only Should be used when sorting with multiple elements Need to implement compareTo(Obj ob) Need to implement compare(Obj e1, Obj e2) Found in java.lang package Found in java.util package Actual Read more…

By Ashish, ago
JAVA

Sorting in Java (Comparator)

In my previous post we saw Comparable and it’s implementation. Java provides another way to compare objects. Instead of implementing the comparable interface, we can implement the Comparator interface. The main difference between these two interfaces is that the comparable interface defines one method compareTo(), which takes one parameter. The comparator interface defines Read more…

By Ashish, ago
JAVA

Sorting in Java (Comparable)

How do we sort in Java? We can go the traditional way by implementing Bubble Sort or Merge Sort etc We can use Java APIs In this post we will focus on Sorting using Java APIs Sort List of Integer Given an Integer List say [7, 4, 2, 9, 5, Read more…

By Ashish, ago