0

I copy data from HashMap<Class<? extends NXPersistentDataInt>, HashMap<Integer, ? extends NXPersistentDataInt>> to another same type hashmap named datamap,and occured this error

    for (Map.Entry<String, AnalysisTaskDataobj> stringAnalysisTaskDataobjEntry : getTaskDataobjs().entrySet()) {
        HashMap<Class<? extends NXPersistentDataInt>, HashMap<Integer, ? extends NXPersistentDataInt>> hashMap = stringAnalysisTaskDataobjEntry.getValue().getDataMap();
        for (Map.Entry<Class<? extends NXPersistentDataInt>, HashMap<Integer, ? extends NXPersistentDataInt>> classHashMapEntry : hashMap.entrySet()) {
            datamap.putIfAbsent(classHashMapEntry.getKey(), new HashMap<>());
            for (Map.Entry<Integer, ? extends NXPersistentDataInt> integerEntry : classHashMapEntry.getValue().entrySet()) {
                HashMap<Integer, ? extends NXPersistentDataInt> integerHashMap = datamap.get(classHashMapEntry.getKey());
                integerHashMap.put(integerEntry.getKey(), integerEntry.getValue());
            }
        }
    }

at integerEntry.getValue()

IDE said :

put (Integer,capture<? extends com.cae_analysis.model.data.nx.NXPersistentDataInt>) in HashMap cannot be applied to (Integer,capture<? extends com.cae_analysis.model.data.nx.NXPersistentDataInt>)
shmosel
  • 49,289
  • 6
  • 73
  • 138
zssng
  • 5
  • 1
  • 4

1 Answers1

0

First things first. This is not an IDE error...

Well, you can't do it. And the answer is right here: https://stackoverflow.com/a/2777297/3798111

As you can see, you're violating some rules, think about something like this:

abstract class Super {
}

class Child1 extends Super {
}

class Child2 extends Super {
}

private static List<? extends Super> myList;

public static void main(String[] args) {
    myList.add(new Child1());
    myList.add(new Child2());
}

I have used a List, but it's the same for Map ok? When we use capture of we cannot know the right implementation of the List itself. So you would get an error if you add a, let's suppose, instance of Child1 inside an ArrayList<Child2> and nor the compiler and the IDE can assure you're doing it right.

It's better explained in the link I have sent. But I will give you a workaround, but I really discourage it's usage... That sayd, hold it:

static abstract class Super {
    public String saySomething() {
        return "I'm Super";
    }
}

static class Child1 extends Super {
    @Override
    public String saySomething() {
        return "I'm Child1";
    }
}

static class Child2 extends Super {
    @Override
    public String saySomething() {
        return "I like coffee!";
    }
}

private static List<? extends Super> myList;

public static void main(String[] args) {
    myList = new ArrayList<>();
    addSomeData(myList, new Child1());
    addSomeData(myList, new Child2());

    myList.forEach(element -> System.out.println(element.saySomething()));
}

public static void addSomeData(List someList, Object data) {
    someList.add(data);
}

Note that I didn't force the param someList type.

As you saw above, JAVA is tricky, so you can do a lot of sh* with it. I don't want you to do this with your Map, but in the real world as we know... sometimes... in a while... we do some sh*. So feel free to use (do not say you know me)

R. Karlus
  • 2,094
  • 3
  • 24
  • 48