英文:
Problems with sorting List<List<String>> java?
问题
// data is List<List<String>>
data.sort(Comparator.comparing(e -> e.get(col)).reversed());
// data is List<List<String>>
data.sort(Comparator.comparing(e -> e.get(col)).thenComparing(e -> e.get(col2)));
英文:
I have one List<List<String>> I want to sort it according to column . I used the following code.
// data is List<List<String>>
data.sort(Comparator.comparing(e-> e.get(col)));
Its working and sorting according column specified. But If I used reversed()
or thenComparing()
method, It says
error: cannot find symbol
data.sort(Comparator.comparing(e-> e.get(col)).reversed() );
^
symbol: method get(int)
location: variable e of type Object
Also with the thenComparing method,
error: cannot find symbol
data.sort(Comparator.comparing(e-> e.get(col)).thenComparing(e->e.get(col2)) );
^
symbol: method get(int)
location: variable e of type Object
error: cannot find symbol
data.sort(Comparator.comparing(e-> e.get(col)).thenComparing(e->e.get(col2)) );
^
symbol: method get(int)
location: variable e of type Object
2 errors
I'm not getting anything from the error messages. Fyi, Im using OpenJDK 11 for this.
答案1
得分: 3
似乎在有第二层的情况下,Java不会自动推断类型。
在第一个例子中:
data.sort(Comparator.comparing(e -> e.get(0)));
类型是从data
中推断出来的,但在第二个例子中:
data.sort(Comparator.comparing(e -> e.get(0)).reversed());
comparing
的类型没有被推断出来。您可以通过几种方式来解决这个问题,最简单的方法是显式指定类型:
data.sort(Comparator.comparing((List<String> e) -> e.get(0)).reversed());
Java不会自动进行推断似乎有点奇怪。如果我们包括创建比较器的中间步骤,我们可以很清楚地看到它不会自动推断:
Comparator<List<String>> c = Comparator.comparing(e -> e.get(0)).reversed();
现在我们会得到两个错误,一个是因为lambda表达式中的参数是一个Object,第二个错误是因为我们创建了一个Comparator<Object>
。
我认为它是这样工作的,但我不确定如何验证。reversed
方法将采取调用它的实例的类型参数,类型不会被自动推断。在调用reversed
时,对象必须已经被实例化并赋予泛型。然后,对comparing
的调用没有上限,它只需返回一个Comparator<?>
。
另一种解决方法是在调用Comparator时指定类型:
Comparator<List<String>> c = Comparator.<List<String>, String>comparing(e -> e.get(0)).reversed();
英文:
Seems like java doesn't infer the type once you have a second layer.
In the first example.
data.sort( Comparator.comparing( e-> e.get(0) ) );
The type is inferred from data, but in the second example.
data.sort( Comparator.comparing( e-> e.get(0) ).reversed() );
The type of 'comparing' is not inferred. You can resolve this a couple ways, the easiest to be explicit.
data.sort( Comparator.comparing( (List<String> e) -> e.get(0) ).reversed() );
It seems funny that java doesn't chain inferences. If we include the intermediate step of creating the comparator, we can see pretty clearly it doesn't.
Comparator<List<String>> c = Comparator.comparing( e-> e.get(0) ).reversed();
> | Error:
| cannot find symbol
| symbol: method get(int)
| Comparator<List<String>> c = Comparator.comparing( e->e.get(0) ).reversed();
| ^---^
| Error:
| incompatible types: java.util.Comparator<java.lang.Object> cannot be converted to java.util.Comparator<java.util.List<java.lang.String>>
| Comparator<List<String>> c = Comparator.comparing( e->e.get(0) ).reversed();
| ^--------------------------------------------^
Now we get two errors, one in the lambda because the argument is an Object, and the second error because we're creating a Comparator<Object>.
I'm thinking it works this way, but I'm not sure how to verify. 'reversed' will take the type argument of the instance calling it, the type is not inferred. By the time 'reversed' is called the object has to have been instantiated and the generic assigned. The call to comparing then has no upper bounds, it just has to return a Comparator<?>.
An alternative solution, specify the types when the Comparator is called.
Comparator<List<String>> c = Comparator.<List<String>, String>comparing( e->e.get(0) ).reversed();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论