1

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

enter image description here

enter image description here

EDIT 2

Added Java 8 SE, yet still getting the error.

enter image description here

enter image description here

enter image description here

EDIT 3

enter image description here

  • 1
    Is `ticket` a `String[]` then? – 4castle Dec 06 '16 at 03:31
  • 1
    `new PriorityQueue()` should be `new PriorityQueue<>()` – 4castle Dec 06 '16 at 03:35
  • @4castle That would be a warning, not a compiler error. – shmosel Dec 06 '16 at 03:37
  • @shmosel You're right. [I can't reproduce the problem.](https://ideone.com/RPBOIO) – 4castle Dec 06 '16 at 03:44
  • 1
    Are you using Java 8? It may be that you have an older JDK installed, or your Eclipse isn't configured to use Java 8. – 4castle Dec 06 '16 at 03:49
  • @4castle sorry but how can I check? Sorry but it was in a for loop, `for(String[] ticket: tickets) {...}`. –  Dec 06 '16 at 10:51
  • @4castle is right, this is clearly a language level issue, check your project config and make sure you are using JDK 1.8, see http://stackoverflow.com/questions/12588537/how-to-change-jdk-version-for-an-eclipse-project –  Dec 11 '16 at 08:10
  • 1.7 is not 1.8, no? –  Dec 11 '16 at 08:39
  • Possible duplicate of [Eclipse + Java 8 support?](http://stackoverflow.com/q/15027255/5743988) – 4castle Dec 11 '16 at 16:42
  • @RC. please take a look at the original post. Converted to 1.8 yet still have the error. What could I be missing? –  Dec 13 '16 at 08:10
  • @4castle Appreciate the lead and took a look at it, but it is for Kepler mainly. How can I do it for Mars.1 Release (4.5.1)? –  Dec 13 '16 at 08:14
  • `new PriorityQueue()` with String. Seems you are in a required/inferred type pingpong. – Joop Eggen Dec 13 '16 at 08:18
  • @JoopEggen What do you mean? –  Dec 14 '16 at 05:52
  • I am not using eclipse at the moment, but I wondered using `` instead of `<>` would help. – Joop Eggen Dec 14 '16 at 15:29

3 Answers3

0

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 Build Path

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 - project settings Java Complier - Eclipse settings

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.

Installed JREs

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
Mincong Huang
  • 4,311
  • 5
  • 32
  • 53
  • What is the quickest way to convert it to Java 8? –  Dec 13 '16 at 07:43
  • please take a look at the original post. I converted to Java SE 8, yet I'm still getting the same error. –  Dec 13 '16 at 07:53
  • I am using Eclipse Version: Neon Milestone 6 (4.6.0M6) and Java 1.8.0_111 and it seams works fine. To upgrade Eclipse save Projects (not fully Workspace - because new Eclipse will use new configuration of AppServer Java and so on.) -> Download new Version of Eclipse -> open Eclipse -> Import existing project into workspace :))) – Musa Dec 13 '16 at 14:11
  • @LyManeug, you did set the compiler to _1.8_, but in [your last image](https://i.stack.imgur.com/7zJZu.png), it shows that there's a warning _"When selecting 1.8 compilance, make sure the have a compatible JRE installed and activated **(currently 1.7)**"_. So your JRE is still 1.7. Try to configure it through the link "Configure..." – Mincong Huang Dec 13 '16 at 19:57
  • @Musa do you mind making an answer along with screenshots? Would really help! –  Dec 13 '16 at 21:07
  • @MincongHuang I updated the screenshot in the original post with the warning gone. Yet the error still shows. Did you test it out with Mars? –  Dec 13 '16 at 21:09
  • @LyManeug I uploaded the screenshot, please see the update. I'm not sure where does the problem locate. I used Eclipse Neon, but JDK version is really a basic setting, it has nothing to do with Eclipse version, e.g. Mars. – Mincong Huang Dec 13 '16 at 21:49
  • @MincongHuang Please take a look at the original post. Put a new screenshot. Found have found the problem. It says that the project has Java SE 1.7 as the build path. How can I add the Java SE 8 like yours? –  Dec 14 '16 at 06:02
  • @MincongHuang Got it! But my question is do I have to do this to each project individually or does it apply to all projects? –  Dec 15 '16 at 02:33
0

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.

E-Riz
  • 28,616
  • 7
  • 83
  • 119
  • In "Edit 2", the box is checked off for JavaSE-1.8? Do you mind guiding me via screenshots? –  Dec 15 '16 at 02:30
  • I said "the last screenshot in the question," where it shows your *project* is still configured to use JavaSE-1.7. You've added a Java 8 library, but you still need to configure that specific project to use it. – E-Riz Dec 15 '16 at 15:01
-1

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.

Community
  • 1
  • 1
logger
  • 1,818
  • 3
  • 22
  • 44
  • Sorry but where is the environment variable? I'm using mac. Do you mind showing via screenshots? –  Dec 14 '16 at 05:54
  • here is the reference: http://stackoverflow.com/questions/22842743/setting-java-home-environment-variable-on-mac-osx-10-9 – logger Dec 14 '16 at 13:51
  • This answer is completely wrong for Eclipse; Eclipse doesn't use the JAVA_HOME or other environment variables. – E-Riz Dec 14 '16 at 15:28