Java List Join join(List s, String delimiter)

Here you can find the source of join(List s, String delimiter)

Description

join

License

Apache License

Declaration

public static String join(List<? extends CharSequence> s, String delimiter) 

Method Source Code

//package com.java2s;
/*/*from  ww w . j  ava2  s. c om*/
  Copyright 2012 TripAdvisor, LLC
    
  Licensed 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.*;

public class Main {
    public static String join(List<? extends CharSequence> s, String delimiter) {
        int capacity = 0;
        int delimLength = delimiter.length();
        Iterator<? extends CharSequence> iter = s.iterator();
        if (iter.hasNext()) {
            capacity += iter.next().length() + delimLength;
        }

        StringBuilder buffer = new StringBuilder(capacity);
        iter = s.iterator();
        if (iter.hasNext()) {
            buffer.append(iter.next());
            while (iter.hasNext()) {
                buffer.append(delimiter);
                buffer.append(iter.next());
            }
        }
        return buffer.toString();
    }
}

Related

  1. join(List objects)
  2. join(List objects, String joiner)
  3. join(List strings, String delimiter)
  4. join(List vec, String separator)
  5. join(List items, CharSequence delimiter)
  6. join(List s, String delimiter)
  7. join(List items, String conjunction)
  8. join(List items, String separator)
  9. join(List list)