day23--Java集合06

打印 上一主题 下一主题

主题 877|帖子 877|积分 2633

Java集合06

13.Map接口02

13.2Map接口常用方法


  • put():添加
  • remove():根据键键删除映射关系
  • get():根据键获取值
  • size():获取元素个数
  • isEnpty():判断个数是否为0
  • clear():清除
  • containsKey():查找键是否存在
例子1:Map接口常用方法
  1. package li.map;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. @SuppressWarnings("all")
  5. public class MapMethod {
  6.     public static void main(String[] args) {
  7.         Map map = new HashMap();
  8.         // 1.put():添加
  9.         map.put("罗贯中",new Book("111",99));//ok
  10.         map.put("罗贯中","三国演义");//替换上一个value
  11.         map.put("施耐庵","666");//ok
  12.         map.put("克鲁苏","666");//ok
  13.         map.put(null,"空空如也");//ok
  14.         map.put("空空如也",null);//ok
  15.         System.out.println(map);
  16.         // 2.remove():根据键键删除映射关系
  17.         map.remove(null);
  18.         System.out.println(map);//null对应的"空空如也"没有了
  19.         // 3. get():根据键获取值,返回一个Object类型
  20.         System.out.println(map.get("罗贯中"));//三国演义
  21.         // 4. size():获取k-v对数
  22.         System.out.println(map.size());//4
  23.         // 5. isEnpty():判断个数是否为0
  24.         System.out.println(map.isEmpty());//false
  25.         // 6. clear():将所有k-v清空
  26.         map.clear();
  27.         System.out.println(map);//{}
  28.         // 7. containsKey():查找键是否存在
  29.         map.put("我是123","我是123的value");
  30.         System.out.println(map.containsKey("我是123"));//true
  31.     }
  32. }
  33. class Book{
  34.     private String name;
  35.     private int price;
  36.     public Book(String name, int price) {
  37.         this.name = name;
  38.         this.price = price;
  39.     }
  40.     @Override
  41.     public String toString() {
  42.         return "Book{" +
  43.                 "name='" + name + '\'' +
  44.                 ", price=" + price +
  45.                 '}';
  46.     }
  47. }
复制代码
13.3Map接口六大遍历方式



  • containsKey:查找键是否存在
  • keySet:获取所有的键
  • entrySet:获取所有的关系k-v
  • values:获取所有的值
例子:
  1. package li.map;
  2. import java.util.*;
  3. @SuppressWarnings("all")
  4. public class MapFor {
  5.     public static void main(String[] args) {
  6.         Map map = new HashMap();
  7.         map.put("罗贯中", new Book("111", 99));
  8.         map.put("罗贯中", "三国演义");
  9.         map.put("施耐庵", "666");
  10.         map.put("克鲁苏", "666");
  11.         map.put(null, "空空如也");
  12.         map.put("空空如也", null);
  13.         
  14.         //第一组:先取出所有的key,通过key取出对应的value
  15.         Set keySet = map.keySet();
  16.         System.out.println("----增强for----");
  17.         
  18.         //增强for
  19.         for (Object key : keySet) {
  20.             System.out.println(key + "-" + map.get(key));//get():根据键获取值
  21.         }
  22.         
  23.         System.out.println("----迭代器----");
  24.         //迭代器
  25.         Iterator iterator = keySet.iterator();
  26.         while (iterator.hasNext()) {
  27.             Object key = iterator.next();
  28.             System.out.println(key + "-" + map.get(key));
  29.         }
  30.         
  31.         //第二组:把所有的values值取出
  32.         Collection values = map.values();
  33.         //这里可以使用所有collection使用的遍历方法
  34.         
  35.         //增强for:
  36.         System.out.println("---取出所有的value 增强for---");
  37.         for (Object value : values) {
  38.             System.out.println(value);
  39.         }
  40.         
  41.         //迭代器:
  42.         System.out.println("---取出所有的value 迭代器:---");
  43.         Iterator iterator2 = values.iterator();
  44.         while (iterator2.hasNext()) {
  45.             Object value = iterator2.next();
  46.             System.out.println(value);
  47.         }
  48.         
  49.         //第三组:通过EntrySet直接取出k-v对
  50.         Set entrySet = map.entrySet();//EntrySet<Map.Entry<K,V>>
  51.         
  52.         //(1)增强for
  53.         System.out.println("---使用EntrySet的增强for---");
  54.         for (Object entry : entrySet) {
  55.             //将entry转成 Map.Entry
  56.             Map.Entry m = (Map.Entry) entry;
  57.             System.out.println(m.getKey()+"-"+m.getValue());
  58.         }
  59.         
  60.         //(2)迭代器:
  61.         System.out.println("---使用EntrySet的迭代器---");
  62.         Iterator iterator3 = entrySet.iterator();
  63.         while (iterator3.hasNext()) {
  64.             Object entry =  iterator3.next();//这里next取出来的类型本质上是Node,让偶向上转型为Object
  65.             //System.out.println(next.getClass());//class java.util.HashMap$Node
  66.             //向下转型Object---> Map.Entry
  67.             Map.Entry m = (Map.Entry)entry;
  68.             System.out.println(m.getKey()+"-"+m.getValue());
  69.         }
  70.     }
  71. }
