我是靠谱客的博主 野性手链,这篇文章主要介绍HashMap、比较器与Lambda,现在分享给大家,希望可以做个参考。

一、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(推荐使用,效率高)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* * 自定义简单实现HashMap */ public class MyHashMap { Node[] table;//位桶数组 int size;  //存储数据的个数 public MyHashMap() { table = new Node[16]; //默认初始容量为16 length为2的整数幂 } //添加put public 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

操作容器的工具类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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() 在方法内部定义默认比较规则,每次修改,都要修改源代码,硬编码;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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) 方法中定义比较规则

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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>{ //年龄降序 @Override public int compare(Student o1, Student o2) { // TODO Auto-generated method stub return o1.age-o2.age; } } class Compare2 implements Comparator<Student>{ //姓名升序排序 @Override public int compare(Student o1, Student o2) { // TODO Auto-generated method stub return (int) (o2.height-o1.height); } }

四、Lambda

1、匿名内部类

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

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

复制代码
1
2
3
4
5
Comparator<Student> em=new Emploee(){ public int compare(Student o1, Student o2) { return o1.age-o2.age; } });

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

java8提供了lambda表达式

使用前提:函数式接口

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

检查函数式接口:@FunctionalInterface

语法:

  • ()->{}

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

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

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

复制代码
1
2
3
4
//Lambda Comparator<Student> em=(o1,o2)->o1.age-o2.age; //方法引用 Comparator<Student> em=new Emploee()::compare;

最后

以上就是野性手链最近收集整理的关于HashMap、比较器与Lambda的全部内容,更多相关HashMap、比较器与Lambda内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部