Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Comparable interface

// Java program to demonstrate how to Sort HashSet using
// Comparable interface
 
import java.util.*;
 
// Student class implements comparable interface
class Student implements Comparable<Student> {
   
    Integer marks;
 
    Student(Integer marks) { this.marks = marks; }
 
    // override toString method
    public String toString() { return (" " + this.marks); }
 
    // Override compareTo method to sort HashSet in
    // descending order
    public int compareTo(Student stu)
    {
        return stu.marks.compareTo(this.marks);
    }
}
 
class GFG {
    public static void main(String[] args)
    {
 
        // New HashSet
        HashSet<Student> set = new HashSet<>();
 
        // Adding elements to the set
        set.add(new Student(500));
        set.add(new Student(300));
        set.add(new Student(400));
        set.add(new Student(100));
        set.add(new Student(200));
 
        // Print Before sort
        System.out.println("Before sort elements in descending order : "
            + set);
 
        // TreeSet to sort HashSet using comparable
        // interface
        TreeSet<Student> tree_set = new TreeSet<>(set);
 
        // Print after sorting
        System.out.println("After sort elements in descending order : "
            + tree_set);
    }
}
Comment

Comparable interface

Before sort elements in descending order : [ 300,  400,  500,  200,  100]
After sort elements in descending order : [ 500,  400,  300,  200,  100]
Comment

comparable

+------------------------------------------------------------------------------------+
¦               Comparable                ¦                Comparator                ¦
¦-----------------------------------------+------------------------------------------¦
¦ java.lang.Comparable                    ¦ java.util.Comparator                     ¦
¦-----------------------------------------+------------------------------------------¦
¦ int objOne.compareTo(objTwo)            ¦ int compare(objOne, objTwo)              ¦
¦-----------------------------------------+------------------------------------------¦
¦ Negative, if objOne < objTwo            ¦ Same as Comparable                       ¦
¦ Zero,  if objOne == objTwo              ¦                                          ¦
¦ Positive,  if objOne > objTwo           ¦                                          ¦
¦-----------------------------------------+------------------------------------------¦
¦ You must modify the class whose         ¦ You build a class separate from to sort. ¦
¦ instances you want to sort.             ¦ the class whose instances you want       ¦
¦-----------------------------------------+------------------------------------------¦
¦ Only one sort sequence can be created   ¦ Many sort sequences can be created       ¦
¦-----------------------------------------+------------------------------------------¦
¦ Implemented frequently in the API by:   ¦ Meant to be implemented to sort          ¦
¦ String, Wrapper classes, Date, Calendar ¦ instances of third-party classes.        ¦
+------------------------------------------------------------------------------------+
Comment

PREVIOUS NEXT
Code Example
Java :: gson parse json 
Java :: swapping techniques using java 
Java :: java use of super keyword 
Java :: generic array creation java 
Java :: java.lang.stringindexoutofboundsexception: string index out of range: 10 
Java :: java string stringbuilder remove trailing comma 
Java :: print treeset java 
Java :: java graph 
Java :: arraylist add at index 
Java :: Java if (if-then) Statement 
Java :: search in 2d matrix 
Java :: arraylist remove value in java 
Java :: start an activity in adapter 
Java :: Java Access HashMap Elements 
Java :: print outputstream to console java 
Java :: ejercicios resueltos de python codeskulptor 
Java :: exitonclose swing 
Sql :: sql server reseed single table 
Sql :: oracle nls_date_format 
Sql :: how to install mysql ubuntu 
Sql :: psql kill pid 
Sql :: grant revoke privileges to mysql username 
Sql :: set default today mysql 
Sql :: drop all table postgresql 
Sql :: remove space in mysql 
Sql :: find mysql version linux 
Sql :: sql server list table sizes 
Sql :: oracle modify column type 
Sql :: oracle check table lock 
Sql :: update column name and datatype in sql 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =