1

I have a List containing 370 elements , every element is List <String> containing 4 strings. I'm making one big string of all the data by doing like this:

String veryBigString = "";
for (List <String> s : myList) {
    for (String e : s){
            veryBigString += e;
    }
}

However, it takes very long time. Are there any options to make this quicker?

Edit: Using StringBuilder like many of you suggested, I get java.lang.OutOfMemoryError. Maybe I'm doing something wrong:

StringBuilder sb = new StringBuilder();
for (List <String> s : birdsInfoList) {
     for (String e : s){
         sb.append(e);
     }
}
Log.i("Very big String", String.valueOf(sb));
SuperMario32
  • 70
  • 1
  • 1
  • 9
  • 4
    Ideally, avoid doing it altogether. Otherwise, please use `StringBuilder`. – CommonsWare Nov 01 '16 at 17:41
  • Everytime you do `veryBigString += e` you are making a new String object, because Strings are immutable. `StringBuilder` is ideal here because you do not need to make a new object everytime you add on, you can just call `stringBuilder.append( e )` – Orin Nov 01 '16 at 17:43
  • Hint : try calling toString on your outer list ... That gives you exactly what your code is doing! – GhostCat Nov 01 '16 at 17:43
  • @CommonsWare nailed it. You should use StringBuilder. This solution does a great job explaining why: http://stackoverflow.com/questions/5234147/why-stringbuilder-when-there-is-string – Justin L Nov 01 '16 at 17:44
  • @CommonsWare not really, javac will compile to StringBuilder – Sleiman Jneidi Nov 01 '16 at 17:45
  • using list is bad for string concatening if you have huge element.Try using StringBuilder as @CommonsWare said – Real73 Nov 01 '16 at 17:45
  • @GhostCat - `toString` will output [string1, string2 , ...] so you'll have to get rid of the brackets, commas and spaces. – TDG Nov 01 '16 at 17:49
  • Thanks for the quick reply everybody. @ShahrairNazimReal I have a very huge element indeed! What should I use instead of List? Arrays? – SuperMario32 Nov 01 '16 at 18:03
  • @SuperMario32 There is no significant difference between an array and a list if you're using an `ArrayList` or something from `Arrays.asList`. These lists are just wrappers for arrays. – 4castle Nov 01 '16 at 18:04
  • If you're getting an `OutOfMemoryError`, it's because `StringBuilder` allocates more memory than needed to concatenate, with the benefit of not needing to increase that memory as often. If you want both speed and efficient memory use, then don't concatenate the strings. Put the `Log` inside the loop. – 4castle Nov 01 '16 at 18:12
  • If you're getting an OutOfMemoryError, then you just have too much data for _any_ approach to work at all. – Louis Wasserman Nov 01 '16 at 18:12
  • @SuperMario32 Agreed. I forgot about those. On the other hand I am wondering why you are storing stuff in nested lists when you then want to ignore that nesting completely ... – GhostCat Nov 01 '16 at 18:19
  • @4castle The main reason why I want to concatenate is because I want to save that data into a text file. That data is downloaded from external sources so by doing this I want to save the data so that the app won't download every time I open it. Excuse my English. – SuperMario32 Nov 01 '16 at 18:25
  • @GhostCat I'm developing an app about animals. My inner lists contains info about a certain animal. I don't use database because my app suppose to work offline that's why I'm trying these lists to store in a file and read the file later. – SuperMario32 Nov 01 '16 at 18:43
  • @SuperMario32 I would still restrain from defining your **own** text file format then. Instead consider serializing into JSON for example. I rather doubt that alone not using the StringBuilder is the cause of all your performance trouble. There are many things to consider when doing IO and network communication. Long story short: even when not using a DB, try to build on existing technologies. Defining your own file format is definitely **not** a good idea there. Example: when you just flatten your data like that - how do you the structure to *rebuild* it?! – GhostCat Nov 01 '16 at 19:15

1 Answers1

1

You should use StringBuilder and then use concatenate. Much better performance wise.

StringBuilder sb = new StringBuilder("Hello")
    .append("World");

For more information regarding this you can read this.

From Oracle Docs:

StringBuilder objects are like String objects, except that they can be modified. Internally, these objects are treated like variable-length arrays that contain a sequence of characters. At any point, the length and content of the sequence can be changed through method invocations.

Strings should always be used unless string builders offer an advantage in terms of simpler code (see the sample program at the end of this section) or better performance. For example, if you need to concatenate a large number of strings, appending to a StringBuilder object is more efficient.

Read here: http://docs.oracle.com/javase/tutorial/java/data/buffers.html

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108