public static final Map<String, String> bathScheme = new HashMap<>(); 初始化写法
时间: 2023-11-18 13:30:41 浏览: 122
java 读取csv 并转为Map<String,Object>
初始化写法有很多种,以下是几种常见的方法:
1. 使用静态代码块进行初始化:
```
public static final Map<String, String> bathScheme;
static {
bathScheme = new HashMap<>();
bathScheme.put("key1", "value1");
bathScheme.put("key2", "value2");
// 以此类推
}
```
2. 使用Collections.unmodifiableMap()方法进行初始化:
```
public static final Map<String, String> bathScheme = Collections.unmodifiableMap(new HashMap<String, String>() {{
put("key1", "value1");
put("key2", "value2");
// 以此类推
}});
```
3. 使用Map.of()方法进行初始化(需要Java 9及以上版本):
```
public static final Map<String, String> bathScheme = Map.of("key1", "value1", "key2", "value2");
```
阅读全文