zoukankan      html  css  js  c++  java
  • Java-集合框架-map1

    package cn.burce.HashMap;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    
    //Map的实现类HashMap
    
    public class Mapdemo1 {
        public static void main(String[] args) {
            fun();
            fun1();
        }
    
        public static void fun() {
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("abc", 1);
            map.put("abcd", 2);
            map.put("abcde", 3);
            map.put("abcde", 4);
            map.put("abcdef", 4);
            System.out.println(map);// 打印map
            System.out.println(map.get("abcde"));// 通过键取值,没有键返还null
        }
    
        public static void fun1() {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("测试1", 1);
            map.put("测试2", 2);
            map.put("测试3", 3);
            map.put("测试4", 4);
            // 迭代器的步骤取值
            Set<String> set = map.keySet();// 将键存储到set里 在迭代取出后找到对应的值
            Iterator<String> it = set.iterator();
            while (it.hasNext())
            {
                String s = it.next();
                int i = map.get(s);
                System.out.println(s + "...." + i);
            }
            System.out.println("----------------------------------");
            // 代码简化的foreach取值
            for (String key : map.keySet())
            {
                System.out.println(key + "...." + map.get(key));
            }
        }
    }

  • 相关阅读:
    HDU 1348 Wall
    HDU 2202 最大三角形
    HDU 2215 Maple trees
    HDU 1147 Pick-up sticks
    HDU 1392 Surround the Trees
    风语时光
    HDU 1115 Lifting the Stone
    HDU 1086 You can Solve a Geometry Problem too
    HDU 2108 Shape of HDU
    HDU 3360 National Treasures
  • 原文地址:https://www.cnblogs.com/BruceKing/p/13426795.html
Copyright © 2011-2022 走看看