案例分享:文章来源:https://www.toymoban.com/news/detail-609400.html
package test05;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.print.attribute.HashAttributeSet;
public class test03 {
public static void main(String[] args) {
// 数据源1:
List<Map<String,String>> list = new ArrayList<>();
Map<String, String> map1 = new HashMap<>();
map1.put("name", "北京");
map1.put("code", "123");
Map<String, String> map2 = new HashMap<>();
map2.put("name", "西安");
map2.put("code", "456");
Map<String, String> map3 = new HashMap<>();
map3.put("name", "成都");
map3.put("code", "789");
list.add(map1);
list.add(map2);
list.add(map3);
for (int i = 0; i < list.size(); i++) {
System.out.println("list:"+list.get(i));
}
System.out.println("-----------------------------");
// 数据源2:
List<Map<String,String>> list2 = new ArrayList<>();
Map<String, String> map4 = new HashMap<>();
map4.put("name", "北京");
map4.put("code", "123");
list2.add(map4);
for (int i = 0; i < list2.size(); i++) {
System.out.println("list2:"+list2.get(i));
}
System.out.println("-----------------------------");
// 迭代器删除数据:
Iterator<Map<String, String>> iterator = list.iterator();
while (iterator.hasNext()){
Map<String, String> next = iterator.next();
String name = String.valueOf(next.get("name"));
String code = String.valueOf(next.get("code"));
Iterator<Map<String, String>> iterator2 = list2.iterator();
while (iterator2.hasNext()){
Map<String, String> next2 = iterator2.next();
String regionName = String.valueOf(next2.get("name"));
String RegionCode = String.valueOf(next2.get("code"));
// 如果list和list2中的每条数据,name和code完全相同,则删除list中的数据;
if(name.equals(regionName) && code.equals(RegionCode)){
iterator.remove();
}
}
}
for (int i = 0; i < list.size(); i++) {
System.out.println("迭代器删除数据后:"+list.get(i));
}
}
}
测试输出:文章来源地址https://www.toymoban.com/news/detail-609400.html
list:{code=123, name=北京}
list:{code=456, name=西安}
list:{code=789, name=成都}
-----------------------------
list2:{code=123, name=北京}
-----------------------------
迭代器删除数据后:{code=456, name=西安}
迭代器删除数据后:{code=789, name=成都}
到了这里,关于<Java Iterator> 迭代器Iterator双层循环的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!