把数据按照Map<String,Map<String,List<String>>>的形式分类(hashbasedtable_Guava类库学习--Table(双键的Map))

这段代码展示了如何使用Map来存储学生的数据,通过键值对key1和key2将学生对象组织到对应的List中。当遇到新的学生时,首先检查对应key1的Map是否存在,如果不存在则创建新的Map和List,然后将学生添加到List中;如果存在,则检查key2对应的List,若为空则初始化List并添加学生,否则直接将学生添加到List。
Map<String,Map<String,List<Student>>> mapKey1Key2 = new HashMap<>();

for(Student student : list){
String key1 = student.getKey1()
String key2 = student.getKey2()
Map<String,List<Student>> mapKey2 = mapKey1Key2 .get(key1)
if(CollUtil.isEmpty(mapKey2)){
	mapKey2 = new HashMap<>();
	List<Student> listSamekey2 = new ArrayList<>();
	listSamekey2 .add(student);
	mapKey2.put(key2,listSamekey2);
	mapKey1Key2.put(key1,mapKey2); 
} else {
	List<Student> listSamekey2 = mapKey2.get(key2);
	if(CollUtil.isEmpty(listSamekey2)){
		listSamekey2 = new new ArrayList<>();
		mapKey2.put(key2, listSamekey2);
	}
	listSamekey2.add(student);
}
}

Java 中,将 `Map<String, Object>` 转换为 `Map<String, String>` 通常涉及 **遍历原 Map 并转换每个 `Object` 值为 `String`**。以下是几种常见方法: --- ## **方法 1:遍历转换(Java 8 之前)** ```java Map<String, Object> originalMap = new HashMap<>(); // 假设 originalMap 已填充数据 Map<String, String> stringMap = new HashMap<>(); for (Map.Entry<String, Object> entry : originalMap.entrySet()) { String key = entry.getKey(); String value = entry.getValue() != null ? entry.getValue().toString() : null; stringMap.put(key, value); } ``` **适用场景**:兼容所有 Java 版本,逻辑清晰。 --- ## **方法 2:Java 8 Stream API** ```java Map<String, String> stringMap = originalMap.entrySet() .stream() .collect(Collectors.toMap( Map.Entry::getKey, entry -> entry.getValue() != null ? entry.getValue().toString() : null )); ``` **优点**:代码更简洁,适合函数式编程风格。 --- ## **方法 3:Guava 库(`com.google.common.collect.Maps.transformValues`)** ```java import com.google.common.collect.Maps; Map<String, String> stringMap = Maps.transformValues( originalMap, value -> value != null ? value.toString() : null ); ``` **适用场景**:如果项目已引入 Guava,推荐使用。 --- ## **注意事项** 1. **`null` 值处理**: - 如果 `Object` 为 `null`,直接调用 `toString()` 会抛 `NullPointerException`,需判空。 - 示例:`value != null ? value.toString() : null` 或 `String.valueOf(value)`(自动处理 `null`)。 2. **特殊类型转换**: - 如果 `Object` 是 `Integer`、`Boolean` 等,`toString()` 会返回 `"123"`、`"true"` 等字符串形式- 如需自定义转换逻辑(如日期格式化),需额外处理: ```java .map(entry -> { Object value = entry.getValue(); if (value instanceof Date) { return new SimpleDateFormat("yyyy-MM-dd").format((Date) value); } return value.toString(); }) ``` 3. **`Collectors.toMap` 的冲突处理**: - 如果 `originalMap` 有重复 `key`,`Collectors.toMap` 会抛 `IllegalStateException`,可指定合并策略: ```java .collect(Collectors.toMap( Map.Entry::getKey, entry -> entry.getValue().toString(), (oldVal, newVal) -> newVal) // 保留新值(或自定义合并逻辑) ); ``` --- ## **完整示例(Java 8 Stream + 异常处理)** ```java Map<String, Object> originalMap = Map.of( "name", "Alice", "age", 30, "isStudent", true, "birthday", LocalDate.of(1990, 1, 1), "nullableKey", null ); Map<String, String> stringMap = originalMap.entrySet() .stream() .collect(Collectors.toMap( Map.Entry::getKey, entry -> entry.getValue() != null ? entry.getValue().toString() : "NULL", (oldVal, newVal) -> newVal // 处理重复键 )); System.out.println(stringMap); ``` **输出**: ``` {nullableKey=NULL, name=Alice, age=30, isStudent=true, birthday=1990-01-01} ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值