简体   繁体   中英

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. 在此输入图像描述在此输入图像描述

Correct the project properties Java Compiler :

Doesn't work:

在此输入图像描述

Works:

在此输入图像描述

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 .

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

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