Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert IntStream into List<Character>?

I have an IntStream object that I have limited to 30 elements and I like to get a List<Character> from all the int elements in the stream.

Thanks in advance

like image 749
Anthony Avatar asked Sep 04 '25 02:09

Anthony


2 Answers

You may map each int to a char then collect into a list, this will result in a list of character given by their ASCII code

List<Character> r = IntStream.range(50, 80).mapToObj(a -> (char) a).collect(Collectors.toList());
System.out.println(r); // [2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >, ?, @, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O]
like image 192
azro Avatar answered Sep 07 '25 00:09

azro


in this way you can do this action.

IntStream intStream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
List<Character> characterList = intStream.mapToObj(i -> (char) i).collect(Collectors.toList());

for all streams you can do this via mapping each item to a character and collect them.

like image 28
Seyyed Mahdiyar Zerehpoush Avatar answered Sep 07 '25 00:09

Seyyed Mahdiyar Zerehpoush