convert string to arraylist <Character> in java

核能气质少年 提交于 2020-01-10 00:36:38

问题


How to convert a String without separator to an ArrayList<Character>.

My String is like this:

String str = "abcd..."

I know one way of doing this is converting the String to char[] first, and then convert the char [] to ArrayList <Character>.

Is there any better way to do this? like converting directly? Considering time and performance, because I am coding with a big database.


回答1:


You need to add it like this.

String str = "abcd...";
ArrayList<Character> chars = new ArrayList<Character>();
for (char c : str.toCharArray()) {
  chars.add(c);
}



回答2:


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()
                )
        );    



回答3:


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.




回答4:


If you dn not need to modify list after it created, probably the better way would be to wrap string into class implementing List<Character> interface like this:

import java.util.AbstractList;
import java.util.List;

public class StringCharacterList extends AbstractList <Character>
{
    private final String string;

    public StringCharacterList (String string)
    {
        this.string = string;
    }

    @Override
    public Character get (int index)
    {
        return Character.valueOf (string.charAt (index));
    }

    @Override
    public int size ()
    {
        return string.length ();
    }
}

And then use this class like this:

List <Character> l = new StringCharacterList ("Hello, World!");
System.out.println (l);



回答5:


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);
    }



回答6:


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));
        }
    }
}



回答7:


String myString = "xxx";
ArrayList<Character> myArrayList = myString.chars().mapToObj(x -> (char) x).collect(toCollection(ArrayList::new));
myArrayList.forEach(System.out::println);


来源:https://stackoverflow.com/questions/15331637/convert-string-to-arraylist-character-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!