创建不可变集合
jdk9开始支持
package org.example.d4_dmeo;
import java.util.List;
public class Demo2 {
public static void main(String[] args) {
List<Double> list=List.of(23.0,22.6,22.4);
System.out.println(list);
//其余一样
}
}
stream流
stream流的概述
package org.example.d1_stream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Demo1 {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
Collections.addAll(list,"张无忌","张三丰","孙悟空","张强");
/*
List<String> list1=new ArrayList<>();
//找出姓张的
for (String s : list) {
if (s.startsWith("张")){
list1.add(s);
}
}
System.out.println(list1);
//找出三个字符的
List<String> list2=new ArrayList<>();
for (String s : list1) {
if (s.length()==3){
list2.add(s);
}
}
System.out.println(list2);
*/
//使用stream流
list.stream().filter(s->s.startsWith("张")).filter(s->s.length()==3).forEach(s-> System.out.println(s));
}
}
stream流的获取
package org.example.d1_stream;
import java.util.*;
import java.util.stream.Stream;
public class Demo2 {
public static void main(String[] args) {
System.out.println("===============Collection集合获取流=====================");
Collection<String> list=new ArrayList<>();
Stream<String> stream = list.stream();
System.out.println("=========================Map集合获取流====================");
Map<String,Integer> map=new HashMap<>();
//键流
Stream<String> stream1 = map.keySet().stream();
//值流
Stream<Integer> stream2 = map.values().stream();
//键值对流
Stream<Map.Entry<String, Integer>> stream3 = map.entrySet().stream();
System.out.println("=======================数组获取流==========================");
String[] s={"孙悟空","猪八戒","沙和尚","唐僧"};
Stream<String> stream4 = Arrays.stream(s);
Stream<String> s1 = Stream.of(s);
}
}
stream流的常用API
package org.example.d1_stream;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class Demo3 {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
Collections.addAll(list,"张无忌","张三丰","孙悟空","张强");
/*
list.stream().filter(new Predicate<String>() {
@Override
public boolean test(String s) {
return s.startsWith("张");
}
}).forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
*/
list.stream().filter(s->s.startsWith("张")).forEach(s-> System.out.println(s));
System.out.println(list.stream().filter(s -> s.length() == 3).count());
list.stream().filter(s -> s.startsWith("张")).limit(2).forEach(s -> System.out.println(s));
list.stream().filter(s->s.startsWith("张")).skip(2).forEach(s -> System.out.println(s));
//Map加工方法
//给集合元素前面都加上一个:黑马的
/*
list.stream().map(new Function<String, Object>() {
@Override
public Object apply(String s) {
return "黑马的"+s;
}
}).forEach(s-> System.out.println(s));
*/
list.stream().map(s -> "黑马的"+s).forEach(s -> System.out.println(s));
//把所有的名称都加工成学生对象
list.stream().map(s -> new Student(s)).forEach(s -> System.out.println(s));
//合并流
Stream<String> s1=list.stream().filter(s -> s.startsWith("张"));
Stream<String> s2=Stream.of("JAVA1","JAVA2");
Stream<String> concat = Stream.concat(s1, s2);
concat.distinct().forEach(s -> System.out.println(s));
//distinct()方法是去重复
}
}
收集stream流
package org.example.d1_stream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo2 {
public static void main(String[] args) {
System.out.println("===============Collection集合获取流=====================");
Collection<String> list=new ArrayList<>();
Stream<String> stream = list.stream();
// List<String> list1=stream.collect(Collectors.toList());
// System.out.println(list1);
//流只能使用一次
Set<String> set=stream.collect(Collectors.toSet());
System.out.println(set);
Object[] objects=stream.toArray();
System.out.println(Arrays.toString(objects));
}
}
异常处理
异常概述、体系
常见运行时异常
常见编译时异常
异常的默认处理流程
编译时异常的处理机制
package org.example.d2_exception;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo1 {
public static void main(String[] args) {
System.out.println("程序开始。。。");
parseTime("2021-11-12 11:22:30");
System.out.println("程序结束。。。");
}
public static void parseTime(String date){
try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1=sdf.parse(date);
System.out.println(date1);
InputStream is=new FileInputStream("E:/meinv.jpg");
} catch (ParseException e) {
System.out.println("日期格式不对");
e.printStackTrace();
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
e.printStackTrace();
}
}
}
package org.example.d2_exception;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo2 {
public static void main(String[] args) {
System.out.println("程序开始。。。");
try {
parseTime("2021-11-12 11:22:30");
System.out.println("方法成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("方法错误");
}
System.out.println("程序结束。。。");
}
public static void parseTime(String date) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdf.parse(date);
System.out.println(date1);
InputStream is = new FileInputStream("E:/meinv.jpg");
}
}
运行时异常的处理机制
程序会自动抛出运行时异常,只需在最外层(main方法中)调用try/catch进行处理,保证程序正常运行即可。
异常案例
package org.example.d2_exception;
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while (true){
try {
System.out.println("请您输入价格:");
String priceStr=sc.nextLine();
double price=Double.valueOf(priceStr);
if (price>0){
System.out.println(price+"价格正确");
break;
}else {
System.out.println("请重新输入,价格不能为负数");
}
} catch (NumberFormatException e) {
System.out.println("请输入合法的价格");
}
}
}
}
自定义异常
package org.example.d2_exception;
public class AgeIlLegalException extends Exception{
public AgeIlLegalException() {
}
public AgeIlLegalException(String message) {
super(message);
}
}
package org.example.d2_exception;
public class Demo4 {
public static void main(String[] args) {
try {
checkAge(124);
} catch (AgeIlLegalException e) {
e.printStackTrace();
}
}
public static void checkAge(int age) throws AgeIlLegalException {
if (age < 0 || age > 100)
throw new AgeIlLegalException(age + " is illLegal");
else {
System.out.println("年龄合法,推荐产品给其购买");
}
}
}
注意
- throw: 在方法内部直接创建一个异常对象,并从此点抛出
- throws:用在方法申明上,抛出方法内部的异常