java properties map_Java Properties配置文件读取到Map<String, List<String>>的方法(lambda)

Properties配置文件中内容如下:A=groupA1

A=groupA2

A=groupA3

B=groupB1

B=groupB2

1、通过Map和collect来实现File reqFile = new File("test.config");

try (Stream stream = Files.lines(reqFile.toPath())) {

Map> conf = stream

.map(s -> Arrays.asList(s.split("=")))

.collect(HashMap::new,

(map, item) -> map.computeIfAbsent(item.get(0), k -> new ArrayList<>()).add(item.get(1)),

HashMap::putAll);

for (Map.Entry> entry: conf.entrySet()) {

System.out.println("KEY: " + entry.getKey());

for (String value : entry.getValue()) {

System.out.println("VALUE: " + value);

}

}

} catch (Exception e) {

e.printStackTrace();

}

2、通过map和group by来实现File reqFile = new File("test.config");

try (Stream stream = Files.lines(reqFile.toPath())) {

Map> conf = stream

.map(s -> Arrays.asList(s.split("=")))

.collect(Collectors.groupingBy(s -> s.get(0), Collectors.mapping(v->v.get(1), Collectors.toList())));

for (Map.Entry> entry: conf.entrySet()) {

System.out.println("KEY: " + entry.getKey());

for (String value : entry.getValue()) {

System.out.println("VALUE: " + value);

}

}

} catch (Exception e) {

e.printStackTrace();

}

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页