Convert String to List Java

This post will discuss how to convert a string to a list of characters in Java.

1. Naive solution

A naive solution is to create a new list and add elements to it using a for-each loop, as shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.ArrayList;
import java.util.List;
// Convert a string to a list of characters
class Main
{
public static void main[String[] args]
{
String string = "Techie Delight";
List chars = new ArrayList[];
for [char ch: string.toCharArray[]] {
chars.add[ch];
}
System.out.println[chars];
}
}

DownloadRun Code

Output:

[T, e, c, h, i, e, , D, e, l, i, g, h, t]


We can also use a simple for-loop, as shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.ArrayList;
import java.util.List;
// Convert a string to a list of characters
class Main
{
public static void main[String[] args]
{
String string = "Techie Delight";
List chars = new ArrayList[];
for [int i = 0; i [char]e] // Stream
.collect[Collectors.toList[]];
System.out.println[chars];
}
}

DownloadRun Code

Output:

[T, e, c, h, i, e, , D, e, l, i, g, h, t]


We can also use IntStream.range[] to directly access each character and add it to the list, as shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
// Convert a string to a list of characters
class Main
{
public static void main[String[] args]
{
String string = "Techie Delight";
List chars = IntStream.range[0, string.length[]]
.mapToObj[string::charAt]
.collect[Collectors.toList[]];
System.out.println[chars];
}
}

DownloadRun Code

Output:

[T, e, c, h, i, e, , D, e, l, i, g, h, t]

3. Using AbstractList Interface

To create an immutable list backed by the string, we can implement the AbstractList interface.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.AbstractList;
import java.util.List;
// Convert a string to a list of characters
class Main
{
public static List convert[String string]
{
return new AbstractList[] {
@Override
public Character get[int index] {
return string.charAt[index];
}
@Override
public int size[] {
return string.length[];
}
};
}
public static void main[String[] args]
{
String string = "Techie Delight";
System.out.println[convert[string]];
}
}

DownloadRun Code

Output:

[T, e, c, h, i, e, , D, e, l, i, g, h, t]

4. Using Guava Library

Another plausible way of converting a string to a list of Character is using some third-party library. Guavas Chars class provides several static utility methods pertaining to char primitives. One such method is asList[], which returns a fixed-size list backed by the array of char primitives.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import com.google.common.primitives.Chars;
import java.util.List;
// Convert a string to a list of characters
class Main
{
public static void main[String[] args]
{
String string = "Techie Delight";
List chars = Chars.asList[string.toCharArray[]];
System.out.println[chars];
}
}

Download Code

Output:

[T, e, c, h, i, e, , D, e, l, i, g, h, t]


We can also use the Guava library Lists class, which provides static utility methods pertaining to List instances. It has the charactersOf[] method that returns a view of the specified string as an immutable list of Character values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import com.google.common.collect.Lists;
import java.util.List;
// Convert a string to a list of characters
class Main
{
public static void main[String[] args]
{
String string = "Techie Delight";
List chars = Lists.charactersOf[string];
System.out.println[chars];
}
}

Download Code

Output:

[T, e, c, h, i, e, , D, e, l, i, g, h, t]

Thats all about converting a String to a List of Character in Java.

Video liên quan

Chủ Đề