ArrayList<Collection> gameList = new ArrayList <Collection>
(games.getValues());
games is an instance of this:
public class GameListManager {
TreeMap<String, GameRec> gameListTreeMap;
public GameListManager() {
gameListTreeMap = new TreeMap<String, GameRec>();
}
public Collection getValues() {
return gameListTreeMap.values();
}
...
}
As you can see getValues() returns a Collection, so I can't see what the
warning is trying to tell me.
Thanks very much
JB2006/jdk6.01
gameListTreeMap is a TreeMap<String, GameRec>, so the values method
returns Collection<GameRec>. Since your getValues method returns a
plain Collection without any generic type parameters, you've lost the
restriction to GameRec, which is an unchecked conversion from
Collection<GameRec> to Collection. There are a few ways you can get rid
of the problem.
1. Change your getValues method's return type to Collection<GameRec>.
This is probably the most type safe solution, but depending on how big
your API is, it might just propagate the problem up the tree. Now
anyone that calls your getValues method expecting a Collection is going
to get a similar warning. In other words:
Collection foo = gameListManager.getValues(); // warning!
2. Add a SuppressWarnings annotation to the getValues method. This
basically tells the compiler, "Shut up, I know what I'm doing." It
works, and you probably do know what you're doing, but my preference is
to only do that when I have no other choice (ex. the warning comes from
interacting with some 3rd party API that I can't change).
@SuppressWarnings("unchecked")
public Collection getValues() {
return gameListTreeMap.values();
}
--
Gillmer J. Derge [TeamB]
>> As you can see getValues() returns a Collection, so I can't see what the
>> warning is trying to tell me.
>
> Collection<GameRec> to Collection. There are a few ways you can get rid
> of the problem.
>
> 1. Change your getValues method's return type to Collection<GameRec>. This
> is probably the most type safe solution, but depending on how big your API
> is, it might just propagate the problem up the tree. Now anyone that
> calls your getValues method expecting a Collection is going to get a
> similar warning. In other words:
>
> Collection foo = gameListManager.getValues(); // warning!
Thanks very much for your help, this is very kind of you.
I am in control of all the source, and the api is pretty much limited to
these 2 classes (at present!).
As expected, when I changed the GameListManagers getValues() to read
public Collection<GameRec> getValues() {
return gameListTreeMap.values();
}
I found it compiled fine but then the original calling line now gives these
2 errors:
"GameListRenderer.java": cannot find symbol; symbol : constructor
ArrayList(java.util.Collection<GameRec>), location: class
java.util.ArrayList<java.util.Collection> at line 94, column 40
"GameListRenderer.java": internal error; cannot instantiate
java.util.ArrayList.<init> at java.util.ArrayList<java.util.Collection> to
() at line 94, column 40
I'm trying to think this through by describing it in English - I think I'm
asking for an ArrayList of GameRecs, but we know games.getValues() now
returns the type safe Collection so its almost as if the syntax end up like
ArrayList<Collection<GameRec>> gameList =... , but that wouldn't compile at
all
(the original caller is ArrayList<Collection> gameList = new ArrayList
<Collection> (games.getValues()); ).
I too would like a clean build without "hacks". There are a few others of
this ilk relating to Comparators, so I'd like to fix those having understood
this one.
Your new ArrayList isn't going to be a list of Collection's. It's a
list of GameRec's, containing the same GameRec's that are in the
games.getValues() collection.
ArrayList<GameRec> gameList = new ArrayList<GameRec>(games.getValues());
That did it - thanks. I even fixed the Comparater one after your help too -
thanks again.
-- John
www.phonewebcam.com