[英]Intellij idea java 8 hashmap can't find proper method
I copy data from HashMap<Class<? extends NXPersistentDataInt>, HashMap<Integer, ? extends NXPersistentDataInt>>
我从
HashMap<Class<? extends NXPersistentDataInt>, HashMap<Integer, ? extends NXPersistentDataInt>>
复制数据 HashMap<Class<? extends NXPersistentDataInt>, HashMap<Integer, ? extends NXPersistentDataInt>>
HashMap<Class<? extends NXPersistentDataInt>, HashMap<Integer, ? extends NXPersistentDataInt>>
to another same type hashmap named datamap ,and occured this error HashMap<Class<? extends NXPersistentDataInt>, HashMap<Integer, ? extends NXPersistentDataInt>>
到名为datamap的另一个相同类型的哈希图 ,并发生此错误
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() 在integerEntry.getValue()
IDE said : IDE说:
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>)
First things first. 首先是第一件事。 This is not an IDE error...
这不是IDE错误...
Well, you can't do it. 好吧,你做不到。 And the answer is right here: https://stackoverflow.com/a/2777297/3798111
答案就在这里: 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? 我使用过
List
,但是Map
吗? When we use capture of we cannot know the right implementation of the List
itself. 当我们使用捕获时,我们无法知道
List
本身的正确实现。 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. 所以,如果你加入,你会得到一个错误,让我们假设,实例
Child1
内部ArrayList<Child2>
和NOR编译器和IDE能保证你正在做的是正确的。
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. 请注意,我没有强制参数
someList
类型。
As you saw above, JAVA is tricky, so you can do a lot of sh* with it. 如上所述,JAVA非常棘手,因此可以使用它进行很多sh *操作。 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*. 我不希望您使用自己的
Map
进行此操作,但是在我们所知道的现实世界中……有时……有时……我们会做些sh *。 So feel free to use (do not say you know me) 因此,请随时使用(不要说您认识我)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.