Java 无法推断 Class<> 的类型参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37624993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:30:22  来源:igfitidea点击:

Cannot infer type arguments for Class<>

javagenerics

提问by pawelen

I have a problem with my sorting-test app, where I use a comparator. I got a message:

我的排序测试应用程序有问题,我在其中使用了比较器。我收到一条消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: Cannot infer type arguments for Sorter<>

线程“main”中的异常 java.lang.Error:未解决的编译问题:无法推断 Sorter<> 的类型参数

For that code:

对于该代码:

public class TestClass {

    public static void main(String[] args){

            Sorter<Person> idSorter = new Sorter<>(new idComparator());
    .
    .
    .
    }
}

Sorter class:

分拣机类:

public class Sorter<T extends Comparable> {

    Comparator<T> comparator;
    int switches = 0,
        compares = 0;

    public Sorter(Comparator<T> comparator) {
        this.comparator = comparator;
    }

    public Sorter() {
        this.comparator = null;
    }

    protected int compare(T first, T second) {
        if (this.comparator == null) {
            int cmp = first.compareTo(second);
            this.compares++;
            return cmp;
        }

Comparable interface:

比较界面:

public interface Comparable {
    public int compareTo(Comparable other);
}

idComparator class:

idComparator 类:

public class idComparator implements Comparator<Integer> {

    public int compare(Integer first, Integer second) {
        return first > second? 1: first == second? 0: 1;
    }
}

Comparator interface:

比较器接口:

public interface Comparator<T> {
    int compare(T first, T second);
}

What is wrong with such a use? How can I do it better?

这样的用法有什么问题?我怎样才能做得更好?

采纳答案by Vlad

In this line:

在这一行:

Sorter<Person> idSorter = new Sorter<>(new idComparator());

idComparatorimplements Comparator<Integer>so the Tfor Sorteris Integer, but your declaration indicatesto the compiler that Tis Person, so the compiler is confused and emits a message.

idComparator实现Comparator<Integer>所以Tfor Sorteris Integer,但是您的声明向编译器指示Tis Person,因此编译器感到困惑并发出消息。

Probable fix:

可能的修复

Sorter<Integer> idSorter = new Sorter<>(new idComparator());

but as some said in comments, it's oddto sort persons based on some integer.

但正如一些人在评论中所说,根据某个整数对人进行排序很奇怪



Also note:

另请注意:

  • a returnstatement is (possibly) missing in Sorter#compare
  • idComparatorname should start with an uppercase letter: IdComparator
  • if you really want to use your own Comparable/ Comparatoryou should name them with some other name (MyComparable?) as is, there is a confusion with java ones.
  • 一个return说法是(可能)失踪Sorter#compare
  • idComparator名称应以大写字母开头: IdComparator
  • 如果你真的想使用你自己的Comparable/Comparator你应该用其他名字(MyComparable?)命名它们,as is与java的混淆。

回答by Vlad

Why not use Java 8 to sort and compare? For clarification, Lists.newArrayList is a Google Guava collection.

为什么不使用 Java 8 进行排序和比较?为了澄清起见,Lists.newArrayList 是 Google Guava 集合。

public void sortingEntitiesById() {
    List<Person> persons = Lists.newArrayList(new Person("Sarah", 10), new Person("Hyman", 12));

    persons.sort((h1, h2) -> h1.getId().compareTo(h2.getId()));
    Assert.assertThat(persons.get(0), equalTo(new Person("Sarah", 10)));
}

Reverse Sort

反向排序

public void sortingEntitiesByIdReversed() {
    List<Person> persons = Lists.newArrayList(new Person("Sarah", 10), new Person("Hyman", 12));
    Comparator<Person> comparator = (h1, h2) -> h1.getId().compareTo(h2.getId());

    persons.sort(comparator.reversed());
    Assert.assertThat(persons.get(0), equalTo(new Person("Hyman", 12)));
}

Sort with multiple conditions

多条件排序

public void sortEntitiesWithMultipleConditions() {
    List<Person> persons = Lists.newArrayList(
            new Person("Sarah", 12), new Person("Sarah", 10), new Person("Zack", 12));

    persons.sort(Comparator.comparing(Person::getName).thenComparing(Person::getId));
    Assert.assertThat(persons.get(0), equalTo(new Person("Sarah", 10)));
}