Menelaos
Menelaos

Reputation: 26549

Cannot convert from Set<Map.Entry<String,capture#2-of ? extends A>> to Set<Map.Entry<String,? extends A>>

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?

Upvotes: 2

Views: 3054

Answers (2)

Ray Hulha
Ray Hulha

Reputation: 11221

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());
        }
    }
}

Upvotes: 1

rgettman
rgettman

Reputation: 178313

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();

Upvotes: 4

Related Questions