从一个IntStream生成一个Map [英] Produce a Map from an IntStream of keys
问题描述
我知道我可以初始化 地图
,然后使用流对其进行填充。例如,在这里我使用 IntStream
随机数来填充预定义地图的键。
I know I can initialize a Map
, and then fill it using a stream. For example, here I use an IntStream
of random numbers to populate the key of the predefined map.
int initialCapacity = 3;
Map < Integer, UUID > map = new HashMap <>( initialCapacity );
IntStream numbers = ThreadLocalRandom.current().ints( initialCapacity );
numbers.forEach( number -> map.put( number , UUID.randomUUID() ) );
转储到控制台。
System.out.println( "map = " + map );
map = {-14054365 = ee739423-1200-45e6-80da- d167ce2e2b98,-1079671217 = ba0096fe-b32f-4ebf-a163-114fcb679992,-404094411 = f900052b-8a8d-4e66-b808-618fbc7e115f}
map = {-14054365=ee739423-1200-45e6-80da-d167ce2e2b98, -1079671217=ba0096fe-b32f-4ebf-a163-114fcb679992, -404094411=f900052b-8a8d-4e66-b808-618fbc7e115f}
➥我想知道是否有一种方法可以让流产生 Map
对象,而不是填充先前存在的地图。
➥ I wonder if there is a way to have the stream produce the Map
object, rather than filling a pre-existing map.
我不在乎这是否明智。我只是对语法以及如何使用 收藏家
。
I do not care about if this is a wise approach or not. I am just curious about the syntax and how to use a Collector
.
我了解 Collectors.toMap
方法。但是我似乎无法正确完成语法或语义。我猜想 Function.identity()
是使用 IntStream
产生的每个数字作为键的正确方法。然后我尝试了 UUID.randomUUID()
或 p-> UUID.randomUUID()
生成每个地图条目的值。
I know about the Collectors.toMap
methods. But I cannot seem to get the syntax or semantics done properly. I am guessing that Function.identity()
is the right way to use each number produced by our IntStream
as the key. Then I tried UUID.randomUUID()
or p -> UUID.randomUUID()
to generate the value for each map entry.
int initialCapacity = 3;
Map < Integer, UUID > map =
ThreadLocalRandom
.current() // Returns a random number generator.
.ints( initialCapacity ) // Returns an `IntStream`.
.collect(
Collectors.toMap(
Function.identity() , // Generate map key (?)
x -> UUID.randomUUID() // Generate map value (?)
)
);
但是我在IDE中遇到一个错误,说:
But I get an error in the IDE saying:
期望3个参数,但发现1
Expected 3 arguments but found 1
我找到了的示例具有两个参数的Collectors.toMap
,一个用于键,一个用于值,以产生每个地图项。因此,我不明白为什么要坚持方法调用的第三个参数。
I found examples of Collectors.toMap
with two arguments, one for the key, and one for the value, to produce each map entry. So I do not see why the insistence on a third argument to the method call.
推荐答案
IntStream
仅具有一个 collect
方法,该方法需要3个参数。我怀疑您正在寻找 Stream
中的1参数重载。
IntStream
only has one collect
method, which takes 3 parameters. I suspect you are looking for the 1-parameter overload in Stream
.
了解该 IntStream
生成原始 int
数字流,而不是 Integer
对象流。
Understand that IntStream
produces a stream of primitive int
numbers, not Integer
objects.
调用 .boxed()
调用自动装箱(请参见教程)将每个基本 int
包装为 Integer
目的。这将使 Stream< Integer>
而不是 IntStream
。
Calling .boxed()
invokes auto-boxing (see tutorial) to wrap each primitive int
as an Integer
object. This makes a Stream<Integer>
rather than IntStream
.
Map < Integer, UUID > map =
ThreadLocalRandom
.current()
.ints( initialCapacity )
.boxed() // Add this call
.collect(
Collectors.toMap(
Function.identity() ,
x -> UUID.randomUUID()
)
);
这篇关于从一个IntStream生成一个Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!