• JAVA-Map接口概述、常用方法、排序、Hashtable面试题


    ①. Map接口的概述

    • ①. Map是双列集合与Collection的单列集合并列存在

    • ②. Map  这里的K 、V 都应该是引用数据类型

    • ③. HashSet底层依赖HashMap,单列底层依赖双列集合

    • ④. Map中的key可以为null,value也可以为null,注意key只能有一个null,value可以有多个null

    ②. Map常用方法

    • ①. 添加功能:如果键是第一次储存,就直接储存元素,返回null;如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值

      V put(K key ,V value):添加元素,返回的是以前的值

    1. "prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Map map=new HashMap<>();
    2. Integer i1=map.put("张三",23);
    3. Integer i2=map.put("李四",23);
    4. Integer i3=map.put("王五",24);
    5. Integer i4=map.put("赵六",25);
    6. Integer i5=map.put("张三",26);//相同的键不储存,把原来的值覆盖 把被覆盖的值返回
    7. System.out.println(map);//{李四=23, 张三=26, 王五=24, 赵六=25}
    8. //这里的i1--i4 都是null是因为开始的时候[张三,23][李四,23]....
    9. // 在集合中没有,这样把原来的值覆盖了,返回的是原来的值
    10. System.out.println(i1);//null
    11. System.out.println(i2);//null
    12. System.out.println(i3);//null
    13. System.out.println(i4);//null
    14. System.out.println(i5);//23
    • ②. 删除功能
    1. void clear():移除所有的键值对元素
    2. V remove(Object key):根据键删除键值对元素,并把值返回
    1. "prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Map map=new HashMap<>();
    2. map.put("张三",23);
    3. map.put("李四",23);
    4. map.put("王五",24);
    5. map.put("赵六",25);
    6. //根据键删除元素,返回键对应的值
    7. Integer value=map.remove("张三");
    8. System.out.println(value);//23
  1. boolean containsKey(Objecct key):判断是否包含指定的键
  2. boolean containsValue(Objecct value):判断是否包含指定的值
  1. "prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//判断是否包含传入的键
  2. System.out.println(map.containsKey("张三"));//true
  3. //判断是否包含传入的值
  4. System.out.println(map.containsValue(23));//true
  1. "prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Map map=new HashMap<>();
  2. map.put("张三",26);
  3. map.put("李四",23);
  4. map.put("王五",24);
  5. map.put("赵六",25);
  6. Collectioncoll=map.values();
  7. System.out.println(coll);//[23, 23, 24, 25]
  8. System.out.println(map.size());//4
  1. V get(Object key):根据键获取值

  2. Set  keySet():获取集合中所有的键集合

  3. Collection  values:获取集合中所有值集合

  4. Set> entrySet():拿到所有的键值对对象

    K getKey():得到entrySet中的键

    V getValue():得到entrySet中的值

  1. class="prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Map<String, Integer> map = new HashMap<>();
  2. map.put("张三", 23);
  3. map.put("李四", 24);
  4. map.put("王五", 25);
  5. map.put("赵六", 23);
  1. class="prettyprint hljs swift" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//V get(Object key):根据键获取值
  2. Integer i = map.get("张三");//26
  3. Integer i2=map.get("小智");//没有的话返回null
  4. //1.获取所有的键
  5. Set keySet = map.keySet();
  6. //iterator遍历
  7. Iterator iterator = keySet.iterator();
  8. while (iterator.hasNext()) {
  9. String key = iterator.next();//获取每一个键
  10. Integer value = map.get(key); //根据键获取值
  11. System.out.println(key + "=" + value);
  12. }
  13. System.out.println("--------------");
  14. //使用增强for循环
  15. for (String key : map.keySet()) {
  16. Intger value=map.get(key);
  17. System.out.println(key + "=" +value );
  18. }

  1. class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">interface  Inter{ 
  2. interface Inter2{
  3. public void show();
  4. }
  5. }
  6. //这里的Inter.Inter2 和Map.Entry一样的
  7. class Demo implements Inter.Inter2{
  8. @Override
  9. public void show() {
  10. }
  11. }
  1. class="prettyprint hljs dart" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//2.根据键值对对象,获取键和值
  2. //Map.Entry说明Entry是Map的内部接口,将键和值封装成了Entry对象,并储存在Set集合中
  3. Set<Map.Entry<String,Integer>>entrySet=map.entrySet();
  4. //获取每一个对象
  5. Iterator<Map.Entry<String,Integer>>it=entrySet.iterator();
  6. while(it.hasNext()){
  7. //获取每一个Entry对象
  8. //static class Entry implements Map.Entry{}
  9. Map.Entry<String,Integer>en=it.next();//父类引用指向子类对象
  10. //Entryen=it.next(); 子类对象
  11. //根据键值对对象获取键
  12. String key=en.getKey();
  13. //根据键值对对象获取值
  14. Integer value=en.getValue();
  15. System.out.println(key+"="+value);
  16. }
  17. //增强for循环
  18. for(Map.Entry<String,Integer> en:map.entrySet()){
  19. System.out.println(en.getKey()+"="+en.getValue());
  20. }
  21. for(Entry<String,Integer> en:map.entrySet()){
  22. System.out.println(en.getKey()+"="+en.getValue());
  23. }

