当前位置:   article > 正文

【Java语言面向对象】(61)HashMap_map dict = new hashmap

map dict = new hashmap(){}

Map的遍历

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

键值对封装成了Map.Entry

Map中含有多个Map.Entry

  1. package com.njwbhz.March.week1.part0303;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. /**
  5. * @author FairyKunKun
  6. * @since 2022/3/3
  7. */
  8. public class TestHashMap1 {
  9. public static void main(String[] args) {
  10. Map<String , String> countries = new HashMap<String , String>();
  11. //Map中存放的键值对
  12. //底层是使用单链表实现的
  13. countries.put("CN","伟大的祖国");
  14. System.out.println(countries.get("CN"));
  15. countries.put("CN","中华人民共和国");
  16. //当K已经存在时,会覆盖原来的V
  17. //也就是原来CN指向的伟大的祖国会被修改成中卫人民共和国
  18. countries.put("JP","小日本");
  19. System.out.println(countries.containsKey("JP"));
  20. System.out.println(countries.containsValue("小日本"));
  21. countries.remove("JP");
  22. System.out.println(countries.containsKey("JP"));
  23. countries.put(null,null);
  24. // countries.containsKey();
  25. }
  26. }
  1. package com.njwbhz.March.week1.part0303;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import java.util.Set;
  6. import java.util.stream.Stream;
  7. public class TestHashMap2 {
  8. public static void main(String[] args) {
  9. Map<String , String> countries = new HashMap<String , String>();
  10. //
  11. countries.put("CN","伟大的祖国");
  12. countries.put("JP","小日本");
  13. countries.put("KR","棒子");
  14. countries.put("RS","战斗民族");
  15. // Set<String> keys = countries.keySet();
  16. // Iterator iterator = keys.iterator();
  17. // while (iterator.hasNext()) {
  18. // System.out.println(iterator.next() + "->" + countries.get(iterator.next()));
  19. // }
  20. // Set<Map.Entry<String , String>> entries = countries.entrySet();
  21. // Iterator<Map.Entry<String , String>> iterator1 = entries.iterator();
  22. // while (iterator1.hasNext()) {
  23. // System.out.println(iterator1.next().getKey() + "->" + iterator1.next().getValue());
  24. // }
  25. Set<Map.Entry<String , String>> entries = countries.entrySet();
  26. // entries.stream().forEach();
  27. entries.forEach(entry-> System.out.println(entry.getKey() + "->" + entry.getValue()));
  28. }
  29. }

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

按照如下格式输出

h---3次

e---3次

。。。。

o…

  1. package com.njwbhz.March.week1.part0303.test;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Set;
  5. public class Test {
  6. public static void main(String[] args) {
  7. String string = "hellohehe";
  8. char [] chars = string.toCharArray();
  9. Map<Character , Integer> map = new HashMap<Character, Integer>();
  10. for (int i = 0 ; i < chars.length ; i++) {
  11. if (map.containsKey(chars[i])) {
  12. map.put(chars[i] , map.get(chars[i]) + 1);
  13. }else {
  14. map.put(chars[i] , 1);
  15. }
  16. }
  17. Set<Map.Entry<Character , Integer>> entries = map.entrySet();
  18. entries.forEach(entry -> System.out.println(entry.getKey() + "————" + entry.getValue() + "次"));
  19. }
  20. }
  1. package com.njwbhz.March.week1.part0303.test;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Set;
  5. public class Test2 {
  6. public static void main(String[] args) {
  7. String string = "hellohehe";
  8. Map<String , Integer> map = new HashMap<String, Integer>();
  9. String s;
  10. for (int i = 0 ; i < string.length() ; i++) {
  11. s = String.valueOf(string.charAt(i));
  12. if (map.containsKey(s)) {
  13. map.put(s , map.get(s) + 1);
  14. }else {
  15. map.put(s , 1);
  16. }
  17. }
  18. Set<Map.Entry<String , Integer>> entries = map.entrySet();
  19. entries.forEach(entry -> System.out.println(entry.getKey() + "————" + entry.getValue() + "次"));
  20. }
  21. }
  1. package com.njwbhz.March.week1.part0303.test;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Set;
  5. public class Test3 {
  6. public static void main(String[] args) {
  7. String string = "hellohehe";
  8. Map<Character , Integer> map = new HashMap<Character, Integer>();
  9. char c;//保存单个字符
  10. for (int i = 0 ; i < string.length() ; i++) {
  11. c = string.charAt(i);
  12. if (map.containsKey(c)) {
  13. map.put(c , map.get(c) + 1);
  14. }else {
  15. map.put(c , 1);
  16. }
  17. }
  18. Set<Map.Entry<Character , Integer>> entries = map.entrySet();
  19. entries.forEach(entry -> System.out.println(entry.getKey() + "————" + entry.getValue() + "次"));
  20. }
  21. }
  1. package com.njwbhz.March.week1.part0303.test;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Set;
  5. public class Test4 {
  6. public static void main(String[] args) {
  7. String string = "hellohehe";
  8. // char [] chars = string.toCharArray();
  9. Map<String , Integer> map = new HashMap<String, Integer>();
  10. String s;
  11. for (int i = 0 ; i < string.length() ; i++) {
  12. s = string.substring(i , i + 1);
  13. if (map.containsKey(s)) {
  14. map.put(s , map.get(s) + 1);
  15. }else {
  16. map.put(s , 1);
  17. }
  18. }
  19. Set<Map.Entry<String , Integer>> entries = map.entrySet();
  20. entries.forEach(entry -> System.out.println(entry.getKey() + "————" + entry.getValue() + "次"));
  21. }
  22. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号