import java.util.*; public class ArrayListRemove { //字符字符 public static void HashMapDemo1(){ HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put("a", "AA"); hashMap.put("b", "BB"); hashMap.put("c", "CC"); System.out.println(hashMap);//{a=AA, b=BB, c=CC} System.out.println("a:" + hashMap.get("a"));//a:AA System.out.println(hashMap.containsKey("a"));//true System.out.println(hashMap.keySet());//[a, b, c] System.out.println(hashMap.isEmpty());//false hashMap.remove("a"); System.out.println(hashMap.containsKey("a"));//false //采用Iterator遍历HashMap Iterator it = hashMap.keySet().iterator(); while(it.hasNext()) { String key = (String)it.next(); System.out.println("key:" + key); System.out.println("value:" + hashMap.get(key)); // key:b // value:BB // key:c // value:CC } } //字符列表 public static void HashMapDemo2(){ HashMap<String, List<String>> map = new HashMap<String, List<String>>(); ArrayList<String> list1=new ArrayList<String>(); list1.add("a"); list1.add("aa"); map.put("list1",list1); ArrayList<String> list2=new ArrayList<String>(); list2.add("b"); list2.add("bb"); map.put("list2",list2); System.out.println(map);//{list1=[a, aa], list2=[b, bb]} System.out.println("list1:" + map.get("list1"));//list1:[a, aa] System.out.println(map.containsKey("list1"));//true System.out.println(map.keySet());//[list1, list2] System.out.println(map.isEmpty());//false map.remove("a"); System.out.println(map.containsKey("a"));//false //采用Iterator遍历HashMap Iterator it = map.keySet().iterator(); while(it.hasNext()) { String key = (String) it.next(); System.out.println("key:" + key); System.out.println("value:" + map.get(key)); // key:list1 // value:[a, aa] // key:list2 // value:[b, bb] } } public static void main(String[] args) { HashMapDemo1(); HashMapDemo2(); } }
Java 中HashMap的基本操作增删遍历
猜你喜欢
转载自blog.csdn.net/a1084958096/article/details/80213085
今日推荐
周排行