我有 2 个字符串数组。如何一次遍历两个数组以创建一个 HashMap,其中 Key 来自第一个数组,值来自第二个数组。
例如。Array1 = {“A”、“B”、“C”、“D”}
Array2 = {“苹果”、“男孩”、“猫”、“狗”}
结果 HashMap = [{A:apple}, {B:boy}, {C:cat}, {D:dog}]
这是我的代码,但它不起作用。
AtomicInteger index = new AtomicInteger();
Stream<String> stream = Stream.of(array2);
stream.forEach(x -> mappedData.put(array1[index.getAndIncrement()],x));