Converts a list into a list with unique elements. The order is preserved; the second and subsequent occurrences are removed. If the list is already unique it is returned unchanged.
(List<E> list)
| 2351 | * |
| 2352 | * <p>If the list is already unique it is returned unchanged. */ |
| 2353 | public static <E> List<E> distinctList(List<E> list) { |
| 2354 | // If the list is small, check for duplicates using pairwise comparison. |
| 2355 | if (list.size() < QUICK_DISTINCT) { |
| 2356 | if (isDistinct(list)) { |
| 2357 | return list; |
| 2358 | } |
| 2359 | } else { |
| 2360 | // Lists that have all the same element are common. Avoiding creating a |
| 2361 | // set. |
| 2362 | if (allSameElement(list)) { |
| 2363 | return ImmutableList.of(list.get(0)); |
| 2364 | } |
| 2365 | } |
| 2366 | return ImmutableList.copyOf(new LinkedHashSet<>(list)); |
| 2367 | } |
| 2368 | |
| 2369 | /** Returns whether all of the elements of a list are equal. |
| 2370 | * The list is assumed to be non-empty. */ |