Map

5、1 Map 集合的描述

Map集合的概述:
将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
K:键的值
V:值的值
创建Map集合的对象
!多态的方式
!具体的实现类HashMap

public class GenericDemo {public static void main(String[] args) {Map<String,String > map = new HashMap<>();//V put(K key, V value)//          将指定的值与此映射中的指定键关联(可选操作)。map.put("唐某人","大帅逼");map.put("唐人","帅逼");map.put("唐某","大逼");map.put("唐某","66");System.out.println(map);}
}

**注意:**put方法第一次使用是添加,第二次使用是覆盖;

public class MapDemo1 {public static void main(String[] args) {Map<String,String> map = new HashMap<>();map.put("唐康","大帅比");map.put("曲波","大帅比");map.put("B6","麻瓜");//map.remove("唐康");//map.clear();boolean b6 = map.containsKey("B6");System.out.println(b6);boolean b7 = map.containsKey("B7");System.out.println(b7);System.out.println(map.isEmpty());System.out.println(map.size());//System.out.println(map);}
}

5、2 Map集合的获取功能

public class MapDemo2 {public static void main(String[] args) {Map<String,String> map= new HashMap<>();map.put("唐康","大帅比");map.put("曲波","大帅比");map.put("B6","麻瓜");
//        System.out.println(map.get("唐康"));
//        System.out.println(map.get("康"));Set<String> set = map.keySet();System.out.println(set);System.out.println("--------");Collection<String> values = map.values();System.out.println(values);}
}

5、3 Map集合的遍历(方式1)

步骤:
1、先获取所有的键的集合
2、遍历集合,获取每个键对应的值
3、根据键再去找值

public class MapDemo3 {public static void main(String[] args) {Map<String, String> map = new HashMap<>();map.put("唐康", "大帅比");map.put("曲波", "大帅比");map.put("B6", "麻瓜");Set<String> set = map.keySet();for (String se:set) {String s = map.get(se);System.out.println(se+"--"+s);}}
}

5、4 Map集合的遍历(方式2)

entrySet()
返回此映射中包含的映射关系的 Set 视图。

public class MapDemo3 {public static void main(String[] args) {Map<String, String> map = new HashMap<>();map.put("唐康", "大帅比");map.put("曲波", "大帅比");map.put("B6", "麻瓜");Set<Map.Entry<String, String>> entries = map.entrySet();for (Map.Entry<String, String> name:entries) {String key = name.getKey();String value = name.getValue();System.out.println(key+"---"+value);}}
}
public class MapDemo3 {public static void main(String[] args) {Map<String, Student> map = new HashMap<>();Student student1 = new Student(1,"张三");Student student2 = new Student(1,"张三");Student student3 = new Student(1,"张三");Student student4 = new Student(1,"张三");map.put("1",student1);map.put("2",student2);map.put("3",student3);map.put("4",student4);//遍历Set<String> set = map.keySet();for (String s:set) {Student student = map.get(s);System.out.println(s+"--"+student);}System.out.println("----------------");Set<Map.Entry<String, Student>> entries = map.entrySet();for (Map.Entry<String, Student> y:entries) {String key = y.getKey();Student value = y.getValue();System.out.println(key+"---"+value);}}
}

宇宙无敌之Map集合讲解相关推荐

  1. Map集合(超详细+源码讲解)

    Map 目录 Map 一.Map集合简述 1.map集合是什么? 2.Map集合常用实现类 2.1 HashMap 2.2 HashTable 2.2.1 Properties 2.3 SortedM ...

  2. 对map集合进行排序

    今天做统计时需要对X轴的地区按照地区代码(areaCode)进行排序,由于在构建XMLData使用的map来进行数据统计的,所以在统计过程中就需要对map进行排序. 一.简单介绍Map 在讲解Map排 ...

  3. java集合课程,I学霸官方免费课程三十三:Java集合框架之Map集合

