删除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();
}
}
}