0

I want to write a method which takes a Map as an parameter, which can contain any object as its value. I am trying to implement it using generics, but I am unable to implement it.

Below is the code that I have tried so far. This code as two methods: genericTest, where I am trying to implement a generic solution, and nonGenericTest, which is what I actually want to do with the generic solution but is implemented without using generics. Basically, the second method is the one which I want to convert to generics.

What am I doing wrong?

import java.util.Iterator;
import java.util.Map;

public class Test {
    //V cannot be resolve to a type
    public void genericTest(Map<String, V> params) {
    }

    public void nonGenericTest(Map<String, Object> params) {

        Iterator<String> it = params.keySet().iterator();
        while (it.hasNext()) {
            String key =  it.next();
            Object value = params.get(key);
            if(value instanceof String){
                String val1=(String) value;
                // do something 
            }else if(value instanceof Integer){
                Integer val1 = (Integer) value;
            } 
        }
    }
}

I am getting compiler error as shown in the code.

3
  • You need to declare the generic in the method: public <V> void genericTest(Map<String, V> params) { ... } Commented Jun 17, 2015 at 15:06
  • You're looking for the documentation. Commented Jun 17, 2015 at 15:06
  • Seems like the values are either Strings or Integers, or a mixture of both, rather than just any object. Commented Jun 17, 2015 at 15:09

2 Answers 2

8

You've got two options:

  • Introduce V as a type parameter to the class.

    public class Test<V>
    
  • Introduce V as a type parameter to that function.

    public <V> void genericTest(Map<String, V> params)
    
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest to use it with the class definition like this:

public class SettingsClient<T> extends TestWebserviceClient {

    private Map<String, List<T>> mapWithAllPojoLists = new HashMap<>();

}

Inside the method, you can read/ write to the map like this:

   public List<Discount> getListOfDiscounts() {

        if (mapWithAllPojoLists.get("discount") != null) {
            return (List<Discount>) mapWithAllPojoLists.get("discount");
        }

        GetDiscountsResponse response = getGetDiscountsResponse();
        ArrayOfDiscount arrayOfDiscount = response.getGetDiscountsResult().getValue();

        List<Discount> discounts = arrayOfDiscount.getDiscount();

        if (discounts == null) {
            return new ArrayList<>();
        }

        mapWithAllPojoLists.put("discount", (List<T>) discounts);
        return discounts;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.