java arraylist remove null or ""
ArrayList al=new ArrayList();
al.add("");
al.add("name");
al.add("");
al.add("");
al.add(4, "asd");
System.out.println(al);
首先,可以用arraylist自带的方法
removeAll(Collection<?> c)
来,这个方法api介绍是
* Removes from this list all of its elements that are contained in the * specified collection.
所以可以这样用 al.removeAll(Arrays.asList(null,"")); 这样会吧list中的 null ""删除
List<String> al = new ArrayList<>();
// add elements to al, including duplicates
Set<String> hs = new HashSet<>();
hs.addAll(al);
al.clear();
al.addAll(hs); 这样会改变顺序
List<String[]> someList = (List<String[]>) csvMappedData.get(Constants.CSV_DATA);
Iterator<String[]> iterator = someList.iterator();
while (iterator.hasNext()) {
String[] strings = iterator.next();
if (stringsShouldBeRemoved) {
iterator.remove();
}
}
http://www.iteye.com/topic/1132718