-->

Java Customized Comparator Programing Question

Posted by Admin on

Q:-> Take the FindDupsexample and modify it to use a SortedSet instead of a Set. Specify a Comparator so that case is ignored when sorting and identifying set elements.

FindDups Example :

import java.util.*;

public class FindDups {
    public static void main(String[] args) {
        Set<String> s = new HashSet<String>();
        for (String a : args)
               s.add(a);
               System.out.println(s.size() + " distinct words: " + s);
    }
}

Customized Comparator With SortedSet :
package Collection;

import java.util.*;

public class FindDups {
    public static void main(String[] args) {
   
    //Sorted Set is an interface which itself implements set interface  and TreeSet is class which is an implementation of SortedSet Interface
    //TreeSet does not contain unique elements
    // Tree Set has three kind of constructor
    /*
     * Constructor Summary
       Constructors
       Constructor and Description
       TreeSet()
       Constructs a new, empty tree set, sorted according to the natural ordering of its elements.
       TreeSet(Collection<? extends E> c)
       Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements.
       TreeSet(Comparator<? super E> comparator)
       Constructs a new, empty tree set, sorted according to the specified comparator.
       TreeSet(SortedSet<E> s)
       Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.
      *
     */
    SortedSet<String> eliminatedDups = new TreeSet<String>( IGNORE_CASE);
        
       
    for (String a : args) {
       eliminatedDups.add(a);
     }
     System.out.println(eliminatedDups.size() + " distinct words by ignoring case: " + eliminatedDups);
}
       
    static final Comparator<String> IGNORE_CASE = new Comparator<String>() {
        public int compare(String s1, String s2) {
            return s1.compareToIgnoreCase(s2);
        }
    };

}


Another Example of Using Customized Comparator in Java 

This example gives you how to sort an ArrayList using Comparator. The ArrayList contains user defined objects. By using Collections.sort() method you can sort the ArrayList. You have to pass Comparator object which contains your sort logic. The example sorts the Empl objects based on highest salary.


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MyArrayListSort {
    
    public static void main(String a[]){
        
        List<Empl> list = new ArrayList<Empl>();
        list.add(new Empl("Ram",3000));
        list.add(new Empl("John",6000));
        list.add(new Empl("Crish",2000));
        list.add(new Empl("Tom",2400));
        Collections.sort(list,new MySalaryComp());
        System.out.println("Sorted list entries: ");
        for(Empl e:list){
            System.out.println(e);
        }
    }
}

class MySalaryComp implements Comparator<Empl>{

    @Override
    public int compare(Empl e1, Empl e2) {
        if(e1.getSalary() < e2.getSalary()){
            return 1;
        } else {
            return -1;
        }
    }
}

class Empl{
    
    private String name;
    private int salary;
    
    public Empl(String n, int s){
        this.name = n;
        this.salary = s;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public String toString(){
        return "Name: "+this.name+"-- Salary: "+this.salary;
    }

}


Please comment if you have any question or suggestion for the above code

No comments:

Post a Comment