java 可通过HashSet踢除重复元素,代码如下
import java.util.ArrayList; import java.util.HashSet; import java.util.List; public class RemoveDuplicate { public static void main(String[] args) { List list = new ArrayList(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(1); list.add(1); List result = removeDuplicate(list); System.out.println(result); } public static List removeDuplicate(List list){ HashSet h = new HashSet(list); list.clear(); list.addAll(h); return list; } }
执行结果如下:
[1, 2, 3, 4]