Array List Stream Conversion

Solstice
1 min readJan 18, 2020

--

Three things to remember for the conversion:

  • Stream acts as the center of the conversion.
  • int[] should first be converted to IntStream, which is not a subclass of Stream.
  • String[] and List<String> could be converted directly without the help of Stream.

Please refer to the number for the conversion code below

1. IntStream intStream = Arrays.stream(nums);
2. int[] numsReversed = intStream.toArray();
3. Stream<Integer> integerStream= intStream.boxed();
4. IntStream intStreamReverse = integerStream.mapToInt(i -> i); //Integer::intValue
5. List<Integer> integerList = integerStream.collect(Collectors.toList());
6. Stream<Integer> integerStreamRevered = integerList.stream();
String[] stringsArr = {"a", "b"};
7. Stream<String> stringStream = Arrays.stream(stringsArr);
8. String[] stringsArrReverse = stringStream.toArray(String[]::new);
9. List<String> stringList = Arrays.asList(stringsArr);
10. String[] StringsArrReverseFromList = stringList.toArray(new String[0]);

--

--

Solstice
Solstice

No responses yet