Collection Basics
What is Collection
Why Collection ?
Collection API
Collection Hierarchy
List
What is List
What is ArrayList
What is LinkedList
What is Vector
method of vector class in java
What is Stack
List Advance
ArrayList user defined object
ArrayList internal implementation
LinkedList internal implementation
ArrayList VS LinkedList
ArrayList VS Vector
Set
What is HashSet
What is LinkedHashSet
What is TreeSet
Set Advance
When to Use HashSet
HashSet internal Working
When to use LinkedHashSet
TreeSet internal Working
Set add method
Iterate Collection
Iterate Collection
Advance for loop
What is Iterator
Advantage of Iterator
What is ListIterator
Iterator VS ListLterator
What is Enumeration
Enumeration VS Iterator
Map
What is HashMap
What is LinkedHashMap
What is TreeMap
What is Hashtable
What is WeakHashMap
What is IdentityHashMap
What is Properties file
Map Advance
What is Load-Factor
how to load properties file in java
how to iterate map in java
how to retrieve element of map using keySet
difference between hashmap and hashtable
Map Interview
Internal implementation of HashMap
HashMap Collision
Rehashing Process
Internal working of LinkedHashMap
Queue
What is Priority Queue
Concurrent Collections
Concurrent Collections
Concurrentmap in java
Why ConcurrentModificationException
What is ConcurrentHashMap
Internal Implementation of CHM
HashMap VS ConcurrentHashMap
What is CopyOnWriteArrayList
What is CopyOnWriteArraySet
Concurrent Map 2
Arraylist VS CopyOnWriteArrayList
put() VS putIfAbsent()
remove() method
replace() method
Collection Sorting
Comparable
Comparator
Comparable VS Comparator
Sort by multiple fields
Extra
Legacy class
Arrays class
What is Collections
Collection VS Collections
immutable object

Java Comparable interface


Java Comparable interface

Java Comparable interface provides us facility to sort our custom class object based on natural sorting order means either in ascending order or in descending order.

Comparable interface available in java.lang package and it has only one method public int compareTo().

Comparable provides natural order sorting.

Wen can sort elements  based on single data member only.

Why we need to implement Comparable interface

When we have some requirement to sort Employee record on the basis of their id then using Comparable interface we can sort.If you did not used Comparable interface in your class then you can not call Collections.sort() method on that object.

In simple word i can say it used to allow Collections.sort() to work.

Collections.sort() will be applied only on Comparable implemented classes that is why all wrapper classes are implemented by Comparable interface.

We can not apply Collections.sort() on any non comparable implemented classes.

Working of compareTo(Object)

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

 Method Name  Working
 compareTo(Object)  It will return -1  If comparable object is less than the passed object.
 compareTo(Object)  It will return 0  If comparable object is same as the passed object
 compareTo(Object)   It will return 1  If comparable object is    greater than the passed object

 

Java Comparable interface Example

  1. Employee.java
  2. Test.java

How to implement Comparable

public class Employee implements Comparable<Employee> {
 private int id;
 private String name;
 private String address;

 public Employee(int id, String name, String address) {
     this.id = id;
     this.name = name;
     this.address = address; 
 }
 
 @Override public int compareTo(Employee o) {
 if (id > o.id) { 
     return 1;
 } else if (id < o.id) {
     return -1;
 } else {
     return 0; 
 } 
}
   @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", address=" + address + "]"; }
}

Test.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test {
  public static void main(String[] args) { 
    List<Employee> list = new ArrayList<>();
    list.add(new Employee(101, "Vikas", "Hyderabad"));
    list.add(new Employee(105, "Sanjit", "Banglore"));
    list.add(new Employee(103, "Yogesh", "Pune"));
    Collections.sort(list);
    System.out.println(list);
  }
}






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229