【发布时间】:2022-11-19 01:36:51
【问题描述】:
如何将原始值数组 int、long 或 double 转换为类型为 Map 的集合?
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class PrimitiveCollection {
private static final String[] words = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};
private static final int[] numbers = { 8, 6, 7, 5, 3, 0, 9 };
public static Map<Integer, String> collect(int[] values) {
return // Collect the array of values into a Map<Integer, String>
}
public static void main(String[] args) {
Map<Integer, String> map = collect(numbers);
System.out.println(map); // {0=zero, 3=three, 5=five, 6=six, 7=seven, 8=eight, 9=nine}
}
}
【问题讨论】:
标签: java collections hashmap java-stream primitive