JavaSE-3进阶API以及一些刷题语法

JavaSE-3进阶API以及一些刷题语法

1. 常用API

1.1 Object

image-20221231231933179 image-20221231231957223
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.curious.javase.arraylist;

import java.util.ArrayList;

public class Test {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>();
nums.add(1);
nums.add(2);

ArrayList<Integer> nums2 = new ArrayList<>();
nums2.add(1);
nums2.add(2);

System.out.println(nums.toString()); // [1, 2]
System.out.println(nums.equals(nums2)); // true
}
}

1.1.1 toString()方法(类中重写比较好使)

1
2
3
4
5
6
7
8
9
10
11
package com.curious.javasepro.d9_api_object;

public class Test1 {
public static void main(String[] args) {
Student stu = new Student("Curious");
//
System.out.println(stu.toString());
// 直接输出也是一样的
System.out.println(stu);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.curious.javasepro.d9_api_object;

public class Student {
private String name;

Student() {}

Student(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
image-20221231232300742 image-20221231232314608
  • 在Student中重写
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.curious.javasepro.d9_api_object;

public class Student {
private String name;

Student() {}

Student(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Student {name: " + name + '}';
}
}

1.1.2 equals()方法(可以用generate自动生成的)

image-20221231232821152

equals和上面的差不多,默认是比较两个对象的地址是否相同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.curious.javasepro.d9_api_object;

public class Test1 {
public static void main(String[] args) {
Student stu = new Student("Curious");
//
System.out.println(stu.toString());
// 直接输出也是一样的
System.out.println(stu);

Student stu2= new Student("Curious");
System.out.println("stu.equals(stu2): " + stu.equals(stu2)); // false

}
}

但是如果比较地址相同的话,完全可以用==来比的,所以这里提供的equals同样是为了重写的

这里是多态的一个使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
public boolean equals(Object o) {
// 1. 判断o是不是学生类型
if (o instanceof Student) {
if (this.name == ((Student) o).name) {
return true;
} else {
return false;
}
} else {
return false;
}
}

官方的

1
2
3
4
5
6
7
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(name, student.name);
}

1.2 Objects

image-20221231234436064

1.2.1 equals()

image-20221231233901087 image-20221231233933456

上面那个Objects.equals,这块避免了null.equals()这样的操作

如下面这样,可能存在问题

1
2
3
4
5
6
7
8
9
package com.curious.javasepro.d10_api_object;

public class Test {
public static void main(String[] args) {
String s1 = null;
String s2 = new String("Curious");
System.out.println(s1.equals(s2));
}
}

所以进行一个修改

1
2
3
4
5
6
7
8
9
10
11
12
package com.curious.javasepro.d10_api_object;

import java.util.Objects;

public class Test {
public static void main(String[] args) {
String s1 = null;
String s2 = new String("Curious");
// System.out.println(s1.equals(s2));
System.out.println(Objects.equals(s1, s2)); // 安全的
}
}

看源代码的话,有这样的操作(a != null && a.equals(b)),有熔断操作

1.2.2 isNull

1
2
System.out.println(Objects.isNull(s1));
System.out.println(s1 == null);

1.3 StringBuilder(刷题重要)

image-20221231234826852 image-20221231234840663

1.3.1 StringBuilder常用方法

image-20221231234939550

目标:学会使用StringBuilder,并知道性能好的原因

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.curious.javasepro.d11_api_stringbuilder;

/**
* 目标:学会使用
*/
public class StringBuilderDemo01 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("a");
sb.append("b");
sb.append(1);
sb.append(false);
sb.append(3.3);
System.out.println(sb.toString()); // ab1false3.3
System.out.println(sb.reverse()); // 无返回值,但是会翻转了 3.3eslaf1ba
System.out.println(sb.length()); // 11

}
}

1.4 Math

执行基本的数学运算,没有提供公开的构造器,一种工具类

image-20230101102727392

这里的Math.pow根据chatGPT的搜索结果,是快速幂算法

1.5 System

1.6 BigDecimal(用于解决浮点型运算精度失真的问题)

一些刷题的时候经常被这个卡了,浮点数精度

image-20230101160655914 image-20230101161302182 image-20230101161353202
1
2
3
4
5
6
BigDecimal bd1 = BigDecimal.valueOf(0.1);
BigDecimal bd2 = BigDecimal.valueOf(0.2);
BigDecimal ress = bd1.add(bd2);
System.out.println("ress: " + ress);
System.out.println(ress.getClass().getName()); // BigDecimal
System.out.println(ress.doubleValue()); // 转成double类型了

1.7 BigInteger(大数操作,一些大数加法问题,不过题目也会避免)

这个就是大数操作,基本都是用字符串来实现的

1
2
3
BigInteger a = new BigInteger("9999999999999999999999999999");
BigInteger b = new BigInteger("9999999999999999999999999999");
System.out.println("a.add(b).toString(): " + a.add(b).toString());

2. 一批新的API介绍(日期与时间)

image-20230101163023319

2.1 日期与时间(大概了解,用的时候会查就行)

2.1.1 Date类

image-20230101163523097 image-20230101163530970
1
2
3
4
5
6
7
8
9
10
11
package com.curious.javasepro.d12_useful_api;

import java.util.Date;

public class Test01 {
public static void main(String[] args) {
Date d = new Date();
System.out.println(d); // Sun Jan 01 16:34:58 CST 2023
System.out.println(d.getTime()); // 获取时间毫秒值 1672562137795
}
}
image-20230101163636755
1
2
3
4
5
// 1. 得到当前时间的事件毫秒值
long time2 = System.currentTimeMillis();
time2 += (60 * 60 + 121) * 1000;
Date d2 = new Date(time2);
System.out.println(d2); // 当前时间往后走一些时间

2.1.2 SimpleDateFormat

image-20230101163929489 image-20230101164013551 image-20230101164251001

2.1.3 Calendar概述

image-20230101165248519

2.1.4 JDK8开始新增日期API

image-20230101165902222 image-20230101165928657 image-20230101170440522

3. 包装类(Integer、Character、Boolean等等)

image-20230101171029863 image-20230101171043062

Java:一切皆对象

1
2
int a = 10;
Integer a1 = 11;
image-20230101171204722
1
2
3
Integer t1 = 1;
int t2 = 100;
System.out.println(t1+t2); // 101

4. 正则表达式(暂时了解就可以,真正用到可以搜索使用)

4.1 matches,返回boolean

image-20230101171912182
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.curious.javasepro.d13_regular_expression;

public class Test {
public static void main(String[] args) {
String qq = "123456789";
boolean flag = qq.matches(qq);
System.out.println(flag); // true
}

public static boolean checkQQ(String qq) {
return qq != null && qq.matches("\\d{6, 20}");
}
}

4.2 编译后匹配

image-20230101174353935

5. Arrays(重点)

image-20230101174426604
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.curious.javasepro.d14_arrays;

import java.util.Arrays;

public class ArraysDemo1 {
public static void main(String[] args) {
// 目标:学会使用Arrays类的常用API,并理解其原理
int[] arr = {10, 2, 55, 23, 24, 100};
System.out.println(arr);
System.out.println(Arrays.toString(arr)); // 1. 返回数组内容

// 2. 排序API,默认对数组进行升序排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));

// 3. 二分搜索基础(前提是数组必须排序好,否则出问题)
int index = Arrays.binarySearch(arr, 2); // 如果有返回index,负数说明不存在
}
}

5.1 Arrays类对于Comparator的支持

5.1.1 Integer[]进行降序排序(支持引用类型)

image-20230101175134477
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
package com.curious.javasepro.d14_arrays;

import java.util.Arrays;
import java.util.Comparator;

public class ArraysDemo2 {
public static void main(String[] args) {
// 目标:自定义数组的排序规则:Comparator比较器对象
// 1. Arrays的sort方法对于有值特性的数组是默认升序排序
int[] nums = {34, 12, 42, 23};
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));

// 2. 需求:降序排序(自定义比较器对象,只能支持引用类型的排序)
Integer[] nums2 = {34, 12, 42, 23};
/**
* 参数一:被排序的数组,必须是引用类型
* 参数二:匿名内部类的对象,代表了一个比较器对象
*/
Arrays.sort(nums2, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// 指定比较规则,注意返回值是int
// if (o1 > o2) {
// return 1;
// } else if (o1 < o2) {
// return -1;
// }
// return 0;
// return o1 - o2;
return o2 - o1;
}
});
System.out.println(Arrays.toString(nums2));
}
}

