了解泛型排序的基础知识 [英] Understanding the Basics of Generics Sorting
问题描述
列表< E> l = new LinkedList<>(arbSet);
Collections.sort(l);
arbSet
只是一组元素: 组< E - 代替; arbSet
。
显然,这是有问题的 - 它不应该起作用。为了确保我知道这一点,Eclipse为我尝试调用 .sort
提供了以下内容:
绑定不匹配:类型集合的泛型方法sort(List< T>)不适用于参数(List< E>)。推断的类型E不是有界参数< T扩展了Comparable< ? super T >>
所以,我做了一些文档咨询,看看 sort
's 规格的。
因此,我试图确保 sort
知道 E
继续可比较
:
第一行现在看起来像:
List< E扩展Comparable< ;? super E>> l =新的LinkedSet<>(arbSet);
现在,Eclipse指出:
令牌上的语法错误扩展,预期
我错过了什么?我觉得这是一个非常基本的例子,我只是缺少一些手掌面对面的东西。为了进一步简化它,所有 arbSet
元素都是实现 Comparable
接口的元素。
仅当您提供自定义<$ c $>时,才可以使用 Collections.sort()
c> Comparator 或者如果您尝试排序的元素实现 Comparable
界面。因此,它取决于哪些元素存储在 arbSet
中。
static< E extends可比< E - 代替;>列表与LT E - 代替; sortedListFrom(Set< E> set){
List< E> l = new LinkedList<>(set);
Collections.sort(l);
return l;
}
编辑:
如果你想在一个构造函数中做到这一点,你有两个选择:
-
声明类型
E
就在构造函数之前。当然,这不会有太大的作用,因为list
会在构造函数完成后丢失。
class Test {
< E extends Comparable< E>> Test(Set< E> arbSet){
List< E> list = new LinkedList<>(arbSet);
Collections.sort(list);
System.out.println(list);
$ / code $ <$ $ $ <$ $ $ $ $ <$> $ <$>声明类型
E
,因此您可以将结果存储在属性中。
class Test< E扩展了Comparable< E>> {
列表< E>列表;
Test(Set< E> arbSet){
this.list = new ArrayList<>(arbSet);
Collections.sort(this.list);
System.out.println(this.list);
}
}
There are a number of question on Stack Overflow about sorting Generics; however, I am interested in sorting Generics in the simplest way: nothing nested. The code I have below is an attempt to sort a generic set - or, list - of elements.
List<E> l = new LinkedList<>(arbSet);
Collections.sort(l);
arbSet
is just a set of elements: Set<E> arbSet
.
Clearly, this is problematic - it shouldn't work. To make sure I know this, Eclipse gives me the following for my attempt to call .sort
:
Bound mismatch: The generic method sort(List < T >) of type Collections is not applicable for the arguments (List < E >). The inferred type E is not a valid substitute for the bounded parameter < T extends Comparable < ? super T >>
So, I do a bit of documentation consultation, looking at sort
's specifications.
As a result, I attempt to ensure that sort
knows E
extends Comparable
:
The first line now looking like:
List<E extends Comparable<? super E>> l = new LinkedSet<>(arbSet);
Now, Eclipse states :
Syntax error on token "extends", , expected
What am I missing? I feel like this is a very basic example and that I am just missing something "palm-to-face" esque. Just to simplify it even further, all arbSet
elements are elements which implement the Comparable
interface.
You can use Collections.sort()
only if you provide a custom Comparator
or if the elements you are trying to sort implement the Comparable
interface. So it depends on what elements are stored in arbSet
.
For example, if you wanted to create a method that accepts a set, and returns a list with the set elements sorted, you would do something like this:
static <E extends Comparable<E>> List<E> sortedListFrom(Set<E> set) {
List<E> l = new LinkedList<>(set);
Collections.sort(l);
return l;
}
Edit:
If you want to do this inside a constructor you have two options:
Declare the type
E
just before the constructor. Of course, this won't do much, sincelist
is lost after the constructor finishes.class Test { <E extends Comparable<E>> Test(Set<E> arbSet) { List<E> list = new LinkedList<>(arbSet); Collections.sort(list); System.out.println(list); } }
Declare the type
E
in the class, so you can store the result in a attribute.class Test<E extends Comparable<E>> { List<E> list; Test(Set<E> arbSet) { this.list = new ArrayList<>(arbSet); Collections.sort(this.list); System.out.println(this.list); } }
这篇关于了解泛型排序的基础知识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!