Java List Join join(List list, T element)

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

Description

Creates a list that contains all elements of a given list with an additional appended element.

License

Apache License

Parameter

Parameter Description
list The list of elements to be appended first.
element The additional element.
T The list's generic type.

Return

An containing all elements.

Declaration

public static <T> List<T> join(List<? extends T> list, T element) 

Method Source Code

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

import java.util.*;

public class Main {
    /**/*w w  w  . jav a2s  .  c  o m*/
     * Creates a list that contains all elements of a given list with an additional appended element.
     *
     * @param list    The list of elements to be appended first.
     * @param element The additional element.
     * @param <T>     The list's generic type.
     * @return An {@link java.util.ArrayList} containing all elements.
     */
    public static <T> List<T> join(List<? extends T> list, T element) {
        List<T> result = new ArrayList<T>(list.size() + 1);
        result.addAll(list);
        result.add(element);
        return result;
    }

    /**
     * Creates a list that contains all elements of a given list with an additional prepended element.
     *
     * @param list    The list of elements to be appended last.
     * @param element The additional element.
     * @param <T>     The list's generic type.
     * @return An {@link java.util.ArrayList} containing all elements.
     */
    public static <T> List<T> join(T element, List<? extends T> list) {
        List<T> result = new ArrayList<T>(list.size() + 1);
        result.add(element);
        result.addAll(list);
        return result;
    }

    /**
     * Joins two lists.
     *
     * @param leftList  The left list.
     * @param rightList The right list.
     * @param <T>       The most specific common type of both lists.
     * @return A combination of both lists.
     */
    public static <T> List<T> join(List<? extends T> leftList, List<? extends T> rightList) {
        List<T> result = new ArrayList<T>(leftList.size() + rightList.size());
        result.addAll(leftList);
        result.addAll(rightList);
        return result;
    }
}

Related

  1. join(List items, String separator)
  2. join(List list)
  3. join(List olist, String glue)
  4. join(List valueList, String separator)
  5. join(List words, String iDelimiter)
  6. join(List _items, String _delim)
  7. join(List arr, String seperator)
  8. join(List items, char separator)
  9. join(List items, String delimiter)