code: public static void listToString (StringBuilder buf, Object val, Formatter formatter)
      {
          // get an iterator if this is a collection
          if (val instanceof Iterable<?>) {
              val = ((Iterable<?>)val).iterator();
          }
  
          String openBox = formatter.getOpenBox();
          String closeBox = formatter.getCloseBox();
  
          if (val instanceof Object[]) {
              buf.append(openBox);
              Object[] v = (Object[])val;
              for (int i = 0; i < v.length; i++) {
                  if (i > 0) {
                      buf.append(", ");
                  }
                  buf.append(formatter.toString(v[i]));
              }
              buf.append(closeBox);
  
          } else if (val instanceof Iterator<?>) {
              buf.append(openBox);
              Iterator<?> iter = (Iterator<?>)val;
              for (int i = 0; iter.hasNext(); i++) {
                  if (i > 0) {
                      buf.append(", ");
                  }
                  buf.append(formatter.toString(iter.next()));
              }
              buf.append(closeBox);
  
          } else if (val instanceof Enumeration<?>) {
              buf.append(openBox);
              Enumeration<?> enm = (Enumeration<?>)val;
              for (int i = 0; enm.hasMoreElements(); i++) {
                  if (i > 0) {
                      buf.append(", ");
                  }
                  buf.append(formatter.toString(enm.nextElement()));
              }
              buf.append(closeBox);
  
          } else {
              // fall back on the general purpose
              toString(buf, val);
          }
      }
docs: Formats the supplied collection into the supplied string buffer using the supplied
  formatter. See {@link #listToString(Object,StringUtil.Formatter)} for more details.