补充知识
创建不可变集合
List Set Map 的 of 方法
List list = List.of( , , , );
1. Stream(重要)
**作用: **简化集合和数组的API。结合Lambda
思想:
1.1 Stream流的获取
1.1.1流的获取
集合获取Collection接口中的默认方法stream()
Collection<String> list = new ArrayList<>();
Stream<String> s = list.stream();
Map获取先拿到键流,再拿值流。
Map<String , Integer> maps = new HashMap<>();
Stream<String> keyStream = maps.keySet().stream();
Stream<Integer> valueStream = maps.values().stream();
Map键值对流
Stream<Map.Entry<String, Integer>> keyvalueStream = maps.entrySet().stream();
数组流
String[] strings = {"hah","hah","hah","hah"};
Stream<String> stringsStream = Arrays.stream(strings);
Stream<String> stringsStream2 = Stream.of(strings);
1.1.1流的过滤
filter : 过滤元素
// Stream filter(Predicate<? super T> predicate)
list.stream().filter(s -> s.startsWith("张")).forEach(s -> System.out.println(s));
long size = list.stream().filter(s -> s.length() == 3).count();
System.out.println(size);
1.2 Stream中间方法常用API
filter limit skip distinct map concat
名称 | 说明 |
---|---|
Stream filter(Predicate<? super T> predicate) | 用于对流中的数据进行过滤。 |
Stream limit(long maxSize) | 获取前几个元素 |
Stream skip(long n) | 跳过前几个元素 |
Stream distinct() | 去除流中重复的元素。依赖(hashCode和equals方法) |
static Stream concat(Stream a, Stream b) | 合并a和b两个流为一个流 |
filter 过滤
list.stream().filter(s -> s.endsWith("丰") ).forEach(System.out::println);
filter方法内常见的方法有 endsWith、startsWith
limit 取前几个元素
list.stream().limit(2).forEach(System.out::println);
skip 跳过前几个元素
list.stream().skip(2).forEach(System.out::println);
distinct去重
list.stream().distinct().forEach(System.out::println);
**map 加工方法**
//结合元素加工 为元素添加 部分数据
list.stream().map(s -> "黑马员工: "+s).forEach(System.out::println);
//把元素 加工成对象
list.stream().map(s -> new Student(s)).forEach(System.out::println);
contact 合并流
Stream<String> s1 = list.stream().filter(s -> s.startsWith("张"));
Stream<String> s2 = Stream.of("java1", "java2");
Stream<String> s3 = Stream.concat(s1 , s2);
s3.distinct().forEach(s -> System.out.println(s));
1.3 终结方法的API
名称 | 说明 |
---|---|
void forEach(Consumer action) | 对此流的每个元素执行遍历操作 |
long count() | 返回此流中的元素数 |
**注意: **count的返回值为 long类型
1.4 Stream流的数据收集
注意:流只能用一次
名称 | 说明 |
---|---|
R collect(Collector collector) | 开始收集Stream流,指定收集器 |
名称 | 说明 |
---|---|
public static Collector toList() | 把元素收集到List集合中 |
public static Collector toSet() | 把元素收集到Set集合中 |
public static Collector toMap(Function keyMapper , Function valueMapper) | 把元素收集到Map集合中 |
//toList
Stream<String> s1 = list.stream().filter(s -> s.startsWith("张"));
List<String> zhangList = s1.collect(Collectors.toList()); // 可变集合
//toSet
Stream<String> s2 = list.stream().filter(s -> s.startsWith("张"));
Set<String> zhangSet = s2.collect(Collectors.toSet());
2. 异常体系
2.1 运行时异常 RuntimeException
常见的RuntimeException异常
ArrayIndexOutOfBoundsException 数组索引越界异常
NullPointerException 空指针异常
ArithmeticException 数学操作异常
ClassCastException 类型转换异常
NumberFormatException 数字转换异常
2.2编译时异常
例如:ParseException 日期解析异常
2.3编译时异常处理
- throws
抛出异常格式:
方法 throws 异常1 ,异常2 ,异常3 ..{
}
规范做法:
方法 throws Exception{
}
- try…catch
格式:
try{
// 监视可能出现异常的代码!
}catch(异常类型1 变量){
// 处理异常
}catch(异常类型2 变量){
// 处理异常
}...
建议格式:
try{
// 可能出现异常的代码!
}catch (Exception e){
e.printStackTrace(); // 直接打印异常栈信息
}
Exception可以捕获处理一切异常类型!
3.前两者结合
方法直接将异通过throws抛出去给调用者
调用者收到异常后直接捕获处理
2.4 运行时异常的处理
- 运行时异常编译阶段不会出错,是运行时才可能出错的,所以编译阶段不处理代码也可以运行。
- 运行时异常不需要手工的通过throws抛出去,默认会自动抛出去。
- 运行时异常可以自己捕获处理,也可以在最外层调用者集中捕获处理。
2.5 自定义异常
运行时异常:
自定义一个类继承RuntimeException方法或者Exception方法,有参构造和无参构造(快捷键中选前两个)
public class runtimeException extends RuntimeException{
public runtimeException(String message) {
super(message);
}
public runtimeException() {
}
}
编译时异常:
自定义一个类继承RuntimeException方法或者Exception方法,有参构造和无参构造(快捷键中选前两个)
public class timeException extends Exception{
public timeException(String message) {
super(message);
}
public timeException() {
}
}
测试:
第一步:创建一个方法
第二步:创建一个Exception异常对象 创建完异常后需要对方法进行抛出异常
throw new runtimeException();
//括号中写发成异常后的提示词
throw new runtimeException(age+" 年零异常");
第三步:
编译时异常
public class Test {
public static void main(String[] args) throws Exception {
heduiage(-349);
}
public static void heduiage(int age) throws Exception {
if (age < 0 || age > 100) {
throw new Exception(age+" 年零异常");
}else {
System.out.println("年龄合法");
}
}
}
运行时异常:
public class Test {
public static void main(String[] args) {
heduiage(-349);
}
public static void heduiage(int age) {
if (age < 0 || age > 100) {
throw new RuntimeException(age+" 年零异常");
}else {
System.out.println("年龄合法");
}
}
}