15

I'm trying to declare and define larger hash map at once. This is how I do it:

public HashMap<Integer, Callable<String>> opcode_only = new HashMap<Integer, Callable<String>>() {{
    put(x, y);
    put(x, y);
}};

But, when I try to use lambda expressions in body of put, I'm hitting on eclipse warrning/error. This is how I use lambda in HashMap:

public HashMap<Integer, Callable<String>> opcode_only = new HashMap<Integer, Callable<String>>() {{
    put(0, () -> { return "nop"; });
    put(1, () -> { return "nothing...."; });
}};

Eclipse underlines whole part of lambda starting with comma before. Error messages:

Syntax error on token ",", Name expected    
Syntax error on tokens, Expression expected instead

Does anybody know what am I doing wrong? Is initialization by lambda expression allowed in HashMap? Please help.

fge
  • 119,121
  • 33
  • 254
  • 329
user35443
  • 6,309
  • 12
  • 52
  • 75
  • 1
    Compiles in `javac`. Are you sure that Java 8 is enabled for your project? – axtavt Jun 28 '13 at 14:14
  • Looks like youre compiling with a compiler earlier then version 8 – Reimeus Jun 28 '13 at 14:15
  • I'm not sure if Eclipse is fully updated for [Java 8](http://wiki.eclipse.org/JDT_Core/Java8) yet. Also, see [Efficiency of Java “Double Brace Initialization”](http://stackoverflow.com/q/924285/758280). – Jeffrey Jun 28 '13 at 14:18
  • uh. Then it's just eclipse problem? Will it work when compiling manually? – user35443 Jun 28 '13 at 14:20
  • If you are looking for how to declare a fully populated `HashMap` field using a lambda, see this question and answer: [How to initialize a map using a lambda?](http://stackoverflow.com/q/32868665/773113) – Mike Nakis Sep 30 '15 at 14:27
  • 1
    I have now checked that this code perfectly compiles and run on Eclipse (4.4.1, a 1 year old release) so this appears to be most likely an issue due to an early release of Eclipse's support for Java 8. – Didier L Sep 30 '15 at 22:25

3 Answers3

7

This works fine in the Netbeans Lamba builds downloaded from: http://bertram2.netbeans.org:8080/job/jdk8lambda/lastSuccessfulBuild/artifact/nbbuild/

import java.util.*;
import java.util.concurrent.Callable;

public class StackoverFlowQuery {

  public static void main(String[] args) throws Exception {

    HashMap<Integer, Callable<String>> opcode_only = 
          new HashMap<Integer, Callable<String>>() {
            {
              put(0, () -> {
                return "nop";
              });
              put(1, () -> {
                return "nothing....";
              });
            }
          };
    System.out.println(opcode_only.get(0).call());
  }

}
MohamedSanaulla
  • 6,112
  • 5
  • 28
  • 45
4

You are doing correct, update JDK library to 1.8 version from Java Build Path in Eclipse Project properties.

I just now tried the below code and it is working fine on my Eclipse:

        HashMap<Integer, Integer> hmLambda = new HashMap<Integer, Integer>() {
        {
            put(0, 1);
            put(1, 1);
        }
    };
    System.out.println(hmLambda.get(0));

    hmLambda.forEach((k, v) -> System.out.println("Key " + k
            + " and Values is: " + v));
Nitin Mahesh
  • 3,802
  • 3
  • 30
  • 30
0

As far as I know Netbeans 7.4 fully supports Java 8. I had Problems with eclipse (atm it's not supporting java8 so it's just able to compile the old Lambda expressions of 7), that's why i switched to Netbeans. If you've installed an earlier Version of Netbeans please make sure to FULLY uninstall it to make sure the newer one isn't able to refer to old Logfiles etc.

flumingo
  • 513
  • 2
  • 7
  • 24