2

I'm trying to do the following to avoid an unchecked cast later on:

Code:

Set<Entry<String, ? extends SerializableForLayout>> set = 
layoutObjectList.entrySet();

Error:

Type mismatch: cannot convert from Set<Map.Entry<String,capture#2-of ? 
extends SerializableForLayout>> 
to Set<Map.Entry<String,? extends SerializableForLayout>>

Whole Code:

public void loadLayoutList(ArrayList<SerializableForLayout> preparedList,
                           ConcurrentHashMap<String, ? extends SerializableForLayout> layoutObjectList 
                               )
    {       
        SerializableForLayout t;
        if (layoutObjectList == null)return;

        Set<?> set = layoutObjectList.entrySet();       
        Iterator<?> itTags = set.iterator();
        Entry<String, SerializableForLayout> entry;

        while (itTags.hasNext()) {
            entry = (Entry<String, SerializableForLayout>) itTags.next();
            t = entry.getValue();
            addForSerializableLayouts(t,preparedList);
        }
        Collections.sort(preparedList, ApplicationsSort.LayoutWeightComparator);
    }

This works:

Set<?> set = layoutObjectList.entrySet();

But there is a warning:

Forced to suppress an unchecked cast on the line:

entry = (Entry<String, SerializableForLayout>) itTags.next();

How do I do this without needing to suppress?

Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • If you want compile time type safety, you have to be able to tell it what the types are at compile time. If you don't know until runtime, you will get unchecked cast warnings, that's what the warning means. Lots of "real" APIs are full of suppressing unchecked, it's just a reality of dealing with runtime types in java. – Affe Feb 13 '14 at 17:48
  • possible duplicate of [Bounded-wildcard related compiler error](http://stackoverflow.com/questions/18907262/bounded-wildcard-related-compiler-error) – Paul Bellora Feb 13 '14 at 19:52

2 Answers2

4

The problem is that you are using multiple ?s and in Java they aren't guaranteed to be the same. Make this method generic, so that you have a generic type parameter to reference and to be the same throughout the method.

public <T extends SerializableForLayout>
    void  loadLayoutList(ArrayList<SerializableForLayout> preparedList,
                         ConcurrentHashMap<String, T> layoutObjectList)

Then you should be able to properly define your set variable, using T.

Set<Map.Entry<String, T>> set = layoutObjectList.entrySet();
rgettman
  • 176,041
  • 30
  • 275
  • 357
1

In my case I needed:

public static <K, V> void printMap(Map<K, V> map) {
        Set<Entry<K, V>> entrySet = map.entrySet();
        for (Entry<K, V> entry : entrySet) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
}
Ray Hulha
  • 10,701
  • 5
  • 53
  • 53