java arraylist remove null or ""

来源:互联网 发布:网络微拍大尺度红人 编辑:程序博客网 时间:2024/04/28 09:41

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 duplicatesSet<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























0 0
原创粉丝点击