Comparator in Java - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Saturday, 7 March 2020

Comparator in Java

It is in  java.util package

Comparator is used to have customized sorting

It defines two methods

1.) Compare
2.) Equals


public int compare(Object obj1, Object obj2)

it returns -ve if obj 1 has to come before obj2

it returns +ve if obj 1 has to come after obj2

it returns 0 if obj1 and obj2 are equal

public boolean equals(Object obj)


Implement Comparator

class mycomparator implements comparator


Note: when we implement comparator interface

we need to define only compare method
not equals method--> because it is already available to our class from object class through inheritance

Explanation

boolean equals(Object obj), here boolean method is using object class

mycomparator class already extending object class, because every class have object class

so hence we dont need to define equals method

Write a program to insert integer object into Treeset where sorting order is descending order

 Example

Lets suppose we sort Treeset we get ascending order

public class my {
   
    public static void main(String[] args) {
        TreeSet<Integer> set=new TreeSet<>();
       
        set.add(10);
        set.add(0);
        set.add(15);
        set.add(20);
        set.add(20);
        set.add(20);
          
        System.out.println(set);
       
    }
       
}
Output

[0, 10, 15, 20]
i.e. is in ascending order


We want to implement in descending order


Example

package practice;

public class programming {   
    int rollno;
    String name;
    int age;
   
    public programming(int rollno,String name,int age) {
        this.rollno=rollno;
        this.name=name;
        this.age=age;
       
    }
   
}

Second Class

package practice;

import java.util.Comparator;

public class nameComparator  implements Comparator<programming>{

    @Override
    public int compare(programming o1, programming o2) {
        programming p1=o1;
        programming p2=o2;
       
        return p1.name.compareTo(p2.name);
    }

   
}

Third Class Age Comparator

package practice;

import java.util.Comparator;

public class ageComparator implements Comparator<programming>{
   
    @Override
    public int compare(programming o1, programming o2) {
        if(o1.age==o2.age)
            return 0;
        else if (o1.age>o2.age)
            return 1;
        else
            return -1;
           
       
    }

}

Test Class


package practice;

import java.util.ArrayList;
import java.util.Collections;

public class testComparable {
   
    public static void main(String[] args) {
        ArrayList<programming> ls=new ArrayList<programming>();
        ls.add(new programming(11,"A",7));
        ls.add(new programming(18,"Z",17));
        ls.add(new programming(17,"B",11));
        ls.add(new programming(21,"C",9));
       
        Collections.sort(ls,new nameComparator());
        for(programming ok:ls){
            System.out.println(ok.name); //calling name variable from ok object
        }
       
        Collections.sort(ls,new ageComparator());
        for(programming ok:ls){
            System.out.println(ok.age);
        }
       
       
    }

}

Output

A
B
C
Z
7
9
11
17

No comments:

Post a Comment