简体   繁体   中英

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. 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; ^ 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>(); ^ where S#1,S#2 are type-variables: S#1 extends Object declared in class EntryTreeSet S#2 extends Comparable declared in class CountablePair

I was wondering how I would go about properly making my treeset hold instances of the CountablePair class.

Seems that the only problem in your code is the absense of bounds for the S parameter in EntryTreeSet declaration. 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. Try the below code.

public class EntryTreeSet<F,S extends Comparable<? super S>> implements Map.Entry<F,S> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM