sparsh610
sparsh610

Reputation: 1601

Collection : Undefined k,v in Map

i am learning about collection from this site

Where the guy gave the example

import java.util.HashMap;
import java.util.Map;

public class MapTester {

public static void main(String[] args) {

// keys are Strings
// objects are also Strings
Map<String, String> map = new HashMap<>();
fillData(map);

// write to command line
map.forEach((k, v) -> System.out.printf("%s %s%n", k, v));

// add and remove from the map
map.put("iPhone", "Created by Apple");
map.remove("Android");

// write again to command line
map.forEach((k, v) -> System.out.printf("%s %s%n", k, v));

}

private static void fillData(Map<String, String> map) {
map.put("Android", "Mobile");
map.put("Eclipse IDE", "Java");
map.put("Eclipse RCP", "Java");
map.put("Git", "Version control system");

}

} 

although in above program i can understand that k and v variable are undefined or not present locally.

map.forEach((k, v) -> System.out.printf("%s %s%n", k, v));

and facing the issue with same line.

Please help me learning the collections. enter image description here enter image description here

Upvotes: 1

Views: 187

Answers (3)

A.B.
A.B.

Reputation: 470

Correct the project properties Java Compiler:

Doesn't work:

enter image description here

Works:

enter image description here

Upvotes: 3

sparsh610
sparsh610

Reputation: 1601

Answer of this question

You need to be using 4.4 (Luna) to get Java 8 support.

Might be useful for someone who checked it later.

Thanks @greg-449

Upvotes: 0

Dilip Kumar
Dilip Kumar

Reputation: 2534

It is working fine on my system. Can you check are you using JDK8? Lamdas support introduced in JAVA 8. You can test java version by using java --version.

Upvotes: 0

Related Questions