Convert List to Set - Java java.util

Java examples for java.util:List Convert

Description

Convert List to Set

Demo Code


//package com.java2s;
import java.util.*;

public class Main {
    public static void main(String[] argv) {
        List list = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(toSet(list));
    }//from  w  ww . j  a  v  a  2s .  c  o  m

    public static <T> Set<T> toSet(List<T> list) {
        Set<T> set = new HashSet<T>();
        for (T t : list) {
            if (!set.contains(t)) {
                set.add(t);
            }
        }
        return set;
    }
}

Related Tutorials