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
Cannot infer type arguments for Class<>
提问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());
idComparator
implements Comparator<Integer>
so the T
for Sorter
is Integer
, but your declaration indicatesto the compiler that T
is Person
, so the compiler is confused and emits a message.
idComparator
实现Comparator<Integer>
所以T
for Sorter
is Integer
,但是您的声明向编译器指示T
is Person
,因此编译器感到困惑并发出消息。
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
return
statement is (possibly) missing inSorter#compare
idComparator
name should start with an uppercase letter:IdComparator
- if you really want to use your own
Comparable
/Comparator
you 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)));
}