How to convert String to ArrayList?

In my line, I can have an arbitrary number of words, separated by a comma. I wanted every word to be added to an ArrayList. For example:.

String s = "a,b,c,d,e,........."; 
+90
java string arraylist converter
Sep 08 '11 at 12:16
source share
13 answers

Try something like

 List<String> myList = new ArrayList<String>(Arrays.asList(s.split(","))); 



Demo:

 String s = "lorem,ipsum,dolor,sit,amet"; List<String> myList = new ArrayList<String>(Arrays.asList(s.split(","))); System.out.println(myList); // prints [lorem, ipsum, dolor, sit, amet] 



This post has been rewritten as an article here .

+228
Sep 08 '11 at 12:18
source share
  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()); 
+29
Sep 08 '11 at 12:28
source share

In Java 9, using List#of , which is a static immutable Factory list, has become easier.

  String s = "a,b,c,d,e,........."; List<String> lst = List.of(s.split(",")); 
+7
Oct 07 '17 at 14:12
source share

Option 1 :

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

Option 2 :

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

In my opinion, Option1 is better because

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

Please refer to the documentation here

+6
May 08 '17 at 16:42
source share

If you import or you have an array (type strings) in your code, and you need to convert it to arraylist (offcourse string), then it is better to use collections. eg:

 String array1[] = getIntent().getExtras().getStringArray("key1"); or String array1[] = ... then List allEds = new ArrayList(); Collections.addAll(allEds, array1); 
+2
Dec 04 '14 at 12:24
source share

If you want to convert a string to an 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 want to convert a string array to an 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; } 
+1
Sep 08 '11 at 12:30
source share

Well, I'm going to talk about the answers here, since many people who come here want to split the line on whitespace . Here's how to do it:

 List<String> List = new ArrayList<String>(Arrays.asList(s.split("\\s+"))); 
+1
Jun 26 '16 at 11:04 on
source share

You can use:

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

You should ask yourself if you really need an ArrayList. Very often, you are going to filter the list based on additional criteria for which Stream is ideal. You may need a kit; you can filter them with another regular expression, etc. Java 8 provides this very useful extension, by the way, that will work on any CharSequence : https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#splitAsStream-java.lang .CharSequence- . Since you do not need an array at all, do not create it this way:

 // This will presumably be a static final field somewhere. Pattern splitter = Pattern.compile("\\s+"); // ... String untokenized = reader.readLine(); Stream<String> tokens = splitter.splitAsStream(untokenized); 
+1
Oct 07 '17 at 13:55 on
source share

Itโ€™s easier to understand this:

 String s = "a,b,c,d,e"; String[] sArr = s.split(","); List<String> sList = Arrays.asList(sArr); 
+1
03 Oct '18 at 21:48
source share

Let's take the question: flip the line. I will do this with stream (). Collect (). But first, I will change the line in 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
Mar 18 '18 at 10:37
source share

I recommend using StringTokenizer, very efficient

  List<String> list = new ArrayList<>(); StringTokenizer token = new StringTokenizer(value, LIST_SEPARATOR); while (token.hasMoreTokens()) { list.add(token.nextToken()); } 
0
Nov 29 '18 at 21:15
source share

It uses Gson in Kotlin

  val listString = "[uno,dos,tres,cuatro,cinco]" val gson = Gson() val lista = gson.fromJson(listString , Array<String>::class.java).toList() Log.e("GSON", lista[0]) 
0
Dec 12 '18 at 15:32
source share

If you use guava (as it should be, look at Java 's effective element # 15 ):

 ImmutableList<String> list = ImmutableList.copyOf(s.split(",")); 
0
Jan 26 '19 at 3:56
source share



All Articles