How to convert a String into an ArrayList?

前端 未结 13 605
囚心锁ツ
囚心锁ツ 2020-11-28 04:32

In my String, I can have an arbitrary number of words which are comma separated. I wanted each word added into an ArrayList. E.g.:

String s = \"a,b,c,d,e,...         


        
相关标签:
13条回答
  • 2020-11-28 04:44

    You could use:

    List<String> tokens = Arrays.stream(s.split("\\s+")).collect(Collectors.toList());
    

    You should ask yourself if you really need the ArrayList in the first place. Very often, you're going to filter the list based on additional criteria, for which a Stream is perfect. You may want a set; you may want to filter them by means of another regular expression, etc. Java 8 provides this very useful extension, by the way, which will work on any CharSequence: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#splitAsStream-java.lang.CharSequence-. Since you don't need the array at all, avoid creating it thus:

    // This will presumably be a static final field somewhere.
    Pattern splitter = Pattern.compile("\\s+");
    // ...
    String untokenized = reader.readLine();
    Stream<String> tokens = splitter.splitAsStream(untokenized);
    
    0 讨论(0)
  • 2020-11-28 04:47

    If you want to convert a string into a ArrayList try this:

    public ArrayList<Character> convertStringToArraylist(String str) {
        ArrayList<Character> charList = new ArrayList<Character>();      
        for(int i = 0; i<str.length();i++){
            charList.add(str.charAt(i));
        }
        return charList;
    }
    

    But i see a string array in your example, so if you wanted to convert a string array into ArrayList use this:

    public static ArrayList<String> convertStringArrayToArraylist(String[] strArr){
        ArrayList<String> stringList = new ArrayList<String>();
        for (String s : strArr) {
            stringList.add(s);
        }
        return stringList;
    }
    
    0 讨论(0)
  • 2020-11-28 04:49

    Option1:

    List<String> list = Arrays.asList("hello");
    

    Option2:

    List<String> list = new ArrayList<String>(Arrays.asList("hello"));
    

    In my opinion, Option1 is better because

    1. we can reduce the number of ArrayList objects being created from 2 to 1. asList method creates and returns an ArrayList Object.
    2. its performance is much better (but it returns a fixed-size list).

    Please refer to the documentation here

    0 讨论(0)
  • 2020-11-28 04:49

    Ok i'm going to extend on the answers here since a lot of the people who come here want to split the string by a whitespace. This is how it's done:

    List<String> List = new ArrayList<String>(Arrays.asList(s.split("\\s+")));
    
    0 讨论(0)
  • 2020-11-28 04:51

    Let's take a question : Reverse a String. I shall do this using stream().collect(). But first I shall change the string into an ArrayList .

        public class StringReverse1 {
        public static void main(String[] args) {
    
            String a = "Gini Gina  Proti";
    
            List<String> list = new ArrayList<String>(Arrays.asList(a.split("")));
    
            list.stream()
            .collect(Collectors.toCollection( LinkedList :: new ))
            .descendingIterator()
            .forEachRemaining(System.out::println);
    
    
    
        }}
    /*
    The output :
    i
    t
    o
    r
    P
    
    
    a
    n
    i
    G
    
    i
    n
    i
    G
    */
    
    0 讨论(0)
  • 2020-11-28 04:52
     String s1="[a,b,c,d]";
     String replace = s1.replace("[","");
     System.out.println(replace);
     String replace1 = replace.replace("]","");
     System.out.println(replace1);
     List<String> myList = new ArrayList<String>(Arrays.asList(replace1.split(",")));
     System.out.println(myList.toString());
    
    0 讨论(0)
提交回复
热议问题