简体   繁体   English

无法创建名为Entry的类

[英]Unable to create a class with name Entry

Following code fails compilation with error following error message : 以下代码无法编译,并显示以下错误消息:

Type mismatch: cannot convert from element type Test1.Entry to Map.Entry 类型不匹配:无法从元素类型Test1.Entry转换为Map.Entry

My question is can't we ever use class with name Entry in our project while using hash map ? 我的问题是,在使用hash map ,我们不能在项目中使用名为Entry类吗? And why this error is there though i did not import any Entry class here. 为什么这个错误存在,虽然我没有在这里导入任何Entry类。

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class Test1 {

    public static class Entry<T> {
        public String m() {
            return "A";
        }
    }

    public static void main(String[] args) {
        final Set<Entry> a = new HashSet<>();

        new HashMap<String, Entry>() {
            {
                for (final Entry entry : a) {
                    put(entry.m(), entry);
                }
            }
        };
    }
}

Is there any way I can keep this class name and code compiles successfully. 有什么方法可以保持这个类名和代码成功编译。

You have a issue with the names: 你的名字有问题:

your Entry<T> is shadowing at some point the java.util.Map.Entry<K, V> 你的Entry<T>在某些时候是java.util.Map.Entry<K, V>阴影

just rename the Entry to something else 只需将Entry重命名为其他内容即可

public static class MyEntry<T> {
    public String m() {
        return "A";
    }
}

You need to tell the compiler that you want to use Entry interface provided by java.util. 您需要告诉编译器您要使用java.util提供的Entry接口。

As you already have a class named Entry in the same package in which you are using it hence it is implicitly imported. 由于您已经在使用它的同一个包中有一个名为Entry的类,因此它是隐式导入的。 For all the places where you intent to use Entry from java.util you have to explicitly use java.util.Map.Entry instead of just Entry . 对于您打算使用java.util中的Entry所有地方,您必须显式使用java.util.Map.Entry而不仅仅是Entry This way you can use both the classes. 这样您就可以使用这两个类。

Since you are using double brace initialization the maps internals are exposed in that block. 由于您使用的是双括号初始化,因此映射内部会在该块中公开。

Either rename the Entry class or move the initialization code out of the init block of the HashMap so your Entry does not shadow the maps internal Entry implementation. 重命名Entry类或将初始化代码移出HashMap的init块,这样您的Entry不会影响映射内部Entry实现。

final HashMap<String, Entry> map = new HashMap<>();
for (final Entry entry : a) {
   map.put(entry.m(), entry);
}

This snapshot might clear your doubt, 这个快照可能会让你怀疑,

在此输入图像描述

As error popup states, the compiler is thinking that Inside your for loop it's java.util.Map.Entry but you want to traverse over your custom Entry class over here. 正如错误弹出窗口所述,编译器认为在你的for循环中它是java.util.Map.Entry但你想在这里遍历你的自定义Entry类。

Even though you assume that you aren't importing java.util.Map.Entry , Try removing 1st import (ie HashMap) and you will see that you are no longer getting Incompatible Types Error. 即使你假设你没有导入java.util.Map.Entry ,尝试删除第一个导入(即HashMap),你会发现你不再获得不兼容的类型错误。

You can try any of the suggested workarounds to get rid of this problem. 您可以尝试任何建议的解决方法来摆脱这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM