Java List Join join(List list)

Here you can find the source of join(List list)

Description

join

License

Apache License

Declaration

public static String join(List<String> list) 

Method Source Code

//package com.java2s;
/**/*from   w w  w  .  j av a  2s . c o m*/
 *  Copyright (c) 2014 http://www.lushapp.wang
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */

import java.util.List;

public class Main {
    private static final String defaultSeparator = ",";

    public static String join(List<String> list) {
        return join(list, getDefaultSeparator());
    }

    public static String join(List<String> list, char separator) {
        return join(list, separator + "");
    }

    public static String join(List<String> list, String separator) {
        if (isEmpty(list)) {
            return "";
        }
        if (separator == null) {
            separator = getDefaultSeparator();
        }

        StringBuilder joinStr = new StringBuilder();
        for (String entry : list) {
            joinStr.append(entry).append(separator);
        }

        int lastIndexOfPosi = joinStr.lastIndexOf(separator);
        if (lastIndexOfPosi != -1 && lastIndexOfPosi == (joinStr.length() - separator.length())) {
            return joinStr.substring(0, joinStr.length() - separator.length());
        }
        return joinStr.toString();
    }

    public static String getDefaultSeparator() {
        return defaultSeparator;
    }

    public static boolean isEmpty(List<?> sourceList) {
        return (sourceList == null || sourceList.size() == 0);
    }
}

Related

  1. join(List elements)
  2. join(List items, char separator)
  3. join(List l, String joiner)
  4. join(List lines, char delim)
  5. join(List list)
  6. join(List list)
  7. join(List list)
  8. join(List list)
  9. join(List list)