Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting Map<Integer, Set<Object>>

Tags:

java

casting

I have a method that takes a Map<Integer, Set<Object>> as parameter. I need to call it from two different locations, using a Map<Integer, Set<String>> and a Map<Integer, Set<Integer>> as parameter.

The compiler complaint so I changed the method parameter signature to Map<Integer, ?>, and now I can call it, but have different problems. The method is basically as follows:

private void methodA (Map<Integer, ?> inOutMap, Integer key, Object value) {

        Set<Object> list = new HashSet<Object>();

        if (!inOutMap.containsKey(key)) {
            list.add(value);
        } else {
            list = (Set<Object>) (Set<?>) inOutMap.get(key); //I wrote the cast, but looks quite ugly
            list.add(value);
        }

        inOutMap.put(key, list); //compiler error
        //The method put(Integer, capture#4-of ?) in the type Map<Integer,capture#4-of ?> is not applicable for the arguments (Integer, Set<Object>)
    }

Is there any way of solving the compiler error? This is, casting list to ?.

My second question is conceptual. Is there any better way of doing this besides writting two different methods with different parameters signature?

like image 816
Luis Sep Avatar asked May 03 '13 10:05

Luis Sep


People also ask

Can we use int in map?

int is a primitive data type. It only defines that the value is an integer in binary form. It has no inherent methods or fields that you can call. It is not an object and can not be used in an Object reference.

Can we use integer as key in HashMap?

It can store different types: Integer keys and String values or same types: Integer keys and Integer values. HashMap is similar to HashTable, but it is unsynchronized. It is allowed to store null keys as well, but there can only be one null key and there can be any number of null values.

Can we use primitives as key in HashMap?

The keys and values of a map can be any reference type. We can't use primitive types because of a restriction around the way generics were designed. A HashMap allows one null key and multiple null values.

How define Map in Java?

A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical function abstraction.


2 Answers

declare it as

private <T> void methodA (Map<Integer, Set<T>> inOutMap, Integer key, T value) {

        Set<T> list = new HashSet<T>();

        if (!inOutMap.containsKey(key)) {
            list.add(value);
        } else {
            list = inOutMap.get(key); 
            list.add(value);
        }

        inOutMap.put(key, list); 
    }

Its always good to use Generics when you are trying to use multiple types of arguments, than using Object or ? (unknown type)

Now you can invoke the same method using Set containig different types like below

Map<Integer, Set<String>> m1 = new HashMap<Integer, Set<String>>();
Map<Integer, Set<Integer>> m2 = new HashMap<Integer, Set<Integer>>();

methodA(m1, 1, "t");
methodA(m2, 2, 2);
like image 130
sanbhat Avatar answered Oct 19 '22 03:10

sanbhat


This compiles without errors and without explicit casting

private <T> void methodA (Map<Integer, Set<T>> inOutMap, Integer key, T value) {
    Set<T> list = new HashSet<T>();
    if (!inOutMap.containsKey(key)) {
        list.add(value);
    } else {
        list = inOutMap.get(key);
        list.add(value);
    }
    inOutMap.put(key, list);
}
like image 40
Evgeniy Dorofeev Avatar answered Oct 19 '22 05:10

Evgeniy Dorofeev