package com.model.map; import java.util.*; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/6/13 17:40 */ public class MapDemo01 { public static void main(String[] args) { /** * Map接口实现类的特点,使用实现类HashMap实例 * 1) Map与Collection并列存在。用于保存具有映射关系的数据:Key-Value * 2) Map中的key和value 可以是任何引用类型的数据,会封装到HashMap$Node对象中 Node静态内部类 * 3) Map中的key不允许重复,原因和HashSet一样,前面分析过源码. * 4) Map中的value可以重复 * 5) Map的key可以为null, value也可以为null ,注意key为null,只能有一个, * value为null ,可以多个. * 6)常用String类作为Map的key * 7) key和value之间存在单向一对一关系,即通过指定的 * key总能找到对应的value * * */ Map<Object, Object> hashMap = new HashMap<>(); hashMap.put("a", 1); hashMap.put("b", 2); hashMap.put("c", 1); hashMap.put("a", 2);//当有相同的key,会替换原来的的内容 hashMap.put("d", 1); System.out.println(hashMap.get("a")); } }
Map存放数据是Key-Value的,一对K-V是存放在Node中的,又因为Node实现了Entry接口,Entry存放在在EntrySet中,我们可以通过EntrySet的getKey(),getValue(),进行遍历 ,也可以通过hashMap.KeySet()和hashMap.values()的到EntrySet中所有的key的集合和所有Value的集合,EntrySet存放的Entry类型但是里存放的数据实际类型是HashMap$Node类型
package com.model.map; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/6/14 15:23 */ public class MapDemo02 { //Map存放的数据是Key-Value 的键值对,存放在Table表中的Node静态内部类中(HashMap$Node), //为了方便遍历,我们在内部将 存放数据的Node类,封装一个,转为 Entry接口(为什么能转,因为Node类实现了Entry接口中) //(并不是正真的复制Node,只是entry中的k指向Node中的key,entry中的V指向了Node中的Value,所以EntrySet中数据的类型还是HashMap$Node类型的数据) // static class Node<K,V> implements Map.Entry<K,V> { // final int hash; // final K key; // V value; // Node<K,V> next; // 并将Entry接口放在放在了EntrySet集合中:transient Set<Map.Entry<K,V>> entrySet; // 为什么要放在EntrySet中呢,为了方便遍历,EntrySet中由两个非常重要的方法 K getKey(); V getValue(); // keySet() ,Values() // public static void main(String[] args) { HashMap<Object, Object> hashMap = new HashMap<>(); hashMap.put("a", "a"); hashMap.put("b", "b"); Set<Map.Entry<Object, Object>> set = hashMap.entrySet(); for (Map.Entry entry:set){ System.out.println("EntrySet中存放放入是Entry,但是类型还是HashMap$Node类型"+entry.getClass()); System.out.println(entry.getKey()+"-"+entry.getValue()); } Collection<Object> values = hashMap.values(); //存放着table中所有的Value的Collection Set<Object> keySet = hashMap.keySet(); //存放者Table中所有的key 的set System.out.println(keySet+"-"+values); } }
Map体系继承图
Map接口的常用方法
Map接口的遍历方法
package com.model.map; import java.util.*; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/6/14 17:18 */ public class MapDemo04 { public static void main(String[] args) { HashMap<Object, Object> map = new HashMap<>(); for (int i = 0; i < 10; i++) { map.put(new Integer(i), new Integer(i)); } /** * 第一组:取出所有的key * keySet()方法 * */ Set<Object> keySet = map.keySet(); // (1)增强for循环 for (Object key:keySet){ System.out.println("增强for循环"+key+"-"+map.get(key)); } // (2)迭代器 Iterator<Object> iterator = keySet.iterator(); while (iterator.hasNext()) { Object next = iterator.next(); System.out.println("迭代器方式"+next+"-"+map.get(next)); } /** * * 第二组:取出所有的Values * values()方法 * */ Collection<Object> values = map.values(); //(1)增强for循环 for (Object o:values){ System.out.println("增强for:"+o); } //(2)迭代器 Iterator<Object> iterator1 = values.iterator(); while (iterator1.hasNext()) { Object next = iterator1.next(); System.out.println("迭代器:"+next); } /** * * 第三组:使用EntrySet,的getKey()和getValue()方法实现遍历 * */ Set<Map.Entry<Object, Object>> entrySet = map.entrySet(); // (1)增强for循环 for (Map.Entry entry:entrySet){ System.out.println(entry.getKey()+"-"+entry.getValue()); } // (2)迭代器 Iterator<Map.Entry<Object, Object>> iterator2 = entrySet.iterator(); while (iterator2.hasNext()) { Map.Entry<Object, Object> next = iterator2.next(); System.out.println(next.getKey()+""+next.getValue()); } } }
Map接口课堂练习
package com.model.map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/6/14 17:59 */ public class MapDemo05 { public static void main(String[] args) { Employee employee01 = new Employee("001", "a", 10000.0f); Employee employee02 = new Employee("002", "b", 20000.0f); Employee employee03 = new Employee("003", "c", 30000.0f); Map<Object, Object> hashMap = new HashMap<>(); hashMap.put(employee01.getId(),employee01); hashMap.put(employee02.getId(),employee02); hashMap.put(employee03.getId(),employee03); Set<Object> keySet = hashMap.keySet(); System.out.println("*********第一种*********"); for (Object key:keySet){ Employee employee = (Employee) hashMap.get(key); if (employee.getSal()>18000){ System.out.println(employee); } } System.out.println("*********第二种**********"); Iterator<Object> iterator = keySet.iterator(); while (iterator.hasNext()) { Object next = iterator.next(); Employee employee = (Employee) hashMap.get(next); if (employee.getSal()>18000){ System.out.println(employee); } } System.out.println("***********第三种**********"); Set<Map.Entry<Object, Object>> entrySet = hashMap.entrySet(); for (Map.Entry entry:entrySet){ Employee employee = (Employee) entry.getValue(); if (employee.getSal()>18000){ System.out.println(employee); } } System.out.println("***********第四种**********"); Iterator<Map.Entry<Object, Object>> iterator1 = entrySet.iterator(); while (iterator1.hasNext()) { Map.Entry<Object, Object> next = iterator1.next(); Employee employee = (Employee) next.getValue(); if (employee.getSal()>18000){ System.out.println(employee); } } /** * 通过得到所有的key (hashMap.KeySet()) * 通过得到所有的value (hashMao.Values()) * 通过的到所有的Entry<K,V> (Hash.entrySet()) * * */ } } class Employee{ private String id; private String name; private Float sal; @Override public String toString() { return "Employee{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", sal=" + sal + '}'; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Float getSal() { return sal; } public void setSal(Float sal) { this.sal = sal; } public Employee(String id, String name, Float sal) { this.id = id; this.name = name; this.sal = sal; } }
HashMap阶段小结