Map<String, String> map = new HashMap() {{
put("map1", "value1");
put("map2", "value2");
put("map3", "value3");
}};
map.forEach((k, v) -> {
System.out.println("key:" + k + " value:" + v);
});
Map<String, String> map = new HashMap();
map.put("map1", "value1");
map.put("map2", "value2");
map.put("map3", "value3");
map.forEach((k, v) -> {
System.out.println("key:" + k + " value:" + v);
});
key:map3value:value3
key:map2value:value2
key:map1value:value1
Map<String, String> map = new HashMap() {{
put("map1", "value1");
put("map2", "value2");
put("map3", "value3");
}};
import java.util.HashMap;
class DoubleBracket$1 extends HashMap {
DoubleBracket$1(DoubleBracket var1) {
this.this$0 = var1;
this.put("map1", "value1");
this.put("map2", "value2");
}
}
public class DoubleBracket {
private static String userName = "磊哥";
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Map<String, String> map = new HashMap() {{
put("map1", "value1");
put("map2", "value2");
put("map3", "value3");
put(userName, userName);
}};
}
}
javap -c DoubleBracket\$1.class
Compiled from "DoubleBracket.java"
class com.example.DoubleBracket$1 extends java.util.HashMap {
final com.example.DoubleBracket this$0;
com.example.DoubleBracket$1(com.example.DoubleBracket);
Code:
0: aload_0
1: aload_1
2: putfield #1 // Field this$0:Lcom/example/DoubleBracket;
5: aload_0
6: invokespecial #7 // Method java/util/HashMap."<init>":()V
9: aload_0
10: ldc #13 // String map1
12: ldc #15 // String value1
14: invokevirtual #17 // Method put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
17: pop
18: aload_0
19: ldc #21 // String map2
21: ldc #23 // String value2
23: invokevirtual #17 // Method put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
26: pop
27: return
}
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class DoubleBracket {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Map map = new DoubleBracket().createMap();
// 获取一个类的所有字段
Field field = map.getClass().getDeclaredField("this$0");
// 设置允许方法私有的 private 修饰的变量
field.setAccessible(true);
System.out.println(field.get(map).getClass());
}
public Map createMap() {
// 双花括号初始化
Map map = new HashMap() {{
put("map1", "value1");
put("map2", "value2");
put("map3", "value3");
}};
return map;
}
}
public void createMap() {
Map map = new HashMap() {{
put("map1", "value1");
put("map2", "value2");
put("map3", "value3");
}};
// 业务处理....
}
public Map createMap() {
Map map = new HashMap() {{
put("map1", "value1");
put("map2", "value2");
put("map3", "value3");
}};
return map;
}
public static Map createMap() {
Map map = new HashMap() {{
put("map1", "value1");
put("map2", "value2");
put("map3", "value3");
}};
return map;
}
javap -c DoubleBracket\$1.class
Compiled from "DoubleBracket.java"
class com.example.DoubleBracket$1 extends java.util.HashMap {
com.example.DoubleBracket$1();
Code:
0: aload_0
1: invokespecial #1 // Method java/util/HashMap."<init>":()V
4: aload_0
5: ldc #7 // String map1
7: ldc #9 // String value1
9: invokevirtual #11 // Method put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
12: pop
13: aload_0
14: ldc #17 // String map2
16: ldc #19 // String value2
18: invokevirtual #11 // Method put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
21: pop
22: aload_0
23: ldc #21 // String map3
25: ldc #23 // String value3
27: invokevirtual #11 // Method put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
30: pop
31: return
}
List<String> list = new ArrayList() {{
add("Java");
add("Redis");
}};
List<String> list = Stream.of("Java", "Redis").collect(Collectors.toList());
Map map = new HashMap() {{
put("map1", "value1");
put("map2", "value2");
}};
Map map = Map.of("map1", "Java", "map2", "Redis");
更多精彩推荐
☞一文带你认清数据仓库“维度模型设计”与“分层架构” | 原力计划
☞5 月编程语言排行榜:C 重回第一,今年编程语言名人堂冠军还会是它吗?| 原力计划
你点的每个“在看”,我都认真当成了喜欢