Whats the best to way convert a set of Java objects to another set of objects?

Posted by HDave on Stack Overflow See other posts from Stack Overflow or by HDave
Published on 2010-04-30T16:03:18Z Indexed on 2010/04/30 16:27 UTC
Read the original article Hit count: 173

Filed under:
|
|
|

Basic Java question here from a real newbie. I have a set of Java objects (of class "MyClass") that implement a certain interface (Interface "MyIfc"). I have a set of these objects stored in a private variable in my class that is declared as follows:

protected Set<MyClass> stuff = new HashSet<MyClass>();

I need to provide a public method that returns this set as a collection of objects of type "MyIfc".

public Collection<MyIfc> getMyStuff() {...}

How do I do the conversion? The following line gives me an error that it can't do the conversion. I would have guessed the compiler knew that objects of class MyClass implemented MyIfc and therefore would have handled it.

Collection<MyIfc> newstuff = stuff;

Any enlightenment is appreciated.

© Stack Overflow or respective owner

Related posts about java

Related posts about collections