Step 1: Split your given string input as Arrays.asList(city.split("::"));, you alresy did it.

Step 2: Split each list in to array like, example Tamilnadu;chennai-madurai-salem using String both[]=string.split(";"); here you will get seperated state and cities. like both[0] is State. both[1] is chennai-madurai-salem

Step 3: Split the cities string in both[1] usig both[1].split("-")

So Demo code I have given as below. You can modify.

public static void main(String[] args) {
        String city = "Tamilnadu;chennai-madurai-salem::Kerala;cochin-tiruvandrum-calicut";
        ArrayList<String> mState = new ArrayList<String>();
        ArrayList<String> mCity = new ArrayList<String>();
        List<String> bothList= Arrays.asList(city.split("::"));

        for (String string : bothList) {
            String both[]=string.split(";");
            String state=both[0];
            List<String> tempCityList=Arrays.asList(both[1].split("-"));
            mState.add(state);
            mCity.addAll(tempCityList);
        }
        System.out.println("Your states");
        for (String string : mState) {
            System.out.print(string+" ");
        }
        System.out.println("\nYour cities");
        for (String string : mCity) {
            System.out.print(string+" ");
        }


    }
Answer from mahesh on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-a-string-to-arraylist-in-java
How to Convert a String to ArrayList in Java? - GeeksforGeeks
July 23, 2025 - Splitting the string by using the Java split() method and storing the substrings into an array.
Top answer
1 of 2
4

Step 1: Split your given string input as Arrays.asList(city.split("::"));, you alresy did it.

Step 2: Split each list in to array like, example Tamilnadu;chennai-madurai-salem using String both[]=string.split(";"); here you will get seperated state and cities. like both[0] is State. both[1] is chennai-madurai-salem

Step 3: Split the cities string in both[1] usig both[1].split("-")

So Demo code I have given as below. You can modify.

public static void main(String[] args) {
        String city = "Tamilnadu;chennai-madurai-salem::Kerala;cochin-tiruvandrum-calicut";
        ArrayList<String> mState = new ArrayList<String>();
        ArrayList<String> mCity = new ArrayList<String>();
        List<String> bothList= Arrays.asList(city.split("::"));

        for (String string : bothList) {
            String both[]=string.split(";");
            String state=both[0];
            List<String> tempCityList=Arrays.asList(both[1].split("-"));
            mState.add(state);
            mCity.addAll(tempCityList);
        }
        System.out.println("Your states");
        for (String string : mState) {
            System.out.print(string+" ");
        }
        System.out.println("\nYour cities");
        for (String string : mCity) {
            System.out.print(string+" ");
        }


    }
2 of 2
1

Here is the example

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by Prasad on 6/17/2014.
 */
public class stack {
    public static void main(String[] args) {
        String s="Tamilnadu;chennai-madurai-salem::Kerala;cochin-tiruvandrum-calicut";
        ArrayList<String> x1=new ArrayList<String>();
        ArrayList<String> x2=new ArrayList<String>();
        String string[]=s.split("::");
        for(String y:string){
            try{
                String[] temp=y.split(";");
                x1.add(temp[0]);
                x2.add(temp[1]);
            }catch(Exception e){}
        }
        System.out.println(x1.toString());
        System.out.println(x2.toString());
    }

}
🌐
BeginnersBook
beginnersbook.com › 2015 › 05 › java-string-to-arraylist-conversion
Java – How to Convert a String to ArrayList
September 20, 2022 - The input string consists of numbers and commas. We are splitting the string based on comma (,) as delimiter to get substrings. These substrings are assigned to ArrayList as elements. import java.util.ArrayList; import java.util.List; import java.util.Arrays; public class JavaExample { public ...
🌐
Baeldung
baeldung.com › home › java › java string › convert a comma separated string to a list in java
Convert a Comma Separated String to a List in Java
September 3, 2025 - And, throughout this tutorial, ... core Java. First, we’ll split our string into an array of strings using split, a String class utility method....
🌐
Programming.Guide
programming.guide › java › split-string-into-arraylist.html
Java: How to split a String into an ArrayList | Programming.Guide
Executing code in comments?! ... input = "aaa,bbb,ccc"; // To array String[] arr = input.split(","); // To fixed-size list List<String> l = Arrays.asList(input.split(",")); // To ArrayList List<String> l2 = new ArrayList<>(Arrays.asList(input.split(","))); Be the first to comment!
🌐
Quora
quora.com › How-do-you-split-a-string-and-store-it-in-an-array-in-Java
How to split a string and store it in an array in Java - Quora
Answer (1 of 3): SPLIT Meaning:- To divide or to make a group of people/things divide into smaller groups. In java. lang. String. split(String regex, int limit) method splits this string around matches of the given regular expression. The array returned by this method contains each substring of...
🌐
Javadevnotes
javadevnotes.com › java-split-string-into-arraylist-examples
Java Split String Into ArrayList Examples - JavaDevNotes
March 24, 2015 - import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { String sampleString = "Cat,Dog,Elephant"; String[] items = sampleString.split(","); List<String> itemList = new ArrayList<String>(); for (String item : items) { itemList.add(item); } System.out.println(itemList); } } The code manually loops the array list and add the items to the ArrayList.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 40237789 › split-string-and-store-in-array
java - Split String and store in array - Stack Overflow
I agree with @AxelH..If you have the list, you can convert it into an array and in this context, List is more preferable to use. 2016-10-25T11:05:27.23Z+00:00 ... This is a simple solution I use to read a CSV file. Scanner sc = null; try { sc = new Scanner(new File("test.csv")); String s = null; String[] array; while(sc.hasNext()){ s = sc.nextLine(); array = s.split(","); System.out.println(Arrays.toString(array)); } } catch (FileNotFoundException ex) { ex.printStackTrace(); } finally { if(sc != null) sc.close(); }
🌐
javathinking
javathinking.com › blog › convert-a-string-to-list-in-java
Converting a String to a List in Java — javathinking.com
In this example, we iterate over the tokens using the hasMoreTokens() and nextToken() methods and add them to an ArrayList. Fixed-size List with Arrays.asList(): When using Arrays.asList(), the resulting list is fixed-size. You cannot add or remove elements from it. If you try to do so, a UnsupportedOperationException will be thrown. import java.util.Arrays; import java.util.List; public class FixedSizeListPitfall { public static void main(String[] args) { String inputString = "one,two,three"; String[] array = inputString.split(","); List<String> list = Arrays.asList(array); try { list.add("four"); } catch (UnsupportedOperationException e) { System.out.println("Exception: " + e.getMessage()); } } }
🌐
Techie Delight
techiedelight.com › home › java › convert comma-separated string to list in java
Convert comma-separated String to List in Java | Techie Delight
July 7, 2026 - From Java 8 onward, we can use Stream. The idea is to split the string using the split() method, convert the array into the stream, and then collect all the input elements into a new list using the Collectors.toList() method.
🌐
Medium
medium.com › @AlexanderObregon › turning-a-string-into-a-list-of-words-in-java-c1e4bd85cad8
Turning a String into a List of Words in Java | Medium
July 28, 2025 - Every call to split() now makes new character arrays for each substring, even if two of them share the same characters. The new strings are stored in the heap and are collected later if you drop all references.
Top answer
1 of 2
2

