如何将字符串列表中的数据转换为 Map使用流 API?
我尝试使用 Stream API 将字符串列表中的数据转换为 Map
但我这样做的方式不正确:
public static Map<String, Integer> converter (List<String> data){
return data.stream().collect(Collectors.toMap(e->e, Function.identity()));
如果我们有 List.of("5", "7", "5", "8", "9", "8")
任务 1 作为键 - 字符串
,值 - 字符频率
任务 2 Strong> 作为键 - String
和值 - 字符串转换为整数
I tried to convert data from the list of strings into a Map<String, Integer>
with the Stream API.
But the way I did it is not correct:
public static Map<String, Integer> converter (List<String> data){
return data.stream().collect(Collectors.toMap(e->e, Function.identity()));
If we have List.of("5", "7", "5", "8", "9", "8")
task №1 as key - String
, and value - character frequency
task №2 as key - String
, and value - string converted to integer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的频率。请注意,映射不能有重复的键。
key
是String
。值
以1
开头。Integers::sum
为每次出现的情况添加1
。对于第二个任务,将字符串
转换为ints很简单。但请记住,重复项不可能存在,因此必须正确合并它们。这与第一个任务类似,只是我们不进行计数,而是将其转换为整数。由于您有重复的整数,因此您需要提供有关操作的说明。这就是
merge
函数(a,b)->a
,它表示现有值a
和新值b,只需保留
a
打印
Try it like this for a frequency. Note that a map can't have duplicate keys.
key
is theString
.value
starts a1
.Integers::sum
adds1
for every occurrence.prints
for the second task, to convert the strings to
ints
is trivial. But remember that dups can't exist so they have to be properly merged. This is similar to the first task except instead of counting we're just converting to an integer. Since you have duplicate integers, you need to provide instructions on what to do. That is themerge
function(a,b)->a
which says for existing valuea
and new valueb
, just keepa
prints
以获取每个字符的频率 ,您需要将 flatten 在列表中的每个字符串(IE分开 string targue )。
由于 java 9 我们可以使用方法
FlatMaptoint(String :: Chars)
将产生intstream
,其中包含来自给定列表中每个字符串的字符。然后
maptoobj()
为每个流元素创建一个单字符串。要生成频率地图,我们可以使用collector
collector.groupingby()
。function.Identity()
用作第一个参数等效于e-&gt; e
,这只是一种说出流元素将用作键而无需任何更改的方法。第二个参数是a downstream collectorcollector.counting()
,它生成映射到特定键的元素数。您可以使用 collector collector < /em>
collectors.tomap()
期望三个参数:虽然,以下代码中的 MergeFunction 没有做任何特殊的事情,需要提供它,否则第一次遇到的重复将导致
illegalstateException
。main()
- 演示输出
In order to obtain the frequency of every character, you need to flatten each string in a list (i.e. split every string into characters).
Since Java 9 we can use method
chars()
which returns a stream of characters represented withint
primitives.flatMapToInt(String::chars)
will produce anIntStream
, containing characters from every string in the given list.Then
mapToObj()
creates a single-character string for every stream element.To generate the map of frequencies, we can utilize collector
Collectors.groupingBy()
.Function.identity()
is used as the first argument is an equivalent ofe -> e
, it's just a way to tell that stream elements will be used as a key without any changes. The second argument is a downstream collectorCollectors.counting()
, that generates the number of elements mapped to a particular key.You can do it by using a flavor of collector
Collectors.toMap()
that expects three arguments:Although, the mergeFunction in the code below is not doing anything special, it needs to be provided, otherwise the first encountered duplicate will cause an
IllegalStateException
.main()
- demoOutput