一、HashMap

底层:hash表实现(数组+链表+红黑树)当桶中的数据超过8个,把结构当前链表结构变为红黑树

初始容量:16

加载因子:0.75(当16*0.75达到临界点12时进行扩容)

扩容:扩容位桶的大小

HashMap:线程不安全,效率较高,可以存储null值

Hashtable:线程安全的hash表,不能存储null值

处理HashMap线程安全问题:

  • 可以使用Hashtable

  • 在Collections的提高了一个方法synchronizedMap(Map<K,V> m) 返回一个线程安全的map

  • juc包(java.util.concurrent )下ConcurrentHashMap是一个线程安全的HashMap(推荐使用,效率高)

/** 自定义简单实现HashMap*/
public class MyHashMap {Node[] table;//位桶数组int size;  //存储数据的个数public MyHashMap() {table = new Node[16]; //默认初始容量为16  length为2的整数幂}//添加putpublic void put(Object key,Object value){int hash=myHash(key.hashCode(),table.length);Node newNode=new Node(); //存储当前键值对的节点newNode.hash=hash;newNode.key=key;newNode.Value=value;newNode.next= null;Node node=table[hash];//hash是刚刚根据key计算出来的桶的位置|数组的索引//现在桶中没有节点数据,我当前节点newNode就作为第一个if(node==null){table[hash]=newNode;size++;}else{//1.拿到链表头节点  如果node不等于null,就作为链表头节点存在while(true){//如果当前key与桶中的已有节点key相等,value覆盖if(node.key.equals(key)){node.Value=value;break;}//判断当前是否就为最后一个节点if(node.next==null){node.next=newNode;size++;break;}//如果没有覆盖,继续从我这个节点找到我的下一个节点node=node.next; //node用于是当前要比较的桶中的数据节点}}}/** 根据key的hashcode()码计算hash值* 返回值:桶的位置* 参数1: key的hashcode()* 参数2: 数组的长度*/public int myHash(int keycode,int length){//System.out.println(keycode % length);//System.out.println(keycode & length-1);return keycode & length-1;}public static void main(String[] args) {MyHashMap my=new MyHashMap();System.out.println(my.size);my.put(10, "aa");my.put(20, "bb");my.put(30, "cc");System.out.println(my.size);my.put(40, "haha");System.out.println(my.size);my.put(40, "hehe");System.out.println(my.size);}
}

二、Collections

操作容器的工具类

        List al = new ArrayList();al.add(3);al.add(2);al.add(5);al.add(1);al.add(4);System.out.println(al);Collections.sort(al);//升序System.out.println(al);Collections.reverse(al);//反转System.out.println(al);Collections.shuffle(al);//随机System.out.println(al);Collections.fill(al, 1);//代替System.out.println(al);

三、比较器

1、内部比较器(自然排序)

实现一个Comparable的接口重写比较方法compareTo() 在方法内部定义默认比较规则,每次修改,都要修改源代码,硬编码;

public static void main(String[] args) {List<Student> al = new ArrayList();Student stu1 = new Student("徐小",16,155);Student stu2 = new Student("安楠",16,150);Student stu3 = new Student("贝娜",16,175);al.add(stu1);al.add(stu2);al.add(stu3);System.out.println(al);Collections.sort(al);System.out.println(al);System.out.println((int)'徐');System.out.println((int)'安');System.out.println((int)'贝');}
​
//身高降序
//   @Override
//   public int compareTo(Student o) {
//       return (int) (o.height-this.height);
//   }//名字升序@Override public int compareTo(Student o) { return this.name.compareTo(o.name); }

2、外部比较器(定制排序)

实现一个Comparator接口,重写compare(t1,t2) 方法中定义比较规则

public class ComparatorTest2 {public static void main(String[] args) {List<Student> al = new ArrayList();Student stu1 = new Student("徐小",16,155);Student stu2 = new Student("安楠",11,150);Student stu3 = new Student("贝娜",26,175);al.add(stu1);al.add(stu2);al.add(stu3);Collections.sort(al,new Compare());System.out.println(al);Collections.sort(al,new Compare2());System.out.println(al);
//      System.out.println((int)'徐');
//      System.out.println((int)'安');
//      System.out.println((int)'贝');}
}
//定义外部比较器
class Compare implements Comparator<Student>{//年龄降序@Overridepublic int compare(Student o1, Student o2) {// TODO Auto-generated method stubreturn o1.age-o2.age;}
}
class Compare2 implements Comparator<Student>{//姓名升序排序@Overridepublic int compare(Student o1, Student o2) {// TODO Auto-generated method stubreturn (int) (o2.height-o1.height);}
}

四、Lambda

1、匿名内部类

存在一个接口,对接口中的抽象方法进行重写,调用方法体实现功能

简化实现类:实现类本身没有自己的作用,只是为了重写抽象方法

Comparator<Student> em=new Emploee(){public int compare(Student o1, Student o2) {return o1.age-o2.age;}});

