gcalex5
gcalex5

Reputation: 1111

Map is a raw type. References to generic type Map<K,V> should be parameterized

Whenever I declare a map object I always get the below warning on the declartion, getter, and setter.

Map is a raw type. References to generic type Map<K,V> should be parameterized  

My declarations are:

import java.util.Map;
private Map pricingInfo;
pricingInfo = getCurrentSession();

The map is grabbing the current session information I stored in the previous Struts2 Action and passing them into a generic Java Object. The code I use to store it is included below to give a little more context on what I'm doing.

ServletActionContext.getRequest().getSession().putValue("pricing", this);

However I get all the expected values and have yet to run into a runtime error.

So the question is should I just use annotations to make these warnings go away or is there a 'correct' way to go about instantiating and using these variables?

Upvotes: 0

Views: 203

Answers (2)

Jurgen
Jurgen

Reputation: 2154

Whenever possible one should specify what Type of values a collection will hold. There are two benefits to doing this:

  1. The first is that the compiler can check that you do not mistakenly put an incorrect Type of value into the collection.

  2. The second is that you don't have to cast from Object to whatever Type of value it is when you retrieve the value/object from the collection.

In the case of a Map you need to specify two Types: Map<K,V>

Replace K with the Type of the keys in the Map, and V with the Type of values being held in the Map.

Upvotes: 2

dharr
dharr

Reputation: 328

Specify the type for key and value:

   Map<String, String> aMethod(aType type);

Upvotes: 3

Related Questions