import java.util.ArrayList; import java.util.HashSet; import java.util.List; public class RemoveRepart { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("张三"); list.add("张三1"); list.add("张三2"); list.add("张三"); list.add("张三1"); list.add("张三2"); //方法一 for (int i = 0; i < list.size(); i++) { System.out.println("原生的list:"+list.get(i)); } //方法二 HashSet h = new HashSet(list); System.out.println("放入hashSet中的list:"+h); //[张三, 张三1, 张三2] list.clear(); System.out.println("list调用clear()后的:"+list); //[] list.addAll(h); System.out.println("处理后的list:"+list); //[张三, 张三1, 张三2] } }
时间: 2024-11-13 10:11:13