java - 仅根据字段名称对类数组进行排序

标签 java sorting reflection

我有一个应用程序,用户向我提供字段名称,例如 namecostInCents ,我必须按那个字段排序。我有办法保证字段名称是正确的。此应用程序导致我无法类的并发症 Comparable并实现特定的 compareTo() , 因为自定义实现了 compareTo()我需要知道在实现时要使用哪些字段/方法。

因此,为了实现这个目标,我尝试使用反射来将字段与其访问器相匹配。这是我想做的 MWE。

Product是一个简单的 POJO 类,我想对其实例进行成对比较:

public class Product
{

    final String name;
    final Integer quantity;
    final Long costInCents;

    public Product(final String name, final Integer quantity, final Long costInCents)
    {
        this.name = name;
        this.quantity = quantity;
        this.costInCents = costInCents;
    }

    public String getName()
    {
        return name;
    }
    public Integer getQuantity()
    {
        return quantity;
    }
    public Long getCostInCents()
    {
        return costInCents;
    }
}

还有我的Main类,目前不完整:

public class Main {

    public static void main(String[] args) {
        final Product[] productArray =
                {
                    new Product("Clorox wipes", 50, 700L),
                    new Product("Desk chair", 10, 12000L),
                    new Product("TV", 5, 30000L),
                    new Product("Bookcase", 5, 12000L),
                    new Product("Water bottle", 20, 700L),
                };

        // The following void methods are supposed to sort in-place with something like Arrays.sort() or Collections.sort(),
        // but I am also open to solutions involving stuff like Stream::sorted() or similar ones, which return a sorted array.
        sortByField(productArray, "costInCents");
        sortByField(productArray, "name");
    }

    private void sortByField(final Product[] productArray, final String sorterFieldName)
    {
        final Field sorterField = getSorterField(sorterFieldName, LiteProduct.class); // Gets the Field somehow
        final Method sorterAccessor = getSorterAccessor(sorterField, LiteProduct.class);    // Given the Field, this is easy
        Arrays.sort((Product p1, Product p2)->((Comparable<?>)sorterAccessor.invoke(p1)).compareTo(sorterAccessor.invoke(p2)) > 0); // Capture of ? instead of Object
    }
}

不幸的是,Arrays.sort()行导致编译时错误,消息为 Capture of ? instead of Object .我尝试将第二个参数转换为 Comparable<?> , Comparable<? super sorterField.getType()等,没有运气。想法?

最佳答案

可能是最好的方法 - 使用排序策略。无需反射,兼容更复杂的排序逻辑:

Map<String, Comparator<Product>> sortingStrategies = new HashMap<>(){
    {
        put("costInCents", Comparator.comparingLong(p->p.costInCents));
        put("quantity", Comparator.comparingLong(p->p.quantity));
        put("name", Comparator.comparing(p->p.name));
    }
};

private void sortByField(final Product[] productArray, final String sorterFieldName)
{
    Arrays.sort(productArray, sortingStrategies.get(sorterFieldName));
}

关于java - 仅根据字段名称对类数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65203606/

相关文章:

java - 为什么 arraylist 既要清除原始数组又要清除临时数组?

Java Web服务复杂类型

linux - 在不使用unix中的排序的情况下对字符进行排序

go - 从 golang 中的特定接口(interface)获取底层类型

java - 使用多个 BufferedReader 读取单个文件

Java JTable 与 JComboBox

c - 使用 C 根据对象之间已知的相似性快速对对象数组进行排序

sorting - SQLite - 利用排序索引

java - 将 libgdx 项目导出为 HTML - 您是否忘记继承所需的模块?

C# GetConstructor() 不返回父构造函数