功能函数
package testset;
import java.util.*; public class ListSet { public static Set toSet(List set){ Set resultSet = new HashSet(set); return resultSet; } }测试主函数
package testset;
import java.util.*; public class TestSet { /** * @param args */ public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(1); list.add(3); Set set2 = ListSet.toSet(list); System.out.println("List size=" + list.size()); System.out.println("set size=" + set2.size()); System.out.println("set =" + set2); } }