List集合的removeAll(Collection<E> col) 和clear方法的区别
//removeAll()方法
private static void testList(){ List<String> list = new ArrayList<String>(); List<String> list1 = new ArrayList<String>(); list.add("123"); list.add("456"); list1.add("123"); list.removeAll(list1); Iterator<String> iterator = list1.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } }
//输出结果 456
//clear()方法
private static void testList(){ List<String> list = new ArrayList<String>(); List<String> list1 = new ArrayList<String>(); list.add("123"); list1.add("123"); list.add("456"); list.clear(); Iterator<String> iterator = list.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } if(list == null){ System.out.println("null"); }else if(list.size()==0){ System.out.println("list不为null"+"元素个数为"+list.size()); }else{ System.out.println("仍有元素"); } }
//输出结果 list不为null元素个数为0