2、简化匿名内部类(Lambda)

java8提供了lambda表达式

使用前提:函数式接口

函数式接口: 只有一个必须要重写的抽象方法的接口

检查函数式接口:@FunctionalInterface

语法:

  • ()->{}

  • () :要重写的抽象方法的参数列表

  • -> :lambda符号,箭头符号,箭头函数,具有上下文推到作用

  • {} :定义抽象方法的方法体

//Lambda
Comparator<Student> em=(o1,o2)->o1.age-o2.age;
//方法引用
Comparator<Student> em=new Emploee()::compare;

HashMap、比较器与Lambda相关推荐

  1. HashMap 遍历hashMap的7种方法和删除数据两种方式

    遍历数据 1.使用Iterator 遍历HashMap EntrySet 2.使用Iterator 遍历HashMap KeySet 3.使用For-each EntrySet 循环迭代 HashMa ...

  2. 七种遍历HashMap的方法

    //7种遍历hashMap的方法 public class HashMapCurrent {public static void main(String[] args) {HashMap<Int ...

  3. 遍历 HashMap 的 5 种最佳方式

    1. 使用 Iterator 遍历 HashMap EntrySet 2. 使用 Iterator 遍历 HashMap KeySet 3. 使用 For-each 循环遍历 HashMap 4. 使 ...

  4. HashMap遍历的五种方法

    在本文中,我们将通过示例讨论在 Java 上遍历 HashMap 的五种最佳方法. 1.使用 Iterator 遍历 HashMap EntrySet 2.使用 Iterator 遍历 HashMap ...

  5. 遍历hashMap的5种方法

    1.使用Iterator 遍历HashMap EntrySet 2.使用Iterator 遍历HashMap KeySet 3.使用For-each 循环迭代 HashMap 4.使用Lambda 表 ...

  6. 按频率对元素进行排序

    Prerequisite: 先决条件: Hashing data structure 散列数据结构 How to write user-defined comparator for priority ...

  7. FileIO - java

    –File– 概述 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 构造方法 public File(String pathname) :通过将 ...

  8. 7月20HashMap、集合、HashSet

    7月20HashMap.集合.HashSet 1.List和Set区别 List : 有序的,可重复的 * 新增了一些根据索引操作的方法 * ArrayList * Vector * LinkedLi ...

  9. 【Java】流(Stream)快速入门

    本文是面向Java初学者的流(Stream)学习指导教程.文章内容偏向怎么用,而弱化其诞生背景.流的概念.内部原理等说明解释性的语段. 主要内容: Optional 创建流 操作流 收集流 目录 什么 ...

最新文章

  1. 4_Shell语言———脚本概述
  2. pycharm ubuntu 安装_Nvidia Jetson Xavier 安装配置(ubuntu 18.04)
  3. php字符集转换,php字符集转换
  4. Js中RegExp对象
  5. tcpdf html 支持css吗,TCPDF 5.1 发布,增加对CSS的支持
  6. JPress v2.0-rc.6 发布,新增 Java 插件的在线升级功能
  7. 通达OA破解版下载|通达OA2015破解版 可用|通达oa2015破解补丁
  8. 【DS】数据结构八股文英文版(1)
  9. QQ互联本地测试【QQ互联第一步】
  10. Js Switch语句
  11. 有参构造方法的作用和无参构造方法的作用
  12. GPU编程 CUDA C++ 使用统一内存编程之【静态统一内存】
  13. Android Camera2教程之打开相机、开启预览、实现PreviewCallback、拍照
  14. 超级计算机神威太湖之光图片,超级计算机“神威·太湖之光”世界最快
  15. 聚类分析详细解读python
  16. matlab汽车驱动力与行驶阻力,用matlab绘制汽车驱动力-行驶阻力平衡图
  17. 第三方风控的窘境:赚钱不易,生存更难
  18. 苹果id登录_苹果服务器挂了...ID 登录不了!
  19. 极路由3 = B50
  20. python语言中ch_已知在Python语言中 upper() 函数可以将英文小写字母转化为大写字母,例如: 'cpda'.upper() CPDA 则下列语句的输出结果为 CHINACPD...

热门文章

  1. 编辑写三个jsp页面,include动作标记的用法
  2. Java实现 LeetCode 646 最长数对链(暴力)
  3. java 数组 移动位置_将数组元素从一个数组位置移动到另一数组位置
  4. 数值积分21 - Gass 高斯求积公式 Legendre勒让得多项式 高斯-勒让得求积公式
  5. CCF x Jina AI:1024 中国工程师文化日全议程
  6. 【python爬虫】爬取ajax数据-马蜂窝旅游网
  7. Qt 重写窗口关闭按钮事件
  8. 前端笔试题2019-玄武科技
  9. Tomcat 结合Atomikos 实现JTA
  10. 医学影像中呼吸门控的动态显示