Jerin Joseph
Jerin Joseph

Reputation: 797

Convert from a list of characters "List<Character>" to an array of chars "char[]"

What is a way in Java to convert from the List<Character> list to char array char[] chars.

I want this so that later I can create a String using String(chars). Do we have any shortcuts using Stream?

I see that we can convert from List<Integer> (Or Double and Long) to int[] (or double[] and long[]), but similar functionality is not available for char as we do not have a char equivalent for IntStream?

Please suggest if we have a quick way of doing this.

My current code is:

new String(getCharArray(charList))
private static char[] getCharArray(List<Character> charList) {
    char[] charArray = new char[charList.size()];
    for (int i = 0; i < charArray.length; i++) {
        charArray[i] = charList.get(i);
    }
    return charArray;
}

Upvotes: 1

Views: 2370

Answers (5)

Kaplan
Kaplan

Reputation: 3728

converting each Character to a String is incredible slow
therefore so

int[] tmp = list.stream().mapToInt( c -> c ).toArray();

convert to a String only once

String rslt = new String( tmp, 0, tmp.length );

if You need the char[] too

char[] array = rslt.toCharArray();

(what does someone need a List<Character> for?)

Upvotes: 1

user14940971
user14940971

Reputation:

There is no special Stream method for converting List<Character> to an array of primitives char[], but you can do it yourself with IntStream, for example:

List<Character> list = List.of('a', 'b', 'c');

char[] chars = new char[list.size()];

IntStream.range(0, list.size()).forEach(i -> chars[i] = list.get(i));

System.out.println(Arrays.toString(chars)); // [a, b, c]

Upvotes: 0

Michael Gantman
Michael Gantman

Reputation: 7790

You can do it without Streams. Look at the method toArray(T[] a) of class List. It does exactly what you want. All you will need to do is:

Character[] charArray = charList.toArray(new Character[charList.size()]);

Upvotes: -2

dreamcrash
dreamcrash

Reputation: 51443

TL;DR

You can do it (for instance) with:

list.stream().map(Object::toString).collect(Collectors.joining()).toCharArray();

However, if you want to have it as a String just do directly:

list.stream().map(Object::toString).collect(Collectors.joining());

Detailed Answer

I see that we can convert from List (...) but similar functionality is not available for char as we do not have a char equivalent for IntStream?

Yes, because you can combine the inbuilt methods mapToInt and toArray to get the conversion for free. And the same does not apply for the type char. Nonetheless, you can still use the generic map, convert to a String and then again to array of chars:

  list.stream().map(Object::toString).collect(Collectors.joining()).toCharArray();

I want this so that later I can create a String using String(chars). Do we have any shortcuts using Stream?

Why not then do directly?!:

String collect = list.stream()
                     .map(String::valueOf)
                     .collect(Collectors.joining());

Running example:

public static void main(String[] args) {
    List<Character> list = List.of('H', 'e','l','l','o');
    String collect = list.stream().map(String::valueOf).collect(Collectors.joining());
    System.out.println(collect);
}

Output

Hello

Upvotes: 4

Mohamed Anees A
Mohamed Anees A

Reputation: 4601

private static char[]  getCharArray(List<Character> charList) {
    return charList.stream()
        .map(String::valueOf)
        .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
        .toString()
        .toCharArray();
  }

Please do note this is constructing a String, which is backed by a char[] internally in Java. From the docs of toCharArray()

Converts this string to a new character array.

Returns: a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.

So, This will internally create two char[] (One for backing String) and toCharArray returns a new Array with same content.

Upvotes: 1

Related Questions