Java Program to Convert an ArrayList into a string and Vice Versa

atanh() function in c++ stl

What is  an ArrayList?

An ArrayList is a class in the Java programming language that is used to store a dynamically sized list of elements. It is similar to an array, but can grow and shrink as needed to accommodate the number of elements being stored.

ArrayList is implemented as a resizable array, meaning that it grows and shrinks automatically as you add and remove elements. This makes it more efficient than using a regular array, which has a fixed size that you must specify when you create it.

Convert an ArrayList into a string :

To convert an ArrayList into a String, you can use the join method of the String class, along with the stream method of the ArrayList class. The stream method returns a stream of the elements in the ArrayList, and the join method concatenates the stream into a single string, separated by a delimiter string that you can specify.

 Pseudo code for converting an ArrayList into a string in Java:

import java.util.ArrayList;

// Define the ArrayList
ArrayList names = new ArrayList<>();

// Add elements to the ArrayList
names.add("Alice");
names.add("Bob");
names.add("Charlie");

// Define the delimiter string
String delimiter = ", ";

// Initialize an empty string to store the result
String result = "";

// Iterate through the elements of the ArrayList
for each element in names:
  // Concatenate the element and the delimiter to the result string
  result = result + element + delimiter;

// Remove the last delimiter from the result string
result = result.substring(0, result.length() - delimiter.length());

// Print the result string
print(result);

Example :

Run

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    // Define the ArrayList
    ArrayList<String> names = new ArrayList<>();
    names.add("Alice");
    names.add("Bob");
    names.add("Charlie");

    // Convert the ArrayList into a string
    String namesString = String.join(", ", names);

    // Print the result
    System.out.println(namesString);  // Outputs "Alice, Bob, Charlie"
  }
}

Output :

Alice, Bob, Charlie
Here is an example of how to convert an ArrayList into a string using the collect method of the stream and the joining collector:

Example :

Run

import java.util.ArrayList;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args) {
    // Define the ArrayList
    ArrayList <String>names = new ArrayList<>();
    names.add("Alice");
    names.add("Bob");
    names.add("Charlie");

    // Convert the ArrayList into a string
    String namesString = names.stream().collect(Collectors.joining(", "));

    // Print the result
    System.out.println(namesString);  // Outputs "Alice, Bob, Charlie"
  }
}

Output :

Alice, Bob, Charlie

Convert a String  into an ArrayList :

To convert a string into an ArrayList in Java, you can split the string into an array of substrings using the split method of the String class, and then use the asList method of the Arrays class to convert the array into an ArrayList.

Example :

Run

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

public class Main {
  public static void main(String[] args) {
    String namesString = "Alice, Bob, Charlie";

    // Split the string into an array of substrings
    String[] namesArray = namesString.split(", ");

    // Convert the array into an ArrayList
    ArrayList <String> names = new ArrayList<>(Arrays.asList(namesArray));

    // Print the elements of the ArrayList
    for (String name : names) {
      System.out.println(name);
    }
  }
}

Output :

Alice
Bob
Charlie

 

Explanation : 

The split method takes a delimiter string as an argument and returns an array of substrings that were separated by the delimiter in the original string. In this case, we use the “, ” string as the delimiter, so the resulting array will contain three elements: “Alice”, “Bob”, and “Charlie”.

The asList method takes an array as an argument and returns a fixed-size list that is backed by the array. We then pass this list to the constructor of the ArrayList class to create a new ArrayList that contains the same elements as the original string.

You can also use a loop to manually split the string and add each element to the ArrayList. Here is an example of how to do this:

 

Example :

Run

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    String namesString = "Alice, Bob, Charlie";

    // Initialize an empty ArrayList
    ArrayList<String> names = new ArrayList<>();

    // Split the string into substrings separated by the delimiter
    int startIndex = 0;
    int endIndex = namesString.indexOf(", ");
    while (endIndex != -1) {
      // Add the substring to the ArrayList
      names.add(namesString.substring(startIndex, endIndex));

      // Update the start and end indices for the next iteration
      startIndex = endIndex + 2;
      endIndex = namesString.indexOf(", ", startIndex);
    }

    // Add the final substring to the ArrayList
    names.add(namesString.substring(startIndex));

    // Print the elements of the ArrayList
    for (String name : names) {
      System.out.println(name);
    }
  }
}

Output :

Alice
Bob
Charlie

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription