12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package J20250723;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo06_Map02
- * @description
- * @create 2025/7/23
- */
- public class Demo06_Map02 {
- public static void main(String[] args) {
- Map<String,String> map=new HashMap<>();
- map.put("1001","张三");
- map.put("1002","李四");
- map.put("1003","王五");
- map.put("1004","赵六");
- map.put("1005","张十三");
- map.put("1006","张二十三");
- String s = map.get("1005");
- System.out.println("key是1005的value是"+s);
- //获取所有的key的集合
- Set<String> set = map.keySet();
- for (String s1 : set){
- System.out.println(s1);
- }
- //获取所有的value的集合
- Collection<String> values = map.values();
- for (String s2:values){
- System.out.println(s2);
- }
- //获取键值对对象的集合 通过entrySet遍历键值对集合
- Set<Map.Entry<String, String>> entries = map.entrySet();
- for (Map.Entry<String,String> e:entries){
- System.out.println("键:"+e.getKey());
- System.out.println("值:"+e.getValue());
- }
- System.out.println("---------------------------------");
- //遍历Map集合 --通过keySet()进行遍历
- //先获取所有键的集合
- Set<String> keySet = map.keySet();
- for (String key: keySet){
- //根据键获取值
- String value = map.get(key);
- System.out.println(key+"->"+value);
- }
- }
- }
|