Here you can find the source of addAllSortNoDuplicates(List
Parameter | Description |
---|---|
l1 | List of strings to be merged. |
l2 | List of strings to be merged. |
Parameter | Description |
---|---|
NullPointerException | if either or both params <code>l1</code> and <code>l2</code> is null. |
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
.
public static List<String> addAllSortNoDuplicates(List<String> l1, List<String> l2)
//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); } }