Map的三种 遍历方式

Map中没有迭代器 不能直接迭代(遍历)
*
* Set keySet() 这个方法 是 把map中的所有键(key)取出
* 放入一个Set集合中 返回给你
* V get(Object key) 是通过传入键key 返回的是 键key 对应value值

interface InterA {
    // 在InterA接口中定义了 内部接口InterB
    interface InterB {
        public abstract void fun();
    }
}

// Test类 去实现InterB 接口
// 这里需要 使用 InterA.InterB
class Test implements InterA.InterB {
    @Override
    public void fun() {
        // TODO Auto-generated method stub

    }
}
public class Demo02 {
    public static void main(String[] args) {
        // fun1();
        // fun2();
        // fun3();
        // fun4();
        // fun5();
        // fun6();

        // 键盘输入字符串 统计字符串中每个字符出现的次数 
        // qwertyuioppoiuytrewq 使用map完成
        // 创建map 用来记录 字符出现次数
        // key 保存字符
        // value 保存次数

        HashMap<Character, Integer> map = new HashMap<>();
        System.out.println("请输入字符串");
        Scanner scanner  = new Scanner(System.in);
        String string = scanner.nextLine();
        // 字符串转成数组
        char[] charArray = string.toCharArray();
        // 遍历数组 取出每一个字符
        for (int i = 0; i < charArray.length; i++) {
            // 判断map中 有没有这个键
            // 取出字符数组中的每一个字符 当做map的key 去判断
            char key = charArray[i];
            // 判断map是否包含这个key
            if (!map.containsKey(key)) {
                // 不包含 直接进去 valu给个1
                map.put(key, 1);
            }else {        
                // 包含
                // 需要利用这个key 把值取出 + 1
                // 在原来的基础上 + 1
                Integer num = map.get(key);
                // 再put一遍 去覆盖原来的 value值
                map.put(key, num + 1);
                }
            }
        System.out.println(map);
    }

    private static void fun6() {
        // TreeMap 可以对map中 键进行排序
        // TreeSet 的 底层 依赖的是 TreeMap
        // 要排序 也需要实现 comparable 或者使用 比较器
        // 向TreeMap key是学生 value是户籍 添加3个键值对
        // 并按年龄排序

        TreeMap<Student, String> map = new TreeMap<>();
        map.put(new Student("京京", 14), "北京");
        map.put(new Student("海海", 15), "上海");
        map.put(new Student("安安", 16), "西安");
        System.out.println(map);
    }

    private static void fun5() {
        // linkedHashMap
        // 特点:怎么存就怎么取
        LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
        map.put("松松", 11);
        map.put("涵涵", 12);
        map.put("宁宁", 14);
        map.put("凉凉", 13);
        System.out.println(map);
    }

    private static void fun4() {
        // entrySet()方法 增强for循环遍历
        HashMap<String, Integer> map = new HashMap<>();
        map.put("松松", 11);
        map.put("涵涵", 12);
        map.put("宁宁", 14);
        map.put("凉凉", 13);
        Set<Entry<String, Integer>> entrySet = map.entrySet();
        for (Entry<String, Integer> entry : entrySet) {
            System.out.println(entry.getKey() ) ;
        }
    }



    private static void fun3() {
        // entrySet()
        // 该方法返回是一个集合 集合中存储是一个一个键值对对象
        // Entry对象 中 保存 key和value
        // 利用该方法遍历
        HashMap<String, Integer> map = new HashMap<>();
        map.put("松松", 11);
        map.put("涵涵", 12);
        map.put("宁宁", 14);
        map.put("凉凉", 13);
        // 获取map中所有的 Entry对象的Set集合
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        // 使用Set集合中 迭代器 对每一个Entry对象 遍历
        Iterator<Entry<String, Integer>> iterator = entrySet.iterator();
        // 迭代
        while (iterator.hasNext()) {
            // 取出Entry对象
            Entry<String, Integer> next = iterator.next();
            // 从Entry对象中 获取key 和 value
            String key = next.getKey();
            Integer value = next.getValue();
            System.out.println(key + " = " + value);
        }
    }

    /**
     * 
     */
    public static void fun2() {
        // 使用增强for循环遍历
        HashMap<String, Integer> map = new HashMap<>();
        map.put("松松", 11);
        map.put("涵涵", 12);
        map.put("宁宁", 14);
        map.put("凉凉", 13);
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            Integer value = map.get(key);
            System.out.println(key + " = " + value);
        }
    }

    /**
     * 
     */
    public static void fun1() {
        // 遍历Map
        HashMap<String, Integer> map = new HashMap<>();
        map.put("松松", 11);
        map.put("涵涵", 12);
        map.put("宁宁", 14);
        map.put("凉凉", 13);
        // 取出所有key的Set集合
        Set<String> keySet = map.keySet();
        // 取出Set集合中 迭代器
        Iterator<String> iterator = keySet.iterator();
        // 为了拿到所有的key 去遍历 Set集合
        while (iterator.hasNext()) {
            // 获取集合中每一个key
            String key = iterator.next();
            // 通过每一个找出 对应的每一个value
            // 这块注意 是从map中找 key对应Value
            Integer value = map.get(key);
            // 打印
            System.out.println(key + " = " + value);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值