Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可比泛型的局部变量_Java_Sorting_Generics_Wildcard_Comparable - Fatal编程技术网

Java 可比泛型的局部变量

Java 可比泛型的局部变量,java,sorting,generics,wildcard,comparable,Java,Sorting,Generics,Wildcard,Comparable,我正在尝试为可比较对象的通用列表实现排序。问题是我不能为这样的列表定义局部变量,这会导致代码重复 基本上,我需要一个变量来维护一个带有签名的方法返回的值 列出getItemsint itemType,类别cl 这是代码。目标是避免第25、26和29、30行上的代码重复 有可能吗 将Associations itemType->Class放置到映射中,如果没有映射,则使用它 private static final Map<Integer, Class<? extends Compar

我正在尝试为可比较对象的通用列表实现排序。问题是我不能为这样的列表定义局部变量,这会导致代码重复

基本上,我需要一个变量来维护一个带有签名的方法返回的值 列出getItemsint itemType,类别cl

这是代码。目标是避免第25、26和29、30行上的代码重复

有可能吗

将Associations itemType->Class放置到映射中,如果没有映射,则使用它

private static final Map<Integer, Class<? extends Comparable>> types = new HashMap<>();
static {
    types.put(1, Long.class);
    types.put(2, Date.class);
}

public List<Long> getSortedIds(int itemType) {
    Class<? extends Comparable> clz = types.get(itemType);
    if (clz != null) {
        List<Item> items = (List<Item>)getItems(itemType, clz);
        Collections.sort(items);
        return items.stream().map(it -> it.id).collect(Collectors.toList());
    }
    //...
    return null;
}

把你的代码贴在这个问题上。好吧,它是有效的。我想知道是否有办法指定一些通配符来减少未检查操作的数量