这里有问题是,必须得转成引用类型?那么平常的int是咋排序啊

5.1.2 对学生类进行定义条件的排序(double String也进入比较了)

image-20230101180455822

字符串的的话,用compareTo

image-20230101180724771

1
2
3
String a1 = "1234";
String a2 = "2345";
a1.compareTo(a2);

6. Lambda表达式

  • 作用:简化匿名内部类的代码写法
image-20230101181341596
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
package com.curious.javasepro.d15_lambda;

public class LambdaDemo2 {
public static void main(String[] args) {
// 目标:学会使用Lambda的标准格式简化匿名内部类的代码形式
// Lambda只能简化接口中只有一个抽象方法的匿名内部类形式

// 复杂版本
Swimming s1 = new Swimming() {
@Override
public void swim() {
System.out.println("游泳游的很好");
}
};

// 简化版本
Swimming s2 = () -> {
System.out.println("游游泳的很好");
};

}

public static void go(Swimming s) {
System.out.println("开始。。。");
s.swim();
System.out.println("结束。。。");

}
}

@FunctionalInterface // 一旦加上接口,必须是函数式接口,只能有一个抽象方法
interface Swimming {
void swim();
}

6.1 lambda表达式简化Comparator接口的匿名形式

image-20230101182010789
1
2
3
4
5
Integer[] nums = {5, 4, 3, 2, 1};
Arrays.sort(nums, (Integer o1, Integer o2) -> {
return o1 - o2;
});
System.out.println(Arrays.toString(nums));

