0

I am trying to print the

TreeMap<String, TreeSet<String>> abbreviations in Java

I tried:

if (abbreviations.size() > 0)
        abbreviations.forEach((x, y) -> {                     
            System.out.println(String.format("%-7s   : %-100s ", x, y)); 
        });

It printing like this:

US        : [ Unity Status,  Unresolved Sample]                                                                  
VC        : [ Value Color,  Value Contex,  Value Context,  Video Conference,  Visibility Created,  Visit Created,  Visits Completed] 

But I like to print without square brackets.

US        : Unity Status,  Unresolved Sample                                                                  
VC        : Value Color,  Value Contex,  Value Context,  Video Conference,  Visibility Created,  Visit Created,  Visits Completed

What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sun
  • 3,444
  • 7
  • 53
  • 83
  • 1
    Please read: [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) --- Iterate over the `Set`, print each entry manually. – Turing85 Sep 12 '21 at 13:02
  • Vague duplicate of [Output ArrayList to String without \[,\] (brackets) appearing](https://stackoverflow.com/questions/32774059/output-arraylist-to-string-without-brackets-appearing). But the good answer there using `String.join()` comes far down the list whereas here it’s the only answer so far. – Ole V.V. Sep 12 '21 at 14:58
  • 1
    Instead of `abbreviations.size() > 0`, you can write `! abbreviations.isEmpty()`, but this pre-test is entirely unnecessary. Further, `System.out.println(String.format("%-7s : %-100s ", …))` is a verbose version of `System.out.printf("%-7s : %-100s%n", …))`. Combining it with the accepted answer, your entire code should be `abbreviations.forEach((x, y) -> System.out.printf("%-7s : %-100s%n", x, String.join(", ", y)));` – Holger Sep 13 '21 at 08:13

1 Answers1

3

There are many ways to do this, one of them is to use String.join like this:

String.format("%-7s   : %-100s ", x, String.join(", ", y))
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140