java - 如何为用户定义的类实现通用比较器

标签 java generics comparator

我想对包含一些数据成员的用户定义类进行排序。 在这里,我创建了名为 Universal 和 Showroom 的类,两者都包含 common 价格标签。我想创建两个类都必须通用的通用比较器compare() 两个类具有具有不同值的相同字段。 两个值均按升序排序。

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;
    }
}

最佳答案

泛型的另一个选项:

    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());
        }
    }

}

输出:

Showroom
5000
7000
8000
9000
Universal
44444
100000
222222
7055500

注意:确保为 Universal 和 Showroom Pojos 中的属性创建 setter/getter。

关于java - 如何为用户定义的类实现通用比较器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56489020/

相关文章:

java - JUnit 5 : How to assert an exception is thrown?

java - 获取 Spring 环境作为属性

swift - 无法检查类型 T : Equatable inside a generic 的值是否相等

c# - 创建通用实例

java - 获取错误 : Comparison method violates its general contract

java - 比较具有涉及 boolean 值的多个属性的列表

java - 终止线程的更复杂的方法(守护线程或 Thread.interrupt())

java - SWT 按钮上的 MouseClick 事件在哪里?

c# - 动态声明泛型类型实例

java - 这是按标题、位置排序然后使用比较器排序的正确方法吗?