編程技巧系列(2)Java 集合(List,Set,Map)遍歷時有條件刪除特定元素

//方法一 先保存符合條件的值然後遍歷刪除

        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();
            }
    }
發佈了21 篇原創文章 · 獲贊 159 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章