Java Iterable Join join(String glue, Iterable tokens)

Here you can find the source of join(String glue, Iterable tokens)

Description

join

License

Apache License

Declaration

static String join(String glue, Iterable<?> tokens) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Iterator;
import java.util.Map;

public class Main {
    static String join(String glue, Iterable<?> tokens) {
        final StringBuilder sb = new StringBuilder();
        final Iterator<?> iterator = tokens.iterator();
        while (iterator.hasNext()) {
            sb.append(iterator.next());/* ww  w .  j  a  v a 2 s  .c  om*/
            if (iterator.hasNext()) {
                sb.append(glue);
            }
        }
        return sb.toString();
    }

    static String join(String glue, Map<String, String> tokens,
            String tokensGlue) {
        final StringBuilder sb = new StringBuilder();
        final Iterator<Map.Entry<String, String>> iterator = tokens
                .entrySet().iterator();
        while (iterator.hasNext()) {
            final Map.Entry<String, String> entry = iterator.next();
            sb.append(entry.getKey()).append(tokensGlue)
                    .append(entry.getValue());
            if (iterator.hasNext()) {
                sb.append(glue);
            }
        }
        return sb.toString();
    }
}

Related

  1. join(Iterator iterator, String separator)
  2. join(String delimiter, Iterable strings)
  3. join(String delimiter, Iterable stringsIterable)
  4. join(String delimiter, String wrap, Iterable objs)
  5. join(String glue, Iterable pieces)
  6. join(String sep, Iterable strings)
  7. join(String separator, Iterable elements)
  8. join(String separator, Iterable objects)
  9. join(String separator, Iterable args)