[英]Treeset Implementation of Map.Entry with Generic Types
I'm building a frequency table implementation in which hashmap table elements are chained treesets. 我正在构建一个频率表实现,其中哈希表表元素是链接的树集。 The key/value pair class is provided with a class definition as follows: 键/值对类提供了如下类定义:
public class CountablePair<F, S extends Comparable<? super S>> implements Comparable<S> {
I wish to make a new class EntryTreeSet that implements Map.Entry, and includes within it a treeset of CountablePairs. 我希望创建一个新类EntryTreeSet,该类实现Map.Entry,并在其中包括CountablePairs树集。 My current code for the class declaration, its treeset variable, and its constructor are as follows: 我当前用于类声明,其树集变量及其构造函数的代码如下:
public class EntryTreeSet<F,S> implements Map.Entry<F,S> {
private TreeSet<CountablePair<F,S>> tree;
public EntryTreeSet() {
this.tree = new TreeSet<CountablePair<F,S>>();
}
However, this yields the following error statements: 但是,这将产生以下错误声明:
EntryTreeSet.java:19: error: type argument S#1 is not within bounds of type-variable S#2 private TreeSet> tree; EntryTreeSet.java:19:错误:类型参数S#1不在类型变量S#2私有TreeSet>树的范围内; ^ where S#1,S#2 are type-variables: S#1 extends Object declared in class EntryTreeSet S#2 extends Comparable declared in class CountablePair EntryTreeSet.java:22: error: type argument S#1 is not within bounds of type-variable S#2 this.tree = new TreeSet>(); ^其中S#1,S#2是类型变量:S#1扩展了在类EntryTreeSet中声明的对象S#2扩展了在类CountablePair EntryTreeSet.java中声明的Comparable:错误:类型参数S#1不在以下范围内类型变量S#2 this.tree = new TreeSet>(); ^ where S#1,S#2 are type-variables: S#1 extends Object declared in class EntryTreeSet S#2 extends Comparable declared in class CountablePair ^其中S#1,S#2是类型变量:S#1扩展在类EntryTreeSet中声明的对象S#2扩展在类CountablePair中声明的Comparable
I was wondering how I would go about properly making my treeset hold instances of the CountablePair class. 我想知道如何使树集容纳CountablePair类的实例。
Seems that the only problem in your code is the absense of bounds for the S
parameter in EntryTreeSet
declaration. 似乎您的代码中唯一的问题是EntryTreeSet
声明中S
参数的边界不EntryTreeSet
。 Such class declaration compiles correctly: 这样的类声明可以正确编译:
public class EntryTreeSet<F, S extends Comparable<? super S>>
implements Map.Entry<F, S> {
S is a raw type and that why the compilation error is coming. S是原始类型,这就是为什么会出现编译错误。 Try the below code. 试试下面的代码。
public class EntryTreeSet<F,S extends Comparable<? super S>> implements Map.Entry<F,S>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.