import java.util.HashMap;import java.util.Map;import java.util.Set;//Map(映射)集合 <键,值> public class MapTest { public static void main(String[] args) { Map map = new HashMap(); //1.增加元素 map.put(101,"金庸"); map.put(102,"古龙"); map.put(103,"倪匡"); map.put(104,"蔡澜"); System.out.println(map); System.out.println("2-------------------------"); //2.查找元素 Object m = map.get(102); System.out.println(m); System.out.println("3-------------------------"); //3.修改元素 map.replace(102,"古龙","黄霑"); System.out.println(map); System.out.println("4-------------------------"); //4.删除元素 map.remove(104); System.out.println(map); map.remove(103,"倪匡"); System.out.println(map); System.out.println("5--------------------------"); //5.查找所有键和值 Object key = map.keySet(); System.out.println(key); Object value = map.values(); System.out.println(value); //6.删除所有键和值 map.clear(); System.out.println(map); }} 键,值>
运行结果
{101=金庸, 102=古龙, 103=倪匡, 104=蔡澜}2-------------------------古龙3-------------------------{ 101=金庸, 102=黄霑, 103=倪匡, 104=蔡澜}4-------------------------{ 101=金庸, 102=黄霑, 103=倪匡}{ 101=金庸, 102=黄霑}5--------------------------[101, 102][金庸, 黄霑]{}