How to convert a String without separator to an ArrayList
.
My String is like this:
String str = \"abcd...\"
String myString = "xxx";
ArrayList<Character> myArrayList = myString.chars().mapToObj(x -> (char) x).collect(toCollection(ArrayList::new));
myArrayList.forEach(System.out::println);
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abcd...";
ArrayList<Character> a=new ArrayList<Character>();
for(int i=0;i<str.length();i++)
{
a.add(str.charAt(i));
}
System.out.println(a);
}
Sorry for the Retrobump, but this is now really easy!
You can do this easily in Java 8 using Streams! Try this:
String string = "testingString";
List<Character> list = string.chars().mapToObj((i) -> Character.valueOf((char)i)).collect(Collectors.toList());
System.out.println(list);
You need to use mapToObj
because chars()
returns an IntStream
.
use lambda expression to do this.
String big_data = "big-data";
ArrayList<Character> chars
= new ArrayList<>(
big_data.chars()
.mapToObj(e -> (char) e)
.collect(
Collectors.toList()
)
);
you can do it like this:
import java.util.ArrayList;
public class YourClass{
public static void main(String [] args){
ArrayList<Character> char = new ArrayList<Character>();
String str = "abcd...";
for (int x = 0; x < str.length(); x ++){
char.add(str.charAt(x));
}
}
}
You need to add it like this.
String str = "abcd...";
ArrayList<Character> chars = new ArrayList<Character>();
for (char c : str.toCharArray()) {
chars.add(c);
}