进一步简化

1
2
3
4
5
Integer[] nums = {5, 4, 3, 2, 1};
Arrays.sort(nums, (o1, o2) -> {
return o1 - o2;
});
System.out.println(Arrays.toString(nums));

7. 集合(Collections相较于List等集合,通用性更高->方法更少,是上层)

集合和数组都是容器,但是集合不定长一些特点,都让实际场景的集合应用更加广泛一些

image-20230101203914844

接口被类实现!

7.1 Collection集合的体系特点

image-20230101201242918 image-20230101201335541 image-20230101201536482

7.2 Collection集合常用API(Collection感觉只有几个,而List更多一些?)

关键:https://blog.csdn.net/HanTangSongMing/article/details/27080151

这里发现,使用Collection的,不能用.get,但是前后都是ArrayList的就可以,不知道这个地方是什么原理

7.2.1 QA补充(chatGPT,重要)

  • Collections<Integer> c = new ArrayList<>()ArrayList<Integer> c = new ArrayList<>()的区别
image-20230101203440283

7.2.2 QA补充2(chatGPT)

image-20230101211218290

7.2.3 QA补充3(关键!!!)

image-20230101211410795

ArrayList对象实现了Collection接口!!!!方法是从接口中获取的方法!!!!

7.3 Collection集合的遍历方式

7.3.1 迭代器(while循环,it.hasNext(),it.next())

image-20230101204146295 image-20230101204157673
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
package com.curious.javasepro.d16_collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionDemo2 {
public static void main(String[] args) {
Collection<Integer> c = new ArrayList<>();
c.add(1);
c.add(2);
c.add(3);
c.add(4);

// 1. 得到当前集合的迭代器对象
Iterator<Integer> it = c.iterator();
// int a = it.next(); // 1,单独走一个
// System.out.println(a);

// 2. 定义while循环,遍历集合
while (it.hasNext()) {
int tmp = it.next();
System.out.println(tmp);
} // 1 2 3 4都会一个个的输出
}
}

7.3.2 foreach/增强for循环(简单很多)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.curious.javasepro.d16_collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionDemo2 {
public static void main(String[] args) {
Collection<Integer> c = new ArrayList<>();
c.add(1);
c.add(2);
c.add(3);
c.add(4);

for (Integer a: c) {
System.out.println(a);
}
}
}

循环的过程中,修改没有作用!!修改无意义,和python那个很像

7.3.3 lambda表达式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.curious.javasepro.d16_collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Consumer;

public class CollectionDemo2 {
public static void main(String[] args) {
Collection<Integer> c = new ArrayList<>();
c.add(1);
c.add(2);
c.add(3);
c.add(4);

c.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
System.out.println(integer);
}
});
}
}

8. 集合(List集合系列,Collection的子接口)

image-20230101201335541

List -> ArrayList<>()和LinkedList()

image-20230101212156627

ArrayList就是数组,查询更快,但是插入删除比较慢

LinkedList就是链表,插入删除更快,但是查询比较慢

8.1 ArrayList

经典代码,多态写法!!!!!

1
List<String> list = new ArrayList<>();

8.1.1 转换为数组(chatGPT yyds)

要将一个 List<String> 转化为一个 String 类型的数组,可以使用 List 类的 toArray() 方法,它返回一个包含列表中所有元素的数组。

1
2
3
4
5
6
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");

String[] array = list.toArray(new String[0]);

上面的代码会将 list 中的元素转化为一个 String 类型的数组。

注意:toArray() 方法的参数是一个指定类型的数组,并且返回的是指定类型的数组。因此,你需要传入一个空的指定类型的数组,如 new String[0],以便返回一个指定类型的数组。

