编程技巧系列(2)Java 集合(List,Set,Map)遍历时有条件删除特定元素

来源:互联网 发布:科比0506赛季详细数据 编辑:程序博客网 时间:2024/06/05 16:44
//方法一 先保存符合条件的值然后遍历删除
        List<String> obeyList = new ArrayList<String>();        for (String temp : list)        {            if (temp.compareTo(currentDate) < 0)            {                obeyList.add(temp);            }        }        for (String deleteLine : obeyList)        {            list.remove(deleteLine);        }
//方法二使用迭代法
    Iterator<String> it = list.iterator();    while (it.hasNext()) {   String temp = it.next();   if (temp.compareTo(currentDate) < 0) {       it.remove();            }    }
0 0
原创粉丝点击