简体   繁体   中英

Java + Eclipse: The method computeIfAbsent(String, (<no type> k) -> {}) is undefined for the type

I am using Eclipse (Mars.1 Release (4.5.1)) and encountering the following error: The method putIfAbsent(String, new PriorityQueue<>()) is undefined for the type Map<String,PriorityQueue<String>> . Everything seems done correctly but still getting the error. What could I be missing?

Map<String, PriorityQueue<String>> targets = new HashMap<>();

...


for(String[] ticket: tickets) {
    targets.putIfAbsent(ticket[0], new PriorityQueue<>());
}

EDIT Did the following, yet still getting the error

在此处输入图片说明

在此处输入图片说明

EDIT 2

Added Java 8 SE, yet still getting the error.

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

EDIT 3

在此处输入图片说明

There's no method Map#putIfAbsent(K key, V value) in Java 7, but there's in Java 8. See:

In your specific case, make sure the project properties under Eclipse (described below) are set to Java 8. You can edit them by doing: Right click on project > Properties :

  • Project properties > Java Build Path
  • Project properties > Java Comiler

Here's a possible solution:

Java构建路径

If your Java Build Path is targeted to Java 7, follow the below steps to change it:

  • Select JRE System Library
  • Click button Edit... located on the right side
  • Use one of the following options
    • Global: Workspace default JRE. Make sure it is set to Java 8
    • Local: Alternate JRE. Choose Java 8.

After setting this, you can continue to check the Java Complier.

Java Complier-项目设置 Java Complier-Eclipse设置

Now, you should run your project without problem.


Do I have to do this to each project individually or does it apply to all projects?

You don't have to do this for each project. You can have a set-up for all projects of this workspace.

To setup Java Runtime Environments for all the projects, you can check which JREs are installed and change the default one.

  • Go to Eclipse > Preferences...
  • Search JRE
  • Check the installed JREs
    • If your recently installed JREs aren't appeared, then add them manually one by one using button Add... . Or add them automatically using the button Search... .
    • Select the default JRE that you want

As the screenshot shown: By default, the checked JRE is added to the build path of newly created Java projects. Now it should be OK for all the projects.

已安装的JRE

To edit the JRE for a specific project, you can. Here're the steps to do it:

  • Select the target project, right click > Properties
  • Select Java Build Path
  • Edit JRE System Library
  • Choose your preferred JRE version from
    • Execution Environment
    • Alternate JRE
    • Workspace default JRE

Although you have set the compiler settings for Java 8, your project still references the Java 7 System Library in its Build Path (according to the las screen shot in the question, Edit 3 ). You need to select that system library ( JavaSE-1.7 ) and use the Edit... button to change it to JavaSE-1.8 ) - the one you added in your "Edit 2" update.

In case you're wondering, the difference is between how the compiler produces .class files for your code (the Compiler Compliance setting) versus the version of the Java standard libraries that are used to compile against (The JRE System Library ). The two things are separate but usually configured to match each other.

It is supported only in Java 8.

Make sure the following are checked:

  1. go to your environment variable (start -> type environment variable in search box)
  2. make sure your java_home has your bin for jdk 1.8.x version and it is included in your path. (reference you can also look here: Environment variables for java installation )

After the environment variable is set restart your eclipse. Undefined should go away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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