Javatpoint Logo
Javatpoint Logo

How to Print Arraylist Without Brackets Java?

In Java, the ArrayList is a widely used data structure that allows dynamic resizing of elements. When it comes to displaying the contents of an ArrayList, the default behavior is to print the elements enclosed within square brackets. However, there are scenarios where you might want to print the ArrayList without these brackets. In this section, we will explore various methods to achieve it.

Method 1: Using a Loop

One of the simplest ways to print the contents of an ArrayList without brackets is by using a loop. The method allows us to iterate through the elements of the ArrayList and print them individually.

ArrayListExample.java

Output:

Apple Banana Orange

In this example, we use a for-each loop to iterate through the elements of the ArrayList fruits. Each element is printed with a space in between, resulting in the desired output.

Method 2: Using Java 8 Stream API

If you are using Java 8 or later versions, you can leverage the Stream API to achieve the same result in a more concise manner.

ArrayListExample.java

Output:

Apple Banana Orange

Here, we use the forEach() method of the ArrayList to apply the specified action (in this case, printing) to each element in the list.

Method 3: Using StringBuilder

Another approach is to use a StringBuilder to build a custom string representation of the ArrayList.

ArrayListExample.java

Output:

Apple Banana Orange

In this method, we create a StringBuilder named result to construct the desired output. We iterate through the ArrayList, appending each element followed by a space to the StringBuilder.

Method 4: Using String.join() (Java 8+)

Java 8 introduced the String.join() method that allows us to join elements of a collection using a specified delimiter. The method can be used to print ArrayList elements without brackets.

ArrayListExample.java

Output:

Apple Banana Orange

In this example, we use String.join() to join the elements of the ArrayList fruits with a space delimiter. The resulting string is then printed.

Method 5: Using a Custom Print Method

If you find yourself needing to print ArrayLists without brackets frequently, consider creating a custom method for it. It can improve code readability and reusability.

ArrayListExample.java

Output:

Apple Banana Orange

Here, we define a printArrayList() method that takes an ArrayList of strings as an argument. It then iterates through the elements, printing them with spaces in between. The method can be reused for any ArrayList of strings.

Printing the contents of an ArrayList without brackets in Java can be achieved using various methods. Whether we prefer loops, Stream API, or StringBuilder, the choice ultimately depends on your specific use case and coding style. By understanding these techniques, we can choose the one that best suits our needs and enhance the presentation of ArrayList contents in your Java programs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA