Java Iterable to String toString(Iterable iterable)

Here you can find the source of toString(Iterable iterable)

Description

to String

License

Apache License

Declaration

public static String toString(Iterable<String> iterable) 

Method Source Code

//package com.java2s;
/*/*from  w w w  .ja va2  s .c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.List;

public class Main {
    public static String toString(Iterable<String> iterable) {
        String ret = "";

        if (iterable != null) {
            int count = 0;
            for (String str : iterable) {
                if (count == 0)
                    ret = str;
                else
                    ret += (", " + str);
                count++;
            }
        }

        return ret;
    }

    public static String toString(String[] arr) {
        String ret = "";

        if (arr != null && arr.length > 0) {
            ret = arr[0];
            for (int i = 1; i < arr.length; i++) {
                ret += (", " + arr[i]);
            }
        }

        return ret;
    }

    public static String toString(List<String> arr) {
        String ret = "";

        if (arr != null && arr.size() > 0) {
            ret = arr.get(0);
            for (int i = 1; i < arr.size(); i++) {
                ret += (", " + arr.get(i));
            }
        }

        return ret;
    }
}

Related

  1. toString(final Iterable iterable, final CharSequence delimiter)
  2. toString(Iterable c, String delim, String prefix, String suffix)
  3. toString(Iterable objects, String sep)
  4. toString(Iterable it, String separator)
  5. toString(Iterable names)
  6. toString(Iterable strs)
  7. toString(Iterable iterable, String separator)