package com.jike.list; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class iteratorDemo { public static void main(String[] args) { List<String>lists=new ArrayList<String>(); lists.add("A"); lists.add("B"); lists.add("C"); lists.add("D"); Iterator<String>iter=lists.iterator(); while(iter.hasNext()) { String str=iter.next(); if("A".equals(str)) { iter.remove(); }else System.out.println(str); } } }
輸出:java
B C D
map:鍵值對的方式存儲數據。blog
package com.jike.list; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class mapDemo01 { public static void main(String[] args) { Map<String, String>map=new HashMap<String,String>(); map.put("key1", "jike"); map.put("key2", "com"); map.put("key4", "world"); map.put("key3", "hello"); String str=map.get("key1"); System.out.println(str); if(map.containsKey("key1")){ System.out.println("key存在"); }else { System.out.println("key不存在"); } if(map.containsValue("aaa")) { System.out.println("值存在"); }else { System.out.println("值不存在"); } Set<String> s = map.keySet(); Iterator<String>i=s.iterator(); while (i.hasNext()) { System.out.println(i.next()); } } }
輸出:rem
jike key存在 值不存在 key1 key2 key3 key4