我需要对每个数组元素进行平方,并将此值作为键插入哈希映射中,值为true。我已经尝试过这样做,但我无法解决问题。
int [] array = {3, 1, 4, 6, 5};
HashMap<Integer, Boolean> map = IntStream.of(array)
.map(x -> x*x)
.collect(Collectors.toMap(p -> Integer.valueOf(p), Boolean.valueOf(true)));
boxed()
步骤是不必要的。此外,Boolean.valueOf(true)
已经过时:Arrays.stream(array).mapToObj(x -> x*x) .collect(Collectors.toMap(p -> p, p -> true))
- Holger