我是靠谱客的博主 无限银耳汤,最近开发中收集的这篇文章主要介绍【Java语言面向对象】(61)HashMap,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Map的遍历

  1. HashMap底层的数据存储方式,使用了单向链表
  2. 数据封装成了Node,Node  extends Map.Entry

键值对封装成了Map.Entry

Map中含有多个Map.Entry

package com.njwbhz.March.week1.part0303;

import java.util.HashMap;
import java.util.Map;

/**
 * @author FairyKunKun
 * @since 2022/3/3
 */
public class TestHashMap1 {
    public static void main(String[] args) {
        Map<String , String> countries = new HashMap<String , String>();
        //Map中存放的键值对
        //底层是使用单链表实现的
        countries.put("CN","伟大的祖国");
        System.out.println(countries.get("CN"));
        countries.put("CN","中华人民共和国");
        //当K已经存在时,会覆盖原来的V
        //也就是原来CN指向的伟大的祖国会被修改成中卫人民共和国

        countries.put("JP","小日本");
        System.out.println(countries.containsKey("JP"));
        System.out.println(countries.containsValue("小日本"));
        countries.remove("JP");
        System.out.println(countries.containsKey("JP"));
        countries.put(null,null);
//        countries.containsKey();
        
    }
}
package com.njwbhz.March.week1.part0303;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

public class TestHashMap2 {
    public static void main(String[] args) {
        Map<String , String> countries = new HashMap<String , String>();
        
        //
        countries.put("CN","伟大的祖国");
        countries.put("JP","小日本");
        countries.put("KR","棒子");
        countries.put("RS","战斗民族");

//        Set<String> keys = countries.keySet();
//        Iterator iterator = keys.iterator();
//        while (iterator.hasNext()) {
//            System.out.println(iterator.next() + "->" + countries.get(iterator.next()));
//        }

//        Set<Map.Entry<String , String>> entries = countries.entrySet();
//        Iterator<Map.Entry<String , String>> iterator1 = entries.iterator();
//        while (iterator1.hasNext()) {
//            System.out.println(iterator1.next().getKey() + "->" + iterator1.next().getValue());
//        }

        Set<Map.Entry<String , String>> entries = countries.entrySet();
//        entries.stream().forEach();
        entries.forEach(entry-> System.out.println(entry.getKey() + "->" + entry.getValue()));

    }
}

需求:给定任意一个字符串,如“hellohehe”,统计每个字符出现的次数

按照如下格式输出

h---3次

e---3次

。。。。

o…

package com.njwbhz.March.week1.part0303.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test {
    public static void main(String[] args) {

        String string = "hellohehe";
        char [] chars = string.toCharArray();
        Map<Character , Integer> map = new HashMap<Character, Integer>();

        for (int i = 0 ; i < chars.length ; i++) {
            if (map.containsKey(chars[i])) {
                map.put(chars[i] , map.get(chars[i]) + 1);
            }else {
                map.put(chars[i] , 1);
            }
        }

        Set<Map.Entry<Character , Integer>> entries = map.entrySet();
        entries.forEach(entry -> System.out.println(entry.getKey() + "————" + entry.getValue() + "次"));
    }
}
package com.njwbhz.March.week1.part0303.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test2 {
    public static void main(String[] args) {

        String string = "hellohehe";
        Map<String , Integer> map = new HashMap<String, Integer>();
        String s;
        for (int i = 0 ; i < string.length() ; i++) {
            s = String.valueOf(string.charAt(i));
            if (map.containsKey(s)) {
                map.put(s , map.get(s) + 1);
            }else {
                map.put(s , 1);
            }
        }

        Set<Map.Entry<String , Integer>> entries = map.entrySet();
        entries.forEach(entry -> System.out.println(entry.getKey() + "————" + entry.getValue() + "次"));
    }
}
package com.njwbhz.March.week1.part0303.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test3 {
    public static void main(String[] args) {

        String string = "hellohehe";
        Map<Character , Integer> map = new HashMap<Character, Integer>();
        char c;//保存单个字符
        for (int i = 0 ; i < string.length() ; i++) {
            c = string.charAt(i);
            if (map.containsKey(c)) {
                map.put(c , map.get(c) + 1);
            }else {
                map.put(c , 1);
            }
        }

        Set<Map.Entry<Character , Integer>> entries = map.entrySet();
        entries.forEach(entry -> System.out.println(entry.getKey() + "————" + entry.getValue() + "次"));
    }
}
package com.njwbhz.March.week1.part0303.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test4 {
    public static void main(String[] args) {

        String string = "hellohehe";
//        char [] chars = string.toCharArray();
        Map<String , Integer> map = new HashMap<String, Integer>();
        String s;

        for (int i = 0 ; i < string.length() ; i++) {
            s = string.substring(i , i + 1);
            if (map.containsKey(s)) {
                map.put(s , map.get(s) + 1);
            }else {
                map.put(s , 1);
            }
        }

        Set<Map.Entry<String , Integer>> entries = map.entrySet();
        entries.forEach(entry -> System.out.println(entry.getKey() + "————" + entry.getValue() + "次"));
    }
}

最后

以上就是无限银耳汤为你收集整理的【Java语言面向对象】(61)HashMap的全部内容,希望文章能够帮你解决【Java语言面向对象】(61)HashMap所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部