    I学霸官方免费教程三十三:Java集合框架之Map集合 Map接口 Map集合采用键值对(key-value)的方式存储数据,其中键不可以重复.值可以重复. 常用类有HashMap.TreeMap和P ...

  4. pat 1074. 宇宙无敌加法器(20)

    1074. 宇宙无敌加法器(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 地球人习惯使用十进制数,并且默 ...

  5. Java基础知识强化之集合框架笔记50:Map集合之Map集合的概述和特点

    1. Map集合的概述: 1 public interface Map<K,V> 作为学生来说,是根据学号来区分不同的学生的,那么假设我现在已经知道了学生的学号,我要根据学号去获取学生姓名 ...

  6. java8新特性:对map集合排序

    一.简单介绍Map 在讲解Map排序之前,我们先来稍微了解下map,map是键值对的集合接口,它的实现类主要包括:HashMap, TreeMap, Hashtable以及LinkedHashMap等 ...

  7. java map集合的等号改为逗号_老司机用一篇博客带你快速熟悉Dart语法

    [前言]Dart语言是使用flutter框架开发时候必备的语言,flutter是一个跨平台的框架,一套代码就可以完美实现安卓和ios两个平台,适配也很不错,Dart语言很友好,和java很类似,学习成 ...

  8. 18.集合框架(Map集合,HashMap和Hashtable的区别,Collections(集合工具类),集合练习,模拟斗地主(洗牌,发牌,看牌))

    1.Map集合概述和特点 1.需求:    根据学号获取学生姓名 2.Map接口概述     查看API可以知道:     将键映射到值的对象     一个映射不能包含重复的键     每个键最多只能 ...

  9. Java编程基础19——Map集合斗地主案例

    1_Map集合概述和特点 A:Map接口概述 查看API可以知道: 将键映射到值的对象 一个映射不能包含重复的键 每个键最多只能映射到一个值 B:Map接口和Collection接口的不同 Map是双 ...

最新文章

  1. command not found: shopt 的 ~/.bashrc
  2. VMWare虚拟机迁移时,打开后提示主机不支持 CPUID 错误
  3. 我的程序跑了 60 多小时,就是为了让你看一眼 JDK 的 BUG 导致的内存泄漏
  4. html 倒计时字体消失,最简单的一个网页倒计时代码 时间到期后会显示出提醒内容 收藏版...
  5. Java中HashMap原理
  6. kali下搭建WiFi钓鱼热点
  7. 带你细品Cookie、Session和Token的区别
  8. 如何解决谷歌浏览器插件屏蔽问题
  9. cad.net 筛选、选择集
  10. matlab 仿真短路故障设置,基于MatlabSimulink的电力系统故障仿真与
  11. Linux数据结构之radix-tree
  12. matlab linux x11 display,Xming安裝 + X11-Forwarding使用
  13. vb6.0企业版win7_教你安装纯净版windows系统
  14. 一网打尽Mac上的高效工具 - 效率工作篇(附演示视频)
  15. 从谷歌官网下载android 6.0源码、编译并刷入nexus 6p手机
  16. SpringBoot - LogAroundAop MVC请求日志拦截
  17. 基本的安全和HTTP认证
  18. 祝愿天下所有的有情人都终成眷属
  19. 解决关于电脑跳出一些垃圾弹窗的问题
  20. java游戏蜀山回合制,蓝港3D萌系回合制仙侠手游《大话蜀山》上线

热门文章

  1. RGB 空间颜色量化 - 减少颜色数目
  2. 【ChatBot】走进聊天机器人
  3. C语言程序设计实现调制解调,C语言程序设计课件.ppt
  4. 你觉得做程序员期间最开心的是什么?
  5. 117年的老商场也直播卖货了:活下来,再谈创造纪录!
  6. rtf富文本_轻松生成动态RTF(富文本格式)文档
  7. Mark Text使用说明
  8. 国外可以免费发布供求信息的网站(转)
  9. 在虚拟机上搭建云平台环境(2)Controller和Computer系统安装
  10. 「只推荐一位」一个全是Python干货的公众号