Type safety warning

Im getting a warning that states

"Type safety: The method put(Object, Object) belongs to the raw type TreeMap.

References to generic type TreeMap<K,V> should be parameterized"

in the following code

public class....{

TreeMap entriesFlights =new TreeMap();

publicvoid addFlight(Flight aFlight){

String message;

//check if entry with the same name already exists

if(this.entriesFlights.keySet().contains(aFlight.getStartDest())){

message ="An entry with the name " + aFlight.getStartDest()+" already exists \n";

message = message + aFlight.getStartDest() +" has not been added";

reportError(message);

}

else{

this.entriesFlights.put(aFlight.getStartDest(),aFlight);//THIS IS THE LINE THAT IS UNDERLINED IN YELLOW (WARNING)

message ="Entry for " + aFlight.getStartDest();

message = message +" has been added to Airline Company";

confirmationMessage(message);

}

}

Im not sure what this type safety warning sign means.

My program runs fine without it (so far), but concerned that having programs with warning signs is bad practice.

[1748 byte] By [B_Reala] at [2007-11-27 1:39:53]
# 1
TreeMap<KeyType, ValueType> entriesFlights = new TreeMap<KeyType, ValueType>();KeyType and ValueType get replaced with whatever types are appropriate for that map. http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
jverda at 2007-7-12 0:53:27 > top of Java-index,Java Essentials,New To Java...
# 2
Do I need to parameterize it then or can I leave it as it is? And what exactly is parameterization in laymans terms? (I did read your link, but struggled to get a grasp of what it was saying)
B_Reala at 2007-7-12 0:53:27 > top of Java-index,Java Essentials,New To Java...
# 3

So I guess you want something like:

TreeMap<String, Flight> entriesFlights = new TreeMap<String, Flight>();

Assuming GetDest returns a String.

The parameters are just telling everyone (developer and compiler) what type of objects are stored in the Map. This allows the compiler to check that you are not trying to put something else in there.

pthorsona at 2007-7-12 0:53:27 > top of Java-index,Java Essentials,New To Java...
# 4

> Do I need to parameterize it then or can I leave it

> as it is?

>

You can leave it. The messages are just warnings, not errors, so they don't stop things from compiling or running. But yes, it is generally bad form to have warnings in your code. You can use an @ignore annotation or something like that to supress the warnings. It should be described in that generics page, or the pdf that's linked at the bottom of it.

> And what exactly is parameterization in laymans

> terms?

It's a way to specify a relationship among types at declaration time, and what exactly those types are at usage time. It lets you use the same code for different types, and have the compiler enforce that the usages of those types is consistent--for example, you don't put a String into a List and then try to get a Date out.

jverda at 2007-7-12 0:53:27 > top of Java-index,Java Essentials,New To Java...