I’ve got some legacy code that I want to upgrade to generics:
/**
* Places in this World
*/
public Map places;
/**
* Players watching this World
*/
public Collection players;
/**
* A reference to the Adventure using this World
*/
public Adventure owner;
/**
* Create a World. `a' is a reference to the Frame itself.
* This reference is used when loading images and sounds.
*
* @param a An instance of adventure.
*/
public World( Adventure a ) {
places = new HashMap();
players = new LinkedList();
owner = a;
}
My IDE warns me that I have not parametrized the variables places and players so I should add generics to this code but how? When I add <> or <Place> to the `places´ object then it says it is not generics so however I do it turns out wrong. Could you tell me how to modernize this part of my code to using generics?
Thanks
As for
places…First, add types to
places. Assuming each value is aPlace, and each key is aString:(You need two types: one for the keys, and one for the values.)
Then, in your constructor, do the same thing.
Like so:
The other fields are simpler;
LinkedListandCollectionshould only require one type, and if this is old old code,Adventure(being part of that code) wouldn’t need any.