ToB企服应用市场:ToB评测及商务社交产业平台

标题: 8-Map接口和常用方法 [打印本页]

作者: 慢吞云雾缓吐愁    时间: 2024-7-12 22:38
标题: 8-Map接口和常用方法
8-Map接口和常用方法


注意:遍历存储的数据,本质上通过调用 HashIterator 迭代器进行的,并且 KeySet、Values、EntrySet 对象中并不存放存储的数据。另有就是 HashMap$Node 继续 Map.Entry ,这一点要注意。另有就是肯定要注意 EntrySet 集合,是通过 entrySet 方法初始化的。另有 HashMap 中的 toString (继续父类)会调用 entrySet . iterator 方法进行获取存储的数据,这也是为啥在 IDEA 中 Debug 中,发现并未调用 entrySet 方法就会发现 EntrySet 集合会被初始化了,并且初始化了其中还包括数据,这是因为  IDEA 中 Debug 过程会调用 HashMap 中的 toString 方法,进行获取其中内容,以是  EntrySet 集合会被初始化及赋值
  1. package map;
  2. import java.lang.reflect.Field;
  3. import java.util.HashMap;
  4. public class MapEntrySet {
  5.     @SuppressWarnings({"all"})
  6.     public static void main(String[] args) {
  7.         HashMap hashMap = new HashMap();
  8.         Field entrySetField = null ;
  9.         try {
  10.             // 通过反射机制获取 HashMap 中的 entry 属性成员(Set<Map.Entry<K,V>> 类型)
  11.             entrySetField = HashMap.class.getDeclaredField("entrySet");
  12.             // 开启 entry 属性成员的访问权限
  13.             entrySetField.setAccessible(true);
  14.             // 从 HashMap 对象中获取 entry 属性成员
  15.             Object entrySet = entrySetField.get(hashMap);
  16.             // 打印该属性成员,查看属性成员是否为空
  17.             System.out.println("entrySet=" + entrySet);
  18.             // 模拟 IDEA 的 Debug 过程中调用 toString 方法获取数据(检验 entrySet 属性是否被初始化)
  19.             System.out.println("hashMap.toString" + hashMap.toString());
  20.             // 从 HashMap 对象中获取 entry 属性成员
  21.             entrySet = entrySetField.get(hashMap);
  22.             // 打印该属性成员,查看属性成员是否为空
  23.             System.out.println("entrySet=" + entrySet);
  24.         } catch (NoSuchFieldException e) {
  25.             throw new RuntimeException(e);
  26.         } catch (IllegalAccessException e) {
  27.             throw new RuntimeException(e);
  28.         }
  29.     }
  30. }
复制代码
  1. package map;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import java.util.Set;
  6. public class HashMapExercise {
  7.     @SuppressWarnings({"all"})
  8.     public static void main(String[] args) {
  9.         HashMap hashMap = new HashMap();
  10.         hashMap.put(1,new Employee02("吴彦祖",20000,1)) ;
  11.         hashMap.put(2,new Employee02("彭于晏",15000,2)) ;
  12.         hashMap.put(3,new Employee02("杨洋",28000,3)) ;
  13.         // Set<Map.Entry<K,V>> 遍历
  14.         Set entrySet = hashMap.entrySet();
  15.         Iterator iterator = entrySet.iterator();
  16.         while (iterator.hasNext()) {
  17.             Map.Entry entry =  (Map.Entry) iterator.next();
  18.             Employee02 entryValue = (Employee02) entry.getValue();
  19.             if (entryValue.getSalary() > 18000) {
  20.                 System.out.println("====工资大于 18000 的员工信息为" + entryValue);
  21.             }
  22.         }
  23.         // keySet 遍历
  24.         Set keySet = hashMap.keySet();
  25.         for (Object object : keySet) {
  26.             Employee02 employee02 = (Employee02) hashMap.get(object);
  27.             if (employee02.getSalary() >18000) {
  28.                 System.out.println("====工资大于 18000 的员工信息为" + employee02);
  29.             }
  30.         }
  31.     }
  32. }
  33. @SuppressWarnings({"all"})
  34. class Employee02 {
  35.     private String name ;
  36.     private double salary ;
  37.     private int id ;
  38.     @Override
  39.     public String toString() {
  40.         return "Employee02{" +
  41.                 "name='" + name + '\'' +
  42.                 ", salary=" + salary +
  43.                 ", id=" + id +
  44.                 '}';
  45.     }
  46.     public Employee02(String name, double salary, int id) {
  47.         this.name = name;
  48.         this.salary = salary;
  49.         this.id = id;
  50.     }
  51.     public String getName() {
  52.         return name;
  53.     }
  54.     public void setName(String name) {
  55.         this.name = name;
  56.     }
  57.     public double getSalary() {
  58.         return salary;
  59.     }
  60.     public void setSalary(double salary) {
  61.         this.salary = salary;
  62.     }
  63.     public int getId() {
  64.         return id;
  65.     }
  66.     public void setId(int id) {
  67.         this.id = id;
  68.     }
  69. }
复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4