当前位置: 首页 > news >正文

333333333333

一、Map 接口

        接下来讲的都是基于 jdk8 来开展的。

1.1 特点

        1、Map Collection 并列存在。Map 是用于保存具有映射关系的数据,即 key-value

        2、Map 中的 key value 可以是任何引用类型的数据类型。

        3、Map 中的 key 不允许重复,原因和 HashSet 一样。

        4、Map 中的 value 是可以重复的。

        5、Map 中的 key 可以为 nullvalue 也可以为 null,注意 key null 时只能有一个,value null 时可以有多个。

        6、常用 String 类作为 Map key

        7、key value 之间存在单向一对一关系,即通过指定的 key 总能找到对于的 value

        8、Map 存放数据的 key-value 示意图,一对 key-value 是放在一个 Node 中的,又因为 Node 实现了 Entry 接口,也有人说是一对 key-value 就是一个 Entry

1.2 常用实现类

        HashMapHashTableSortedMap(接口)、TreeMap LinkedHashMapProperties 等。

1.3 常用方法

public class TestMap {public static void main(String[] args) {Map map = new HashMap<>();// put 添加元素map.put("孙悟空","唐僧");map.put("孙悟空","猪八戒");map.put("宋江","潘金莲");map.put("武大郎","西门庆");map.put("曹操",null);map.put(null,"荀彧");// get 根据 key 获取元素Object o = map.get("武大郎");// 根据 key 删除元素map.remove("宋江");// 判断 key 是否存在boolean b = map.containsKey(null);System.out.println(b);// 获取 map 的元素个数System.out.println(map.size());// 判断 map 元素个数是否为 0System.out.println(map.isEmpty());// 清除 mapmap.clear();}
}

1.4 遍历方式

1.4.1 keySet 方式

public static void main(String[] args) {Map map = new HashMap<>();map.put("孙悟空","唐僧");map.put("武大郎","西门庆");// 先取出所有的 key,然后再取出 valueSet keySet = map.keySet();// (1) 使用增强 for 循环for (Object obj :keySet) {System.out.println(obj+"-"+map.get(obj));}System.out.println("-----------------------");// (2) 迭代器Iterator iterator = keySet.iterator();while (iterator.hasNext()) {Object next =  iterator.next();System.out.println(next+"-"+map.get(next));}
}

1.4.2 value 值方式

public static void main(String[] args) {Map map = new HashMap<>();map.put("孙悟空","唐僧");map.put("武大郎","西门庆");// 只能取出所有的 valuesCollection values = map.values();// (1) 增强 for 循环for (Object obj :values) {System.out.println(obj);}System.out.println("-----------------------");//(2)迭代器Iterator iterator1 = values.iterator();while(iterator1.hasNext()){Object next = iterator1.next();System.out.println(next);}
}

1.4.3 EntrySet 方式

public static void main(String[] args) {Map map = new HashMap<>();map.put("孙悟空","唐僧");map.put("武大郎","西门庆");Set set = map.entrySet();// (1) 增强 for 循环for (Object entry :set) {// 将 entry 转换成 Map.EntryMap.Entry m = (Map.Entry)entry;System.out.println(m.getKey()+"-"+m.getValue());}System.out.println("-----------------------");// (2) 迭代器Iterator iterator2 = set.iterator();while (iterator2.hasNext()) {Map.Entry m=  (Map.Entry) iterator2.next();System.out.println(m.getKey()+"-"+m.getValue());}
}

1.5 习题练习

        使用 HashMap 添加 3 个员工对象,要求:键为员工 id,值为员工对象。并遍历显示工资 > 18000 员工(遍历方式最少两种)。员工类:姓名、工资、员工 id

class Employee{private String id;private String name;private double salary;public Employee(String id, String name, double salary) {this.id = id;this.name = name;this.salary = salary;}// setter、getter、toString@Overridepublic String toString() {return "Employee{" +"id='" + id + '\'' +", name='" + name + '\'' +", salary=" + salary +'}';}
}
public static void main(String[] args) {Map map = new HashMap();Employee e1 = new Employee("1","张三",20000);Employee e2 = new Employee("2","李四",8000);Employee e3 = new Employee("3","王五",60000);map.put(e1.getId(),e1);map.put(e2.getId(),e2);map.put(e3.getId(),e3);Set set = map.keySet();for (Object key :set) {Employee employee = (Employee)map.get(key);if(employee.getSalary()>18000){System.out.println(key+"-"+employee);}}System.out.println("--------------");Iterator iterator = set.iterator();while (iterator.hasNext()) {Object key =  iterator.next();Employee employee = (Employee)map.get(key);if(employee.getSalary()>18000){System.out.println(key+"-"+employee);}}System.out.println("--------------");Set set1 = map.entrySet();for (Object entrySet :set1) {Map.Entry entry =  (Map.Entry)entrySet;Employee employee = (Employee) entry.getValue();if(employee.getSalary()>18000){System.out.println(entry.getKey()+"-"+employee);}}
}
http://www.lryc.cn/news/217255.html

相关文章:

  • Python:字符串格式化
  • 虹科示波器 | 汽车免拆检修 | 2010款江铃陆风X8车发动机怠速抖动、加速无力
  • 左和右,激进与保守,都是相对概念,但是都会滑向同一种
  • js中的遍历
  • Python算法——快速排序
  • 操作系统备考学习 day12 (第五章)
  • Elasticsearch删除映射类型
  • 网络工程师进阶课:华为HCIP认证课程介绍
  • 单行自动横向滚动——css实现
  • 多线程基础
  • 贝锐向日葵亮相阿里云“云栖大会”:独创专利算法赋能全新云桌面
  • QT在线安装5.15之前的版本(下载速度飞快)
  • 零日漏洞预防
  • 企业内部外网向内网传输文件如何实现高效安全?
  • C++--二叉搜索树初阶
  • Type List(C++ 模板元编程)
  • 使用老北鼻CharGPT对话查询 Qt/C++ 使用gumbo-parse解析加载的html全过程
  • ​ iOS App Store上传项目报错 缺少隐私政策网址(URL)解决方法
  • 设计模式第一课-单例模式(懒汉模式和饿汉模式)
  • Yaml文件详解
  • 【题解 线段树】[蓝桥杯 2022 省 A] 选数异或
  • 宠物喂食器方案智能开发设计
  • chatgpt综述阅读理解
  • XCTF-RSA-2:baigeiRSA2、 cr4-poor-rsa
  • js 根据word文档模板导出内容
  • AIGC | 如何用“Flow”,轻松解决复杂业务问题
  • 多级菜单 树结构 排序 前端 后端 java
  • LAN-Free在数据备份时的应用与优势
  • HTML 文档声明和语言设置
  • 【C++基础知识学习笔记】精华版(复习专用)