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

杰森

我有一个应用程序,用户在其中向我提供字段名称,例如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()行导致带有message的编译时错误Capture of ? instead of Object我曾尝试铸造的第二个参数Comparable<?>Comparable<? super sorterField.getType()等等,没有运气。有想法吗?

Qwer Izuken

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

根据字段名称值对numpy结构化数组中的值进行排序

来自分类Dev

根据字段名称值对numpy结构化数组中的值进行排序

来自分类Dev

如何根据 Java Spark 2.1.1 中的字段名称对 structType 进行排序/排序

来自分类Dev

杰克逊仅使用字段名称对响应进行排序

来自分类Dev

按字段名称对json字段进行排序

来自分类Dev

mongodb:使用字段名称的动态参数对嵌套数组进行排序

来自分类Dev

AngularJS Spring排序字段名称

来自分类Dev

空字段名称的数组对象

来自分类Dev

Linux-根据2个字段名称排序

来自分类Dev

mysql - 查询以创建新表并按字段名称对其进行排序不起作用

来自分类Dev

如何根据字段类型获取字段名称?

来自分类Dev

如何映射对象数组的名称以更改字段名称?

来自分类Dev

按FIELD()和字段名称排序

来自分类Dev

仅查询Mongodb中的字段名称

来自分类Dev

仅查询Mongodb中的字段名称

来自分类Dev

如何将前端字段名称映射到数据库列名称以进行排序

来自分类Dev

如何在Java中获取类的字段名称

来自分类Dev

Django:获取模型类的字段名称

来自分类Dev

如何在Java中获取类的字段名称

来自分类Dev

在 Java 中获取类的字段名称

来自分类Dev

使用参数而不是字段名称进行选择

来自分类Dev

更改字段名称

来自分类Dev

使用数组值作为对象字段名称

来自分类Dev

python + django尝试将mongoengine文档序列化为json,但仅获取字段名称数组

来自分类Dev

根据字段名称有选择地设置每个字段返回的SOLR行数

来自分类Dev

没有字段名称的元组

来自分类Dev

jQuery递增字段名称

来自分类Dev

VBA函数到字段名称

来自分类Dev

在ruby中串联字段名称

Related 相关文章

热门标签

归档