List<Map<String, String>>最简单的遍历方式
使用增强for循环(最常用)
List<Map> listOfMaps = getData(); // 获取数据for (Map map : listOfMaps) { for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); System.out.println(key + \": \" + value); } System.out.println(\"---\"); // 分隔每个Map}
使用传统的for循环
for (int i = 0; i < listOfMaps.size(); i++) { Map map = listOfMaps.get(i); System.out.println(\"第 \" + (i + 1) + \" 个Map:\"); Iterator<Map.Entry> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = iterator.next(); System.out.println(entry.getKey() + \" = \" + entry.getValue()); }}
使用forEach和Lambda表达式
listOfMaps.forEach(map -> { map.forEach((key, value) -> { System.out.println(key + \" -> \" + value); }); System.out.println(\"----------\");});
使用Stream API
listOfMaps.stream() .forEach(map -> { map.entrySet().stream() .forEach(entry -> { System.out.println(entry.getKey() + \": \" + entry.getValue()); }); });
使用AtomicInteger(线程安全)
AtomicInteger index = new AtomicInteger(1);listOfMaps.forEach(map -> { System.out.println(\"Map \" + index.getAndIncrement() + \":\"); map.forEach((key, value) -> System.out.println(\" \" + key + \": \" + value));});
使用IntStream
IntStream.range(0, listOfMaps.size()) .forEach(i -> { Map map = listOfMaps.get(i); System.out.println(\"索引 \" + i + \":\"); map.forEach((key, value) -> System.out.println(\" \" + key + \" = \" + value)); });
过滤特定键值对
listOfMaps.forEach(map -> { map.entrySet().stream() .filter(entry -> entry.getKey().startsWith(\"user_\")) // 过滤key .filter(entry -> !entry.getValue().isEmpty()) // 过滤空值 .forEach(entry -> System.out.println(entry.getKey() + \": \" + entry.getValue()));});
查找特定数据
List foundValues = listOfMaps.stream() .flatMap(map -> map.values().stream()) // 扁平化所有值 .filter(value -> value.contains(\"特定内容\")) .collect(Collectors.toList());
转换为其他数据结构
// 转换为List(所有值)List allValues = listOfMaps.stream() .flatMap(map -> map.values().stream()) .collect(Collectors.toList());// 转换为Set(所有键)Set allKeys = listOfMaps.stream() .flatMap(map -> map.keySet().stream()) .collect(Collectors.toSet());// 转换为单个Map(注意键冲突)Map combinedMap = listOfMaps.stream() .flatMap(map -> map.entrySet().stream()) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue // 处理键冲突 ));
处理数据库查询结果
// 模拟数据库返回的List<Map>List<Map> queryResults = getQueryResults();queryResults.forEach(row -> { String id = row.get(\"id\"); String name = row.get(\"name\"); String email = row.get(\"email\"); System.out.println(\"用户ID: \" + id + \", 姓名: \" + name + \", 邮箱: \" + email);});
处理JSON数据
List<Map> jsonData = parseJsonData();// 提取特定字段并处理jsonData.stream() .filter(item -> \"active\".equals(item.get(\"status\"))) .forEach(item -> { String username = item.get(\"username\"); String role = item.get(\"role\"); sendNotification(username, role); });
数据统计和分析
// 统计每个键的出现次数Map keyStatistics = listOfMaps.stream() .flatMap(map -> map.keySet().stream()) .collect(Collectors.groupingBy(key -> key, Collectors.counting()));// 统计值的分布Map valueDistribution = listOfMaps.stream() .flatMap(map -> map.values().stream()) .collect(Collectors.groupingBy(value -> value, Collectors.counting()));
大数据量时的优化
// 使用并行流处理大数据量(注意线程安全)listOfMaps.parallelStream() .forEach(map -> { // 处理每个Map,确保操作是线程安全的 processMap(map); });// 分批处理int batchSize = 1000;for (int i = 0; i < listOfMaps.size(); i += batchSize) { List<Map> batch = listOfMaps.subList(i, Math.min(i + batchSize, listOfMaps.size())); processBatch(batch);}
内存优化
// 使用迭代器避免创建临时对象Iterator<Map> listIterator = listOfMaps.iterator();while (listIterator.hasNext()) { Map map = listIterator.next(); Iterator<Map.Entry> mapIterator = map.entrySet().iterator(); while (mapIterator.hasNext()) { Map.Entry entry = mapIterator.next(); // 处理每个键值对 }}
完整工具类示例
public class MapListUtils { /** * 遍历List
总结
选择遍历方法时的考虑因素:
-
简单性:增强for循环最易读
-
功能性:Stream API提供丰富的操作
-
性能:大数据量时考虑并行流或分批处理
-
可维护性:复杂的处理逻辑可以封装到工具类中
根据具体需求选择最适合的遍历方式。