Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

comparable interface java

public class BinarySearchTree< T extends Comparable<T>> {
    Node root;
    class Node {
        T data;
        Node left;
        Node right;

        public Node(T data) {
            this.data = data;
        }
    }

    public boolean isEmpty() {
        return root == null;
    }

    public void insert(T value) {
        if(isEmpty())
            root = new Node(value);
        else
            insert(root, value);
    }

    private void insert(Node node, T value) {

        if(value.compareTo(node.data) < 0) {
            if(node.left == null)
                node.left = new Node(value);
            else
                insert(node.left, value);
        }
        else {
            if(node.right == null)
                node.right = new Node(value);
            else
                insert(node.right, value);
        }
    }

}
Comment

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 :: expandablelistview android 
Java :: Java program to find largest of three numbers using nested if 
Java :: += operator casting in java 
Java :: how to set background color in jframe in java 
Java :: How to Implement GET and POST Requests With Java 
Java :: java windowbuilder launch on second monitor 
Java :: intellij run single java file 
Java :: also in java 
Java :: RNGestureHandlerButtonViewManager.java uses or overrides a deprecated API 
Java :: es java painless source int value increase 
Java :: java formatted string input 
Java :: generate paranthesis 
Java :: 1 2 1 3 2 1 4 3 2 1 3 2 1 2 1 1 java 
Java :: float division by zero 
Java :: Which one of the following values can a Java variable NOT have? 
Java :: index out of bounds exception java 
Java :: springBoot Disable a Specific Auto-Configuration 
Java :: ConnectionString connection timeOut mongodb java 
Java :: all loops in java 
Java :: how to get map with string as key and Arraylist as value in java 
Java :: candies price hackerearth solution in java 
Java :: Program to check Vowel or Consonant using Switch Case 
Java :: song listening app android 
Java :: java search tree 
Java :: date to timestamp java 
Java :: extend vs implement in java 
Java :: alert dialog not displayed android 
Java :: java calling a method 
Java :: is string a primitive data type/use of getclass() 
Java :: java color light gray 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =