【问题标题】:Collect primitive array values into a collection将原始数组值收集到集合中
【发布时间】:2022-11-19 01:36:51
【问题描述】:

如何将原始值数组 intlongdouble 转换为类型为 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


    【解决方案1】:

    为了将原始值数组转换为集合;这些值需要是 boxed 到相应的对象类型,或者需要调用原始流的 collect 方法。

    拳击

    以下是 8 种原始类型及其对应的包装类:

    • boolBoolean
    • byteByte
    • charCharacter
    • doubleDouble
    • floatFloat
    • intInteger
    • longLong
    • shortShort

    也可以看看: https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

    在下面的代码中,int[] 被转换为 IntStream,然后装箱到 Stream&lt;Integer&gt; 中,并使用 Collectors.toMap 收集器进行收集。

    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> collectBoxed(int[] values) {
            return Arrays.stream(values)
                .boxed()
                .collect(
                    Collectors.toMap(
                        Function.identity(),
                        value -> words[value]));
        }
    
        public static void main(String[] args) {
            Map<Integer, String> boxedMap = collectBoxed(numbers);
            System.out.println(boxedMap);
        }
    }
    

    原始流

    Arrays.stream 方法已被覆盖,仅接受doubleintlong 的数组。

    • double[]DoubleStream
    • int[]IntStream
    • long[]LongStream

    在下面的代码中,直接在原始流对象上调用了collect方法。它需要三个参数:

    • supplier - 在本例中为HashMap&lt;Integer, String&gt;
    • accumulator - 如何将 iteree 应用于供应商
    • combiner - 如何组合多个值
    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> collectUnboxed(int[] values) {
            return Arrays.stream(values)
                .collect(
                    HashMap::new,
                    (acc, value) -> acc.put(value, words[value]),
                    HashMap::putAll);
        }
    
        public static void main(String[] args) {
            Map<Integer, String> unboxedMap = collectUnboxed(numbers);
            System.out.println(unboxedMap);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多