JDK-7172407 : There should be better printing functionality of List, Sets and Maps
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86
  • Submitted: 2012-05-29
  • Updated: 2021-03-03
  • Resolved: 2012-08-15
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
Presently the implementation of toString() in most of the collection classes (implementations of java.util.List, java.util.Set, java.util.Map) is such that it prints the content using comma (,) as the separator by default.
There should be a provision to set the separator to something else (e.g. tab character or newline character).

E.g.
        List myList = new ArrayList();
        myList.add("MyFirstObject");
        myList.add("MySecondObject");
        myList.add("MyThirdObject");

        myList.setPrintSeperator('\n');   // SOMETHING LIKE THIS
        
        System.out.println("Contents of collection are as follows \n"+myList);




JUSTIFICATION :
Presently if I have to print the contents of collection one below the other I have to use the following steps

        1.   List myList = new ArrayList();
        2.   myList.add("MyFirstObject");
        3.   myList.add("MySecondObject");
        4.   myList.add("MyThirdObject");
        5.
        6.   System.out.println("Contents of collection are as follows ");
        7.   for (Object obj : myList) {
        8.      System.out.println(obj);
        9.   }

Lines 7, 8 and 9 can be totally eliminated and that would make the code cleaner
Moreover if you set the separator once then the collection object can be printed 'N' number of times reducing the code by 'N' * 3 lines

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
After having set the separator character to be new line character when I print the List it should print as follows

MyFirstObject
MySecondObject
MyThirdObject

ACTUAL -
Presently because there is no provision to set the separator character it always prints as follows

[MyFirstObject, MySecondObject, MyThirdObject]

---------- BEGIN SOURCE ----------
import java.util.*;

public class Try {
    public static void printAsUsual() {
        List myList = new ArrayList();
        myList.add("MyFirstObject");
        myList.add("MySecondObject");
        myList.add("MyThirdObject");
        
        System.out.println("Contents of collection are as follows ");
        for (Object obj : myList) {
            System.out.println(obj);
        }
    }

    public static void printWithNewFeature() {
        List myList = new ArrayList();
        myList.add("MyFirstObject");
        myList.add("MySecondObject");
        myList.add("MyThirdObject");
        
        //#################################################################################
        //
        // myList.setPrintSeparator('\n');      // THIS IS THE FEATURE THAT I AM REQUESTING
        //
        //#################################################################################
        
        System.out.println("Contents of collection are as follows \n"+myList);
        
    }

}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Its not a bug, rather its a feature request.
So without this feature I have to run a loop and print if I want the contents to be printed one below the other rather than on the same line (comma seperated)