If you inspect the output of System.out.println(Tuples); you'll notice a problem:

Tuples.add(List2) does not make a copy of List2. You're storing multiple references to the same List2 in Tuples, and appending more data to the same List2. So you end up with a very long list (i.e., List2) containing all tuples concatenated, and that list replicated many times inside Tuples.

Instead of

Tuples.add(List2);

try this instead:

Tuples.add(new ArrayList<>(List2));
List2.clear();

This will make elements of Tuples reference different lists.

2 of 2
1

I think you are doing it in a way too complicated way, just do:

    List<String> inputList = new ArrayList<String>();
    inputList.add("<b>SELL 512</b> lots of <b>[email protected]</b>");
    inputList.add("<b>BUY 513</b> lots of <b>[email protected]</b>");

    System.out.println("inputList: "+inputList);


    List<List<String>> Tuples = new ArrayList<List<String>>();
    List<String> list3 = new ArrayList<String>();
    List<String> list4 = new ArrayList<String>();
    List<String> list5 = new ArrayList<String>();
    List<String> list6 = new ArrayList<String>();

     for (int i = 0; i < inputList.size(); i++) {
           String var = inputList.get(i).trim();
           String[] splitArr=var.split("</b>|<b>|\\d*@"); //remove the \\d* if you expect to have xyz18 in output instead of x

           list3.add((splitArr[1].split("\\s"))[0]);
           list4.add((splitArr[1].split("\\s"))[1]);
           list5.add(splitArr[3]);
           list6.add(splitArr[4]);

           Tuples.add(Arrays.asList(var.replaceAll("</b>|<b>", "")));

       }
       System.out.println("Tuples: "+Tuples);
       System.out.println("list3: "+list3);
       System.out.println("list4: "+list4);
       System.out.println("list5: "+list5);
       System.out.println("list6: "+list6);

OUTPUT:

inputList: [<b>SELL 512</b> lots of <b>[email protected]</b>, <b>BUY 513</b> lots of <b>[email protected]</b>]
Tuples: [[SELL 512 lots of [email protected]], [BUY 513 lots of [email protected]]]
list3: [SELL, BUY]
list4: [512, 513]
list5: [xyz, abc]
list6: [112.00, 113.00]
🌐
LabEx
labex.io › tutorials › java-how-to-split-a-string-into-an-arraylist-using-a-delimiter-in-java-415655
How to split a string into an ArrayList using a delimiter in Java | LabEx
Java provides a built-in method called split() in the String class that allows us to divide a string into parts based on a delimiter. The method returns an array of strings containing the substrings divided by the delimiter.
🌐
How to do in Java
howtodoinjava.com › home › string › split csv string using regex in java
Split CSV String using Regex in Java
February 22, 2023 - String blogName = "how, to, do, in, java"; String[] tokenArray = blogName.split("\\s*,\\s*"); Assertions.assertArrayEquals(new String[]{"how", "to", "do", "in", "java"}, tokenArray); Note that the rex takes care of extra unwanted spaces in the String and only tokenizes the proper strings. In the following example, notice the unwanted multiple spaces between the commas. Assertions.assertArrayEquals(new String[]{"a", "b", "c"}, "a, b,c".split("\\s*,\\s*")); Assertions.assertArrayEquals(new String[]{"a", "b", "c"}, "a, b, c".split("\\s*,\\s*")); To get the List of tokens, we can pass the array to Arrays.asList() method that returns a fixed-size, unmodifiable read-only list backed by the array.