③. HashMap概述

  1. class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">// HashMap集合键是Student值是String的案例
  2. public class Student {
  3. private String name;
  4. private int age;
  5. public Student() {
  6. }
  7. public Student(String name, int age) {
  8. this.name = name;
  9. this.age = age;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. @Override
  24. public boolean equals(Object o) {
  25. if (this == o) return true;
  26. if (o == null || getClass() != o.getClass()) return false;
  27. Student student = (Student) o;
  28. return age == student.age &&
  29. Objects.equals(name, student.name);
  30. }
  31. @Override
  32. public int hashCode() {
  33. return Objects.hash(name, age);
  34. }
  35. @Override
  36. public String toString() {
  37. return "Student{" +
  38. "name='" + name + '\'' +
  39. ", age=" + age +
  40. '}';
  41. }
  42. }
  1. class="prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">HashMap<Student,String>hm=new HashMap<>();
  2. hm.put(new Student("张三",23),"北京");
  3. String i=hm.put(new Student("张三",23),"上海");
  4. hm.put(new Student("李四",24),"广州");
  5. hm.put(new Student("王五",25),"深圳");
  6. System.out.println(i);
  7. //没有重写equals方法之前,调用的是Object类的,会比较地址值
  8. //没有重写equals方法和 hashCode方法的时候map中的元素有4个,重写后只有3个
  9. //注意这里的张三对应的值是上海,因为把北京覆盖了
  10. System.out.println(hm);//北京
  11. //{Student{name='张三', age=23}=上海, Student{name='李四', age=24}=广州, Student{name='王五',

④. LinkedHashMap

  1. class="prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">LinkedHashMap<String,Integer>lhm=new LinkedHashMap<>();
  2. lhm.put("张三",23);
  3. lhm.put("李四",24);
  4. lhm.put("王五",25);
  5. lhm.put("赵六",26);
  6. System.out.println(lhm);//{张三=23, 李四=24, 王五=25, 赵六=26}

⑤. TreeMap排序

  1. class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//自然排序
  2. public class Student implements Comparable<Student> {
  3. private String name;
  4. private int age;
  5. public Student() {
  6. }
  7. public Student(String name, int age) {
  8. this.name = name;
  9. this.age = age;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Student{" +
  26. "name='" + name + '\'' +
  27. ", age=" + age +
  28. '}';
  29. }
  30. @Override
  31. public int compareTo(Student s) {
  32. //以年龄为主要条件,姓名为次要条件
  33. int num=this.age-s.age;
  34. return num==0?this.name.compareTo(s.name):num;
  35. }
  36. }
  1. class="prettyprint hljs tcl" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">TreeMap<Student,String> tm=new TreeMap<>();
  2. tm.put(new Student("张三",23),"北京");
  3. tm.put(new Student("李四",13),"上海");
  4. tm.put(new Student("王五",33),"广州");
  5. tm.put(new Student("赵六",43),"北京");
  6. System.out.println(tm);
  7. //{Student{name='李四', age=13}=上海, Student{name='张三', age=23}=北京,
  8. // Student{name='王五', age=33}=广州, Student{name='赵六', age=43}=北京}
  1. class="prettyprint hljs dart" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//选择器排序
  2. TreeMap tm=new TreeMap<>(new Comparator() {
  3. @Override
  4. public int compare(Student s1, Student s2) {
  5. //按照姓名比较
  6. int num=s1.getName().compareTo(s2.getName());
  7. return num==0?s1.getAge()-s2.getAge():num;
  8. }
  9. });
  10. tm.put(new Student("张三",23),"北京");
  11. tm.put(new Student("李四",13),"上海");
  12. tm.put(new Student("王五",33),"广州");
  13. tm.put(new Student("赵六",43),"北京");
  14. System.out.println(tm);
  15. //{Student{name='张三', age=23}=北京, Student{name='李四', age=13}=上海,
  16. //Student{name='王五', age=33}=广州, Student{name='赵六', age=43}=北京}

⑥. Hashtable面试题

  1. class="prettyprint hljs yaml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">HashMapHashtable的区别
  2. 共同点:
  3. 底层都是哈希算法,都是双列集合
  4. 区别
  5. 1.HashMap是线程不安全的,效率高,jdk1.2版本
  6. Hashtable是现场安全的,效率低,jdk1.0版本
  7. 2.HashMap可以存储null键和null
  8. Hashtable不可以存储null键和null
  • 相关阅读:
    虹科Pico动态 |【盖世汽车-走进东风商用车技术展】精彩回顾
    数据分享 I 地级市人口和土地使用面积基本情况
    JAVA_SSM+VUE校园二手物品交易平台(含论文)源码
    电压频率的变换原理
    公司电脑文件加密防泄密软件系统——「天锐绿盾」
    Linux内核中ideapad-laptop.c文件全解析2
    电气无线测温系统在设备监测中的作用
    10月26日,每日信息差
    云计算之分布式计算
    Java 常用API的运用,效率及技巧
  • 原文地址:https://blog.csdn.net/weixin_62421895/article/details/126174624