复制代码
13.4Map课堂练习

使用HashMap添加三个员工对象,要求:
键:员工id
值:员工对象
并遍历显示工资>18000的员工(遍历方式最少两种)
员工类:姓名、工资、员工id
练习:
  1. package li.map;
  2. import java.util.*;
  3. @SuppressWarnings("all")
  4. public class MapExercise {
  5.     public static void main(String[] args) {
  6.         Map map = new HashMap();
  7.         map.put(1, new Employee("smith", 8800, 1));
  8.         map.put(2, new Employee("John", 18900, 2));
  9.         map.put(3, new Employee("Jack", 8900, 3));
  10.         map.put(4, new Employee("Marry", 19900, 4));
  11.         map.put(5, new Employee("Jack", 3000, 5));
  12.         //keySet
  13.         Set keySet = map.keySet();
  14.         System.out.println("---keySet的增强for---");
  15.         for (Object key : keySet) {
  16.             Employee value = (Employee) map.get(key);//将获得的value对象向下转型为Employee类型
  17.             double salary = value.getSalary();//获取工资
  18.             if (salary > 18000) {
  19.                 System.out.println(map.get(key));
  20.             }//判断输出
  21.         }
  22.         System.out.println("---keySet的迭代器---");
  23.         Iterator iterator = keySet.iterator();
  24.         while (iterator.hasNext()) {
  25.             Object key = iterator.next();
  26.             Employee value = (Employee) map.get(key);//将获得的value对象向下转型为Employee类型
  27.             double salary = value.getSalary();//获取工资
  28.             if (salary > 18000) {
  29.                 System.out.println(map.get(key));
  30.             }//判断输出
  31.         }
  32.         //EntrySet
  33.         Set entrySet = map.entrySet();
  34.         System.out.println("---entrySet的增强for---");
  35.         for (Object entry : entrySet) {//entry代表一对k-v
  36.             //将entry向下转型转成 Map.Entry
  37.             Map.Entry m = (Map.Entry) entry;
  38.             Employee employee = (Employee) m.getValue();
  39.             double salary = employee.getSalary();
  40.             if (salary > 18000) {//判断输出
  41.                 System.out.println(m.getValue());
  42.             }
  43.         }
  44.         System.out.println("---entrySet的迭代器---");
  45.         Iterator iterator2 = entrySet.iterator();
  46.         while (iterator2.hasNext()) {
  47.             Object entry = iterator2.next();
  48.             Map.Entry m = (Map.Entry) entry;//将Object强转为Map.Entry类型
  49.             Employee employee = (Employee) m.getValue();
  50.             double salary = employee.getSalary();
  51.             if (salary > 18000) {//判断输出
  52.                 System.out.println(m.getValue());
  53.             }
  54.         }
  55.     }
  56. }
  57. class Employee {
  58.     private String name;
  59.     private double salary;
  60.     private int id;
  61.     public Employee(String name, double salary, int id) {
  62.         this.name = name;
  63.         this.salary = salary;
  64.         this.id = id;
  65.     }
  66.     @Override
  67.     public String toString() {
  68.         return "Employee{" +
  69.                 "name='" + name + '\'' +
  70.                 ", salary=" + salary +
  71.                 ", id=" + id +
  72.                 '}';
  73.     }
  74.     public String getName() {
  75.         return name;
  76.     }
  77.     public void setName(String name) {
  78.         this.name = name;
  79.     }
  80.     public double getSalary() {
  81.         return salary;
  82.     }
  83.     public void setSalary(double salary) {
  84.         this.salary = salary;
  85.     }
  86.     public int getId() {
  87.         return id;
  88.     }
  89.     public void setId(int id) {
  90.         this.id = id;
  91.     }
  92. }
复制代码
13.5HashMap小结


  • Map接口的常用实现类:HashMap、Hashtable、Properties
  • HashMap是Map接口使用频率最高的实现类
  • HashMap是以key-value对的方式来存储数据(HashMap$Node类型)
  • key不能重复,value可以重复。允许使用null键和null值
  • 如果添加相同的key键,则会覆盖原来的key-value,等同于修改(key不会替换,value会替换)
  • 与HashSet一样,不保证映射的顺序,因为底层是以hash表的顺序来存储的。(JDK8的HashMap底层:数组+链表+红黑树)
  • HashMap没有实现同步,因此线程不安全,方法没有做同步互斥的操作,没有synchronized

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

用多少眼泪才能让你相信

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表