How can I create a stream from an array?

后端 未结 6 2281
挽巷
挽巷 2020-11-27 03:18

Currently whenever I need to create stream from an array, I do

String[] array = {\"x1\", \"x2\"};
Arrays.asList(array).stream();

Is there s

6条回答
  •  情深已故
    2020-11-27 03:53

    You can use Arrays.stream :

    Arrays.stream(array); 

    This ensures the return type of steam based on your array input type if its String [] then return Stream, if int [] then returns IntStream

    When you already know input type array then good to use specific one like for input type int[]

     IntStream.of(array); 

    This returns Intstream.

    In first example, Java uses method overloading to find specific method based on input types while as in second you already know the input type and calling specific method.

提交回复
热议问题