How to convert a String into an ArrayList?

前端 未结 13 613
囚心锁ツ
囚心锁ツ 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: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 list = new ArrayList(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
    */
    

提交回复
热议问题