Java List Sort addAllSortNoDuplicates(List l1, List l2)

Here you can find the source of addAllSortNoDuplicates(List l1, List l2)

Description

To merge and sort two lists of strings.

License

Open Source License

Parameter

Parameter Description
l1 List of strings to be merged.
l2 List of strings to be merged.

Exception

Parameter Description
NullPointerException if either or both params <code>l1</code> and <code>l2</code> is null.

Return

Params l1 and l2 merged and sorted into one list of strings. NB. the list will not be sorted if any element in the merged list is null.

Declaration

public static List<String> addAllSortNoDuplicates(List<String> l1, List<String> l2) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;

import java.util.Collections;
import java.util.HashSet;

import java.util.List;

import java.util.Set;

public class Main {
    /**/*ww  w  .j  a v  a  2 s  . co m*/
     * To merge and sort two lists of strings. If some element in the lists
     * happens to be null the sort operation will be skipped.
     *
     * @param l1
     *            List of strings to be merged.
     * @param l2
     *            List of strings to be merged.
     *
     * @return Params <code>l1</code> and <code>l2</code> merged and sorted into
     *         one list of strings. NB. the list will not be sorted if any
     *         element in the merged list is <code>null</code>.
     *
     * @throws NullPointerException
     *             if either or both params <code>l1</code> and <code>l2</code> is null.
     */
    public static List<String> addAllSortNoDuplicates(List<String> l1, List<String> l2) {
        Set<String> set = new HashSet<String>();
        List<String> list = new ArrayList<String>();

        if (l1 != null && l2 != null) {
            set.addAll(l1);
            set.addAll(l2);
            list.addAll(set);

            if (!list.contains(null)) {
                Collections.sort(list);
            }

            return list;
        }

        throw new NullPointerException(
                "One or both of given arguments are null. Arguments --> l1 = " + l1 + ", l2 = " + l2);
    }
}

Related

  1. addSorted(final List list, final T element)
  2. calculateBox(List values, boolean copyAndSort)
  3. calculateQ1(List values, boolean copyAndSort)
  4. descendingSortByCreationTime(List tasks)