刪除map、list集合元素總結


刪除map:

 

@Test
public void removeElementFromMap()
{
Map<Integer, String> test = new HashMap<Integer, String>();
test.put(1, "a");
test.put(2, "b");
test.put(3, "c");
test.put(4, "d");

Iterator<Entry<Integer, String>> it = test.entrySet().iterator();

int key = 0;
while (it.hasNext())
{
key = it.next().getKey();
if (key == 1)
{
it.remove();
}
}

System.out.println(test);
}

 

刪除List集合元素:

@Test
public void removeElementFromList()
{
List<Integer> testList = new ArrayList<Integer>();
testList.add(1111);
testList.add(2222);
testList.add(3333);
testList.add(4444);

Iterator<Integer> it = testList.iterator();

int value = 0;
while (it.hasNext())
{
value = it.next();
if (value == 1111)
{
it.remove();
}
}
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM