Java List Uniqne Item unique(List list)

Here you can find the source of unique(List list)

Description

unique

License

Open Source License

Declaration

public static <T extends Comparable<? super T>> List<T> unique(List<T> list) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    public static <T extends Comparable<? super T>> List<T> unique(List<T> list) {
        if (list.size() < 2) {
            return list;
        }/*from   w  w  w  . ja v  a 2  s .c o  m*/
        List<T> sorted = new LinkedList<T>(list);
        Collections.sort(sorted);
        T current = sorted.get(sorted.size() - 1);
        for (int i = sorted.size() - 1; i > 0; i--) {
            T previous = sorted.get(i - 1);
            if (current == null || current.compareTo(previous) == 0) {
                sorted.remove(i);
            }
            current = previous;
        }
        if (sorted.get(0) == null) {
            sorted.remove(0);
        }
        return sorted;
    }
}

Related

  1. unique(List array)
  2. unique(List entities)
  3. uniqueList(Collection

    list)

  4. uniqueList(List list)
  5. uniqueListInsert(List list1, List list2)
  6. uniqueMacTest(List macs, String attempt)