Java List Join joinUnique(List list, T element)

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

Description

Joins a list with an element only if the element is not yet contained in the list.

License

Apache License

Parameter

Parameter Description
list The list of elements to be joined together with the element.
element The additional element to join to the end of the list.
T The most specific common type of the list and the element.

Return

The list joined with the element if not yet contained.

Declaration

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

Method Source Code

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

import java.util.*;

public class Main {
    /**/*from   w  w w.ja v a 2s .  c  o  m*/
     * Joins a list with an element only if the element is not yet contained in the list.
     *
     * @param list    The list of elements to be joined together with the element.
     * @param element The additional element to join to the end of the list.
     * @param <T>     The most specific common type of the list and the element.
     * @return The list joined with the element if not yet contained.
     */
    public static <T> List<T> joinUnique(List<? extends T> list, T element) {
        return list.contains(element) ? new ArrayList<T>(list) : join(list, element);
    }

    /**
     * Joins a list with an element only if the element is not yet contained in the list.
     *
     * @param element The additional element to join to the beginning of the list.
     * @param list    The list of elements to be joined together with the element.
     * @param <T>     The most specific common type of the list and the element.
     * @return The list joined with the element if not yet contained.
     */
    public static <T> List<T> joinUnique(T element, List<? extends T> list) {
        if (list.contains(element)) {
            List<T> result = new ArrayList<T>(list.size() + 1);
            result.add(element);
            for (T item : list) {
                if (!item.equals(element)) {
                    result.add(item);
                }
            }
            return result;
        } else {
            return join(element, list);
        }
    }

    /**
     * Joins two lists with only adding the elements of the right list if they are not yet contained.
     *
     * @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> joinUnique(List<? extends T> leftList, List<? extends T> rightList) {
        List<T> result = new ArrayList<T>(leftList.size() + rightList.size());
        result.addAll(leftList);
        Set<T> addedElements = new HashSet<T>(leftList.size() + rightList.size());
        addedElements.addAll(leftList);
        for (T element : rightList) {
            if (addedElements.add(element)) {
                result.add(element);
            }
        }
        return result;
    }

    /**
     * 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. joinTextWithCommas(List list)
  2. joinThreads(List threads)
  3. joinTokens(final List tokens)
  4. joinToString(List list)
  5. JoinToString(List list, String delimiter)
  6. joinWordList(List wordList, String delimiter)
  7. listToStr(List list, String joinChar)
  8. listToText(List list, String join)
  9. orderJoinType(List jList)