Java: LinkedList printing without square brackets?

This is a pretty simple question. When you print a LinkedList, for example:

System.out.println(list); 

He prints it, surrounding the list in square brackets as follows:

 [thing 1, thing 2, thing 3] 

Is there a way to print it without square brackets?

+7
source share
8 answers

Yes - iterate the list and print (a comma after each, but the last item). However, there are utilities to help:

Guava :

 String result = Joiner.on(", ").join(list); 

commons-lang :

 String result = StringUtils.join(list, ", "); 

And one note: do not rely on the .toString() method of any object. It is not intended for displaying an object to users or for use as a predefined format - it is intended primarily for debugging purposes.

+21
source

Quick and dirty answer:

 String s = list.toString(); System.out.println(s.substring(1, s.length()-1)); 
+5
source

You can subclass LinkedList and override the toString() method, but that seems a bit overkill. Instead, swipe elements over them and build a String using either a StringBuilder or a StringBuffer (if the problem is concurrency).

Note:
I suggest you not follow @Sean Owen's answers, as it is implementation dependent and therefore fragile.

+2
source

This is the standard implementation of toString () for LinkedList. You can extend LinkedList to provide your own implementation or use composition and just implement the java.util.List interface.

 public class MyLinkedList<E> implements List<E> { private final List<E> delegate; public MyLinkedList(List<E> list) { delegate = list; } /** * @return see overriden class or implemented interface * @see java.lang.Object#toString() */ @Override public String toString() { return "My special toString() method"; } // implement rest of the java.util.List interface 
0
source

Changed from AbstractCollection.java :

  /* * @return a string representation of a collection */ public static final String collectionToString(Collection c) { Iterator<E> it = c.iterator(); if (! it.hasNext()) return ""; StringBuilder sb = new StringBuilder(); for (;;) { E e = it.next(); sb.append(e == c ? "(this Collection)" : e); if (! it.hasNext()) return sb.toString(); sb.append(',').append(' '); } } 
0
source
 StrBuilder builder = new StrBuilder(); // apache commons lang. for (Object object : yourList) { builder.appendSeperator(", "); builder.append(object.toString()); } System.out.println(builder.toString()); 
0
source

You can grab the String returned by the .toString() method and delete the first and last character, or create your own list class and override the .toString() method to iterate through the list and print out items without parentheses. Or you can do it as an anonymous class, for example:

 List<String> list = new List<String>() { public String toString() { // Custom To String Stuff Here } }; 

And of course I'm too slow.

0
source

If you are in groovy , there is no need to import anything, just do:

 list = ["thing 1", "thing 2", "thing 3"] println list.collect { i -> "$i" }.join(', ') 

item 1, item 2, item 3


The same thing happens with the collection of cards:

 map = [I:"James Bond", love:"rock N roll", id:"007"] println map.collect { k,v -> "$k = $v" }.join('\n') 

I = James Bond

love = rock n roll

id = 007

0
source

All Articles