user11612465
user11612465

Reputation: 1

How to implement generic comparator for user-defined class

I want to sort user-defined class that contains some data members. Here I created Class called Universal & Showroom Both Contains price tag as common . I want To create generic Comparator compare() that has to be common for Both the Classes Both Class has same field with different values. Both value are to be sorted in ascending order.

public class Showroom  {
    String phone_vendor;int price,ram,storage;

    public Showroom(String phone_vendor, int price, int ram, int storage) {
        this.phone_vendor = phone_vendor;
        this.price = price;
        this.ram = ram;
        this.storage = storage;
    }

}
public class Universal {
    String phone_vendor;
    int price,ram,storage;

    public Universal(String phone_vendor, int price, int ram,int storage) {
        this.phone_vendor = phone_vendor;
        this.price = price;
        this.ram = ram;
        this.storage=storage;
    }
}

Upvotes: 0

Views: 1578

Answers (3)

Madhusudana
Madhusudana

Reputation: 302

One more option with Generics:

    import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class GenericComparator<T> implements Comparator<T> {

    @Override
    public int compare(T o1, T o2) {
        int price1 = 0;
        int price2 = 0;
        try {
            Method m1 = o1.getClass().getDeclaredMethod("getPrice", o1.getClass().getClasses());
            price1 = (int) m1.invoke(o1);
            Method m2 = o2.getClass().getDeclaredMethod("getPrice", o1.getClass().getClasses());
            price2 = (int) m2.invoke(o2);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        return price1 - price2;
    }

    public static void main(String[] args) {
        List<Showroom> showRoomList = new ArrayList<Showroom>();
        showRoomList.add(new Showroom("IPHONE", 9000, 4, 4));
        showRoomList.add(new Showroom("SAMSUNG", 5000, 4, 4));
        showRoomList.add(new Showroom("MOTO", 8000, 4, 4));
        showRoomList.add(new Showroom("SONY", 7000, 4, 4));

        List<Universal> universalList = new ArrayList<Universal>();
        universalList.add(new Universal("IPHONE", 100000, 4, 4));
        universalList.add(new Universal("SAMSUNG", 222222, 4, 4));
        universalList.add(new Universal("MOTO", 44444, 4, 4));
        universalList.add(new Universal("SONY", 7055500, 4, 4));
        System.out.println("Showroom");
        Collections.sort(showRoomList, new GenericComparator<Showroom>());
        for (Showroom s : showRoomList) {
            System.out.println(s.getPrice());
        }
        System.out.println("Universal");
        Collections.sort(universalList, new GenericComparator<Universal>());
        for (Universal s : universalList) {
            System.out.println(s.getPrice());
        }
    }

}

Output:

Showroom
5000
7000
8000
9000
Universal
44444
100000
222222
7055500

Note: Make sure to create setter/getter for attributes in Universal and Showroom Pojos.

Upvotes: 2

balaji
balaji

Reputation: 216

Just the example of comment I gave earlier.

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

public abstract class Parent {
    String phone_vendor;int price,ram,storage;

    public Parent(String phone_vendor, int price, int ram, int storage) {
        this.phone_vendor = phone_vendor;
        this.price = price;
        this.ram = ram;
        this.storage = storage;
    }

    public static void main(String[] args) {
        Universal us = new Universal("Samsung", 51, 5, 11);
        Showroom sr = new Showroom("Nokia", 50, 4, 10);

        List<Parent> lst = new ArrayList<Parent>();
        lst.add(us);
        lst.add(sr);
        Collections.sort(lst, new PriceComparator());

        for(Parent p:lst) {
            System.out.println(p.price);
        }

    }
}


class Showroom extends Parent {

    public Showroom(String phone_vendor, int price, int ram, int storage) {
        super(phone_vendor, price, ram, storage);
    }

}
class Universal extends Parent {
   public Universal(String phone_vendor, int price, int ram, int storage) {
       super(phone_vendor, price, ram, storage);
   }
}

class PriceComparator implements Comparator<Parent> {
    @Override
    public int compare(Parent o1, Parent o2) {
        return o1.price - o2.price;
    }
}

Upvotes: 1

Januson
Januson

Reputation: 4841

There are a few ways how you could approach this.

One way would be to define an interface which would give access to the information you need and implement comparator for this interface so that every class that implements it could be compared.

interface Offer {

    String phoneVendor();

    int price();

    int ram();

    int storage();

}

class OfferComparator implements Comparator<Offering> {

    @Override
    public int compare(Offering o1, Offering o2) {
        // Compare
        return 0;
    }

}

Second option would be.. do you really need both those classes if they appear to be the same? Could you perhaps generalize it by giving it type attribute? Merged class could just implement comparable. Something like this:

public class Offer implements Comparable<Offer> {

    enum Type {
        Showroom, Universal;
    }

    Type type;
    String phone_vendor;
    int price, ram, storage;

    public Showroom(String phone_vendor, int price, int ram, int storage) {
        this.phone_vendor = phone_vendor;
        this.price = price;
        this.ram = ram;
        this.storage = storage;
    }

    @Override
    public int compareTo(Offer o) {
        // Compare
        return 0;
    }

}

Upvotes: 0

Related Questions