简体   繁体   English

Map.entry(...) 对于类型 Map 未定义

[英]Map.entry(...) is undefined for type Map

I was messing around with Java and hashmaps on a Computer at School.我在学校的电脑上玩弄 Java 和哈希图。

I was able to instantiate an Entry of type String, Integer on the fly.我能够即时实例化一个String, Integer类型的Entry Then add it to an ArrayList of Entrie s.然后将其添加到EntrieArrayList中。 I emailed this code to myself for later.我将此代码通过电子邮件发送给自己以备后用。

However on my home computer and my laptop, it wont let me.但是在我的家用电脑和笔记本电脑上,它不会让我。 Same exact code says相同的确切代码说

The method entry(string,int) is undefined for type Map未定义Map类型的方法entry(string,int)

The code is:代码是:

Map.Entry<String, Integer> entry = Map.entry(largest, maxCount);

I think this has something to do with the fact that Map.Entry is an interface?我认为这与Map.Entry是一个接口有关?

As google results seem to say you have to make your own Entry class to do this?正如谷歌结果似乎说你必须制作自己的Entry课程才能做到这一点? But did that change in newer versions of Java?但是在较新版本的 Java 中,这种情况发生了变化吗? Why was I able to execute this code just fine earlier?为什么我之前可以很好地执行此代码?

Explanation解释

Lookup the Javadoc of this method.查找此方法的Javadoc It says:它说:

Since: 9自:9

Which means that this method was added in Java 9.这意味着这个方法是在 Java 9 中添加的。

So your machine at home likely did not have Java 9 or newer.所以你家里的机器可能没有 Java 9 或更新版本。


Upgrade升级

Run java -version and javac -version in your CMD to verify your version.在 CMD 中运行java -versionjavac -version以验证您的版本。 Then install a more recent Java (for example from AdoptOpenJDK ).然后安装更新的 Java(例如来自AdoptOpenJDK )。


Pre Java 9前 Java 9

In case you are searching for a solution that works without Java 9, you can use something like:如果您正在寻找无需 Java 9 即可运行的解决方案,您可以使用以下方法:

Map.Entry<String, Integer> entry = new AbstractMap.SimpleEntry<String, Integer>(largest, maxCount);

As explained in more detail here: Java - How to create new Entry (key, value)如此处更详细的解释: Java - How to create new Entry (key, value)

You said:你说:

As google results seem to say you have to make your own Entry class to do this?正如谷歌结果似乎说你必须制作自己的 Entry 课程才能做到这一点?

That is true for versions before Java 6. The mentioned class AbstractMap.SimpleEntry was added in Java 6 and can be (ab)used for this purpose. Java 6 之前的版本也是如此。提到的类AbstractMap.SimpleEntry是在 Java 6 中添加的,可以(ab)用于此目的。 Before that, you would have to create your own implementation.在此之前,您必须创建自己的实现。


OOP面向对象编程

Another option, which might arguably be better, could be to create your own little class to hold that data and then give it a proper name.另一种可能更好的选择可能是创建您自己的小类来保存该数据,然后给它一个适当的名称。

For example, instead of having a Map.Entry<String, Integer> that stores a person by his String name and int age , why not create a Person class directly:例如,与其使用Map.Entry<String, Integer>来存储一个人的String nameint age ,不如直接创建一个Person类:

public class Person {
    private final String name;
    private final int age;

    // Constructor and getters, ...
}

Is much more readable, easier to maintain and extend.更具可读性,更易于维护和扩展。

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

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