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 :
Here's a possible solution:
If your Java Build Path is targeted to Java 7, follow the below steps to change it:
After setting this, you can continue to check the Java Complier.
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.
JRE
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.
To edit the JRE for a specific project, you can. Here're the steps to do it:
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:
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.