Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 135283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:45:56+00:00 2026-05-11T06:45:56+00:00

I have an ArrayList that I want to output completely as a String. Essentially

  • 0

I have an ArrayList that I want to output completely as a String. Essentially I want to output it in order using the toString of each element separated by tabs. Is there any fast way to do this? You could loop through it (or remove each element) and concatenate it to a String but I think this will be very slow.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:45:56+00:00Added an answer on May 11, 2026 at 6:45 am

    Java 8 introduces a String.join(separator, list) method; see Vitalii Federenko’s answer.

    Before Java 8, using a loop to iterate over the ArrayList was the only option:

    DO NOT use this code, continue reading to the bottom of this answer to see why it is not desirable, and which code should be used instead:

    ArrayList<String> list = new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three");  String listString = "";  for (String s : list) {     listString += s + "\t"; }  System.out.println(listString); 

    In fact, a string concatenation is going to be just fine, as the javac compiler will optimize the string concatenation as a series of append operations on a StringBuilder anyway. Here’s a part of the disassembly of the bytecode from the for loop from the above program:

       61:  new #13; //class java/lang/StringBuilder    64:  dup    65:  invokespecial   #14; //Method java/lang/StringBuilder."<init>":()V    68:  aload_2    69:  invokevirtual   #15; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;    72:  aload   4    74:  invokevirtual   #15; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;    77:  ldc #16; //String \t    79:  invokevirtual   #15; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;    82:  invokevirtual   #17; //Method java/lang/StringBuilder.toString:()Ljava/lang/String; 

    As can be seen, the compiler optimizes that loop by using a StringBuilder, so performance shouldn’t be a big concern.

    (OK, on second glance, the StringBuilder is being instantiated on each iteration of the loop, so it may not be the most efficient bytecode. Instantiating and using an explicit StringBuilder would probably yield better performance.)

    In fact, I think that having any sort of output (be it to disk or to the screen) will be at least an order of a magnitude slower than having to worry about the performance of string concatenations.

    Edit: As pointed out in the comments, the above compiler optimization is indeed creating a new instance of StringBuilder on each iteration. (Which I have noted previously.)

    The most optimized technique to use will be the response by Paul Tomblin, as it only instantiates a single StringBuilder object outside of the for loop.

    Rewriting to the above code to:

    ArrayList<String> list = new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three");  StringBuilder sb = new StringBuilder(); for (String s : list) {     sb.append(s);     sb.append("\t"); }  System.out.println(sb.toString()); 

    Will only instantiate the StringBuilder once outside of the loop, and only make the two calls to the append method inside the loop, as evidenced in this bytecode (which shows the instantiation of StringBuilder and the loop):

       // Instantiation of the StringBuilder outside loop:    33:  new #8; //class java/lang/StringBuilder    36:  dup    37:  invokespecial   #9; //Method java/lang/StringBuilder."<init>":()V    40:  astore_2     // [snip a few lines for initializing the loop]    // Loading the StringBuilder inside the loop, then append:    66:  aload_2    67:  aload   4    69:  invokevirtual   #14; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;    72:  pop    73:  aload_2    74:  ldc #15; //String \t    76:  invokevirtual   #14; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;    79:  pop 

    So, indeed the hand optimization should be better performing, as the inside of the for loop is shorter and there is no need to instantiate a StringBuilder on each iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 141k
  • Answers 141k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want fast screenshots, you must use a lower… May 12, 2026 at 8:00 am
  • Editorial Team
    Editorial Team added an answer Try the following: public class MyClass { private URL url;… May 12, 2026 at 8:00 am
  • Editorial Team
    Editorial Team added an answer In one plain English "translation", the library remains available under… May 12, 2026 at 8:00 am

Related Questions

I'm storing an ArrayList of Ids in a processing script that I want to
I am facing some issues while serializing objects (I am using JBoss Drools, and
I wonder if it possible to not have jython automagicaly transform java objects to
I have an arraylist that contains items called Room. Each Room has a roomtype

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.