1
2
3
4
5
6
7
8
List<String> list = new ArrayList<>();
list.add("AAA");
list.add("BBB");
list.add("CCC");
String[] res = list.toArray(new String[0]);

System.out.println(res[0]);
System.out.println(Arrays.toString(res));

8.2 LinkedList(有一些独有的,可以完成队列和栈,独有功能不能用多态写)

1
List<String> list = new LinkedList<>();
image-20230101213644631

这里更有一些新的,但是必须得不能用多态写

1
LinkedList<String> list = new LinkedList<>();

8.2.1 实现stack

image-20230101214314131

8.2.2 实现queue

image-20230101214326719

8.3 List集合的遍历方式小结

1
2
3
4
5
6
7
8
List<String> list = new ArrayList<>();
list.add("AAA");
list.add("BBB");
list.add("CCC");

for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}

8.4 遍历删除(迭代器用it删,for就从后往前删)

image-20230101215031786

9. 泛型深入(未来还要进一步随着项目增强)

9.1 自定义泛型类MyArrayList

1
2
3
public class MyArrayList<E> {
xxx
}
image-20230101220418690

拓展,做功能(偷鸡哈哈哈哈哈)

image-20230101220630097

9.2 自定义泛型方法

image-20230101220856448 image-20230101220906235 image-20230101221119526

9.3 泛型接口

image-20230101221758375 image-20230101221810061 image-20230101221851869 image-20230101221939178

10. 集合(Set)

10.1 Set系列概述

image-20230101201335541

这个set和python那么理解就行了,LinkedHashSet是有序的

  • 家族类特点
image-20230101223350369
  • 实现类特点
image-20230101223422556

10.1.1 HashSet的一个例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.curious.javasepro.d17_set;

import java.util.HashSet;
import java.util.Set;

public class SetTest1 {
public static void main(String[] args) {
Set<Integer> st = new HashSet<>();
st.add(1);
st.add(1);
st.add(2);
System.out.println(st.toString());
for(Integer a: st) {
System.out.println(a);
}
}
}

10.1.2 补充:Set转数组

image-20230101224652636

10.1.3 补充:Set转List

1
2
3
4
5
6
Set<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("orange");

List<String> list = new ArrayList<>(set);

10.2 Set集合去重复(Student类)

  • Student类,全都是自动生成的
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
package com.curious.javasepro.d17_set;

import java.util.Objects;

public class Student {
private String name;
private int age;
private char sex;

Student() {

}

public Student(String name, int age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public char getSex() {
return sex;
}

public void setSex(char sex) {
this.sex = sex;
}

@Override
public String toString() {
return "Student{" + "name='" + name + '\'' + ", age=" + age + ", sex=" + sex + '}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && sex == student.sex && Objects.equals(name, student.name);
}

@Override
public int hashCode() {
return Objects.hash(name, age, sex);
}
}
  • 主类Test类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.curious.javasepro.d17_set;

import java.util.HashSet;
import java.util.Set;

public class SetDemo3 {
public static void main(String[] args) {
Student s1 = new Student("A", 20, '男');
Student s2 = new Student("A", 20, '男');
Student s3 = new Student("B", 21, '女');

Set<Student> st = new HashSet<>();
st.add(s1);
st.add(s2);
st.add(s3);

for (Student stu: st) {
System.out.println(stu); // 这里自动调用了toString方法
}
}
}
  • 输出
1
2
Student{name='A', age=20, sex=男}
Student{name='B', age=21, sex=女}

10.3 LinkedHashSet 有序的

插入的过程中是有序的Set,好像在一些LeetCode题目会用到这种特殊的结构

10.4 TreeSet结构(可排序)

image-20230101232342167

这里有一系列可以定义比较规则等等的,留着以后使用了

image-20230101233324672

和之前的对比

image-20230101180455822

11. 补充知识:可变参数

image-20230101234105641
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.curious.javasepro.d18_params;

import java.util.Arrays;

public class MethodDemo {
public static void main(String[] args) {
sum();
sum(10);
sum(10, 20, 30);
sum(new int[]{10, 20, 30});

}

public static void sum(int... nums) {
// 实际上内部就是一个数组
System.out.println(Arrays.toString(nums));
}
}
1
2
3
4
[]
[10]
[10, 20, 30]
[10, 20, 30]
image-20230101234114009

12. 补充知识:集合工具类(Collections)

image-20230101234507290

12.1 批量添加元素(使用到了可变参数)

image-20230101234724706

12.2 打乱

image-20230101234931697

12.3 排序(对list集合)

image-20230101235250328
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.curious.javasepro.d18_params;

import java.util.*;

public class Test {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 2, 1, 3, 4);
// Arrays.sort(list); 这个排不了
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
System.out.println(list);
}
}

13. Map集合体系(区别于Collection集合体系,Map是双列)

是一个新的集合体系,不属于Collection的

image-20230102003508039

13.1 概述、API、遍历方式

13.1.1 概述

image-20230102000342429 image-20230102000352565

13.1.2 特点

image-20230102000735273
1
Map<Integer, Integer> mp = new HashMap<>();

13.1.3 常用API(可以用一个.来自动显示,Java的做题感觉很依赖IDEA)

image-20230102001711690
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.curious.javasepro.d19_map;

import java.util.HashMap;
import java.util.Map;

public class Test {
public static void main(String[] args) {
Map<Integer, Integer> mp = new HashMap<>();

mp.put(0, 0);
mp.put(1, 1);
mp.put(2, 2);

int tmp = mp.remove(2);
System.out.println("tmp: " + tmp);

for (Map.Entry<Integer, Integer> entry: mp.entrySet()) {
System.out.println(entry.getKey());

System.out.println(entry.getValue());
}
}
}

13.1.4 遍历方式(其他方式可以去chatGPT问,很准确)

  • 键值对
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.curious.javasepro.d19_map;

import java.util.HashMap;
import java.util.Map;

public class Test {
public static void main(String[] args) {
Map<Integer, Integer> mp = new HashMap<>();

mp.put(0, 0);
mp.put(1, 1);
mp.put(2, 2);

for (Map.Entry<Integer, Integer> entry: mp.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}

13.2 Java的Map可以用类做Key吗

是的,在Java中,可以使用类作为Map的键。但是,如果要使用类作为Map的键,该类必须遵循一些规则。

首先,该类必须实现java.lang.Comparable接口或者java.util.Comparator接口。这是因为Map是通过键来对映射进行排序的,因此必须能够比较Map中的键。

其次,该类必须重写equalshashCode方法。在Java中,equals方法用于比较两个对象是否相等,而hashCode方法用于生成对象的哈希码。Map根据键的哈希码来快速确定映射的位置,因此必须能够生成键的哈希码。

例如,如果要使用类Person作为Map的键,则可以这样实现:

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
public class Person implements Comparable<Person> {
private String name;
private int age;

// 省略其他代码

@Override
public int compareTo(Person o) {
// 按照年龄排序
return this.age - o.age;
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Person)) {
return false;
}
Person p = (Person) o;
return this.name.equals(p.name) && this.age == p.age;
}

@Override
public int hashCode() {
int result = 17;
result = 31 * result + name.hashCode();
result = 31 * result + age;
return result;
}
}

然后,就可以使用Person类作为Map的键了。

1
2
3
4
Map<Person, String> map = new HashMap<>();
map.put(new Person("Alice", 20), "Alice's phone number");
map.put(new Person("Bob", 30), "Bob's phone number");
map.put

13.3 用类做Key一个自己的尝试

13.3.1 Student类(好像只需要实现equals和hashCode)

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
package com.curious.javasepro.d17_set;

import java.util.Objects;

public class Student {
private String name;
private int age;
private char sex;

Student() {

}

public Student(String name, int age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public char getSex() {
return sex;
}

public void setSex(char sex) {
this.sex = sex;
}

// @Override
// public int compareTo(Student o) {
// return this.age - o.age;
// }

@Override
public String toString() {
return "Student{" + "name='" + name + '\'' + ", age=" + age + ", sex=" + sex + '}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && sex == student.sex && Objects.equals(name, student.name);
}

@Override
public int hashCode() {
return Objects.hash(name, age, sex);
}
}

13.3.2 Test测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.curious.javasepro.d19_map;

import java.util.HashMap;
import java.util.Map;

public class Test2 {
public static void main(String[] args) {
Map<Person, String> mp = new HashMap<>();

Person a = new Person("AAA", 11);
Person b = new Person("BBB", 22);

mp.put(a, "123456");
mp.put(b, "456789");

System.out.println(mp.get(a)); // 123456

}
}

13.4 TreeMap可以对key进行排序,要求写出来排序的那个要求

比如Map<Apple, String> mp = new TreeMap<>();

在Apple类中重写compareTo

1
2
3
4
@Override
public int compareTo(Student o) {
return this.age - o.age;
}

13.5 集合嵌套

1
Map<String, List<String>> mp = new HashMap<>();

JavaSE-3进阶API以及一些刷题语法
http://example.com/2022/12/31/develop/java/javase/JavaSE-3进阶API以及一些刷题语法/
作者
Curious;
发布于
2022年12月31日
许可协议