Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Generics, Implementing an Interface of Type Iterable<E>

Tags:

java

generics

I have two "Sets": one is a class, GenericSet, the other is an interface, ISet. class GenericSet implements ISet. The class signatures are as follows:

interface ISet<E entends Iterable<E>> { .... }

The idea - of course - is that the interface defines some methods I want to see implemented by the implementing class, GenericSet, whose signature follows (here's where I am stuck):

class GenericSet<E extends Iterable<E> implements ISet<Iterable<<E>> { .... }

I get the warning:

Bound mismatch: The type Iterable is not a valid substitute for the bounded > parameter > of the type ISet

The general idea of class GenericSet is that - while implementing ISet - it is able to facilitate the receipt of many types of objects, creating a set of the received type.

What am I missing? I know the error message explains it; however, as I am new to Generics, I don't understand what would be a "valid substitute" for the bounded parameter.

like image 300
Thomas Avatar asked Dec 29 '25 02:12

Thomas


1 Answers

As far as I can tell, the issue is that ISet doesn't also extend Iterable like

public interface ISet<E extends Iterable<E>> extends Iterable<E> {
}

That way it satisfies the condition of also being an Iterable.

Also, I think you meant

class GenericSet<E extends Iterable<E>> implements ISet<E> {

}

which is then presumably used to implement an Iterable Set.

like image 191
Elliott Frisch Avatar answered Dec 30 '25 16:12

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!