Java TreeSet create from List

Description

Java TreeSet create from List




import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

public class Main {

    public static <T> TreeSet<T> copySet(List<T> ls) {
        TreeSet<T> s = new TreeSet<>();
        s.addAll(ls);//ww w .  j a v  a 2  s  .  c o m
        return s;
    }

    public static void main(String[] args) {
        List<Integer> ls = new ArrayList<>();
        ls.add(1);
        ls.add(1);
        ls.add(2);
        ls.add(5);
        ls.add(4);
        ls.add(4);

        TreeSet<Integer> s1 = copySet(ls);
        System.out.println(ls);
        System.out.println(s1);
    }
}



PreviousNext

Related