如何在不在中间操作中创建条目的情况下将 csv 转换为具有 java 流的 HashMap?
How to convert a csv into HashMap with java streams without creating entry in intermediate operation?
我正在编写 junit
测试,我想从 csv 文件导入预期结果,作为 HashMap
.
以下工作,但我发现我首先创建一个 MapEntry.entry()
样板,然后我将其收集到一个新的 HashMap
.
csv:
#key;amount
key1;val1
key2;val2
...
keyN;valN
测试:
Map<String, BigDecimal> expected = Files.readAllLines(
Paths.get("test.csv"))
.stream()
.map(line -> MapEntry.entry(line.split(",")[0], line.split(",")[1]))
.collect(Collectors.toMap(Map.Entry::getKey, item -> new BigDecimal(item.getValue())));
特别是我正在寻找这样的 oneliner 解决方案。我的意思是:我可以避免在再次将它收集到哈希映射之前必须创建一个显式的 MapEntry.entry
吗?
这可以做得更好吗?或者是否有任何已经可以读取 csv 的 junit 实用程序?
不需要创建条目,可以使用map
函数将行拆分为数组,然后使用Collectors.toMap
Map<String, BigDecimal> expected = Files.readAllLines(
Paths.get("test.csv"))
.stream()
.map(line->line.split(","))
.filter(line->line.length>1)
.collect(Collectors.toMap(key->key[0], value -> new BigDecimal(value[1])));
如果您想将条目收集到特定类型中,您可以使用重载 Collectors.toMap 和 mapSupplier
Returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements.
HashMap<String, BigDecimal> expected = Files.readAllLines(
Paths.get("test.csv"))
.stream()
.map(line->line.split(","))
.filter(line->line.length>1)
.collect(Collectors.toMap(key->key[0], value -> new BigDecimal(value[1]),(val1,val2)->val1, HashMap::new));
}
这对我有用:
Map<String, BigDecimal> result = Files.readAllLines(Paths.get("test.csv"))
.stream()
.collect(Collectors.toMap(l -> l.split(",")[0], l -> new BigDecimal(l.split(",")[1])));
我正在编写 junit
测试,我想从 csv 文件导入预期结果,作为 HashMap
.
以下工作,但我发现我首先创建一个 MapEntry.entry()
样板,然后我将其收集到一个新的 HashMap
.
csv:
#key;amount
key1;val1
key2;val2
...
keyN;valN
测试:
Map<String, BigDecimal> expected = Files.readAllLines(
Paths.get("test.csv"))
.stream()
.map(line -> MapEntry.entry(line.split(",")[0], line.split(",")[1]))
.collect(Collectors.toMap(Map.Entry::getKey, item -> new BigDecimal(item.getValue())));
特别是我正在寻找这样的 oneliner 解决方案。我的意思是:我可以避免在再次将它收集到哈希映射之前必须创建一个显式的 MapEntry.entry
吗?
这可以做得更好吗?或者是否有任何已经可以读取 csv 的 junit 实用程序?
不需要创建条目,可以使用map
函数将行拆分为数组,然后使用Collectors.toMap
Map<String, BigDecimal> expected = Files.readAllLines(
Paths.get("test.csv"))
.stream()
.map(line->line.split(","))
.filter(line->line.length>1)
.collect(Collectors.toMap(key->key[0], value -> new BigDecimal(value[1])));
如果您想将条目收集到特定类型中,您可以使用重载 Collectors.toMap 和 mapSupplier
Returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements.
HashMap<String, BigDecimal> expected = Files.readAllLines(
Paths.get("test.csv"))
.stream()
.map(line->line.split(","))
.filter(line->line.length>1)
.collect(Collectors.toMap(key->key[0], value -> new BigDecimal(value[1]),(val1,val2)->val1, HashMap::new));
}
这对我有用:
Map<String, BigDecimal> result = Files.readAllLines(Paths.get("test.csv"))
.stream()
.collect(Collectors.toMap(l -> l.split(",")[0], l -> new BigDecimal(l.split(",")[1])));