default关键字
在jdk1.8以前接⼝⾥⾯是只能有抽象⽅法,不能有任何⽅法的实现的 jdk1.8⾥⾯打破了这个规定,引⼊了新的关键字default,使⽤default修饰⽅法,可以在接⼝⾥⾯ 定义具体的⽅法实现
默认⽅法: 接⼝⾥⾯定义⼀个默认⽅法,这个接⼝的实现类实现了这个接⼝之后,不⽤管这个 default修饰的⽅法就可以直接调⽤,即接⼝⽅法的默认实现
base64编码
64个可打印字符---->⼆进制数据的⽅法
A-Z,a-z,0-9,+,/的编码⽅式, 是⼀种能将任意⼆进制数据⽤64种字元组合成字符串的⽅法,⽽这个⼆进制数据和字符串资料之 间是可以互相转换的,在实际应⽤上,Base64除了能将⼆进制数据可视化之外,也常⽤来表示字 串加密过后的内容景
参考https://blog.csdn.net/wo541075754/article/details/81734770
早期的实现方法
使⽤JDK⾥sun.misc套件下的BASE64Encoder和BASE64Decoder这两个类
BASE64Encoder encoder = new BASE64Encoder();
BASE64Decoder decoder = new BASE64Decoder();
String text = "⼩滴课堂";
byte[] textByte = text.getBytes("UTF-8");
//编码
String encodedText = encoder.encode(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(decoder.decodeBuffer(encodedText),
"UTF-8"));
缺点:编码和解码的效率⽐较差,公开信息说以后的版本会取消这个⽅法
Apache Commons Codec有提供Base64的编码与解码 缺点:是需要引⽤Apache Commons Codec
JDK1.8之后怎么玩???
Base64.Decoder decoder = Base64.getDecoder();
Base64.Encoder encoder = Base64.getEncoder();
String text = "⼩滴课堂";
byte[] textByte = text.getBytes("UTF-8");
//编码
String encodedText = encoder.encodeToString(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(decoder.decode(encodedText), "UTF8"));
⽇期处理类上集
SimpleDateFormat,Calendar等类
旧版缺点: java.util.Date 是⾮线程安全 的 API设计⽐较差,⽇期/时间对象⽐较,加减麻烦
LocalDate.now();
year-month-day
LocalDate today = LocalDate.now();
System.out.println("今天⽇期:" + today);
//获取年,⽉,⽇,周⼏
System.out.println("现在是哪年:"+today.getYear());
System.out.println("现在是哪⽉:"+today.getMonth());
System.out.println("现在是哪⽉(数字):"+today.getMonthValue());
System.out.println("现在是⼏号:"+today.getDayOfMonth());
System.out.println("现在是周⼏:"+today.getDayOfWeek());
//加减年份, 加后返回的对象才是修改后的, 旧的依旧是旧的
LocalDate changeDate = today.plusYears(1);
System.out.println("加后是哪年:"+changeDate.getYear());
System.out.println("旧的是哪年:"+today.getYear());
//⽇期⽐较
System.out.println("isAfter: "+changeDate.isAfter(today));
//getYear() int 获取当前⽇期的年份
//getMonth() Month 获取当前⽇期的⽉份对象
//getMonthValue() int 获取当前⽇期是第⼏⽉
//getDayOfWeek() DayOfWeek 表示该对象表示的⽇期是星期⼏
//getDayOfMonth() int 表示该对象表示的⽇期是这个⽉第⼏天
//getDayOfYear() int 表示该对象表示的⽇期是今年第⼏天
//withYear(int year) LocalDate 修改当前对象的年份
//withMonth(int month) LocalDate 修改当前对象的⽉份
//withDayOfMonth(int dayOfMonth) LocalDate 修改当前对象在当⽉的⽇期
//plusYears(long yearsToAdd) LocalDate 当前对象增加指定的年份数
//plusMonths(long monthsToAdd) LocalDate 当前对象增加指定的⽉份数
//plusWeeks(long weeksToAdd) LocalDate 当前对象增加指定的周数
//plusDays(long daysToAdd) LocalDate 当前对象增加指定的天数
//minusYears(long yearsToSubtract) LocalDate 当前对象减去指定的年数
//minusMonths(long monthsToSubtract) LocalDate 当前对象减去注定的⽉数
//minusWeeks(long weeksToSubtract) LocalDate 当前对象减去指定的周数
//minusDays(long daysToSubtract) LocalDate 当前对象减去指定的天数
//compareTo(ChronoLocalDate other) int ⽐较当前对象和other对象在时间上的⼤⼩,返回值如果为正,则当前对象时间较晚,
//isBefore(ChronoLocalDate other) boolean ⽐较当前对象⽇期是否在other对象⽇期之前
//isAfter(ChronoLocalDate other) boolean ⽐较当前对象⽇期是否在other对象⽇期之后
//isEqual(ChronoLocalDate other) boolean ⽐较两个⽇期对象是否相等
JDK8之后:引⼊线程安全的⽇期与时间DateTimeFormatter
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String ldtStr = dtf.format(ldt);
System.out.println(ldtStr);
optional类
作用:主要解决的问题是空指针异常(NullPointerException)
how:本质是⼀个包含有可选值的包装类,这意味着 Optional 类既可以含有对象也可以为空
常见的方法:
/**
* Constructs an empty instance.
*
* @implNote Generally only one empty instance, {@link Optional#EMPTY},
* should exist per VM.
*/
private Optional() {
this.value = null;
}
Returns an empty {@code Optional} instance. No value is present for this
* Optional.
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY;
return t;
}
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
optional对象的方法
public T get() {
if (value == null) {
throw new NoSuchElementException("No value present");
}
return value;
}
public boolean isPresent() {
return value != null;
}
兜底方法,返回一个默认的值
public T orElse(T other) {
return value != null ? value : other;
}
Student stu1=null;
Student stu2=new Student(18,"xx");
Student result = Optional.ofNullable(stu1).orElse(stu2);
System.out.println(result);
写法2:把一个对象进转换的时候的写法
Student stu1=null;
Student stu2=new Student(18);
Student result = Optional.ofNullable(stu1).orElse(stu2);
// System.out.println(result.getAge()+result.getName());
int age=Optional.ofNullable(stu1).map(obj->(obj.getAge())).orElse(7);
System.out.println(age);
Lambda表达式:
函数式编程和lambda表达式:即可理解是将⼀个函数(也称为“⾏ 为”)作为⼀个参数进⾏传递, ⾯向对象编程是对数据的抽象(各种各样的POJO类),⽽函数式编 程则是对⾏为的抽象(将⾏为作为⼀个参数进⾏传递)
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("创建线程了");
}
}).start();
1.8以后的方式
new Thread(()->{System.out.println("线程创建了");}).start();
():表示参数
->:箭头的右侧表示方法的方法体表示行为 相当于把整个行为当成一个参数传递进去了‘’
//自定义的排序
List<Integer> list = Arrays.asList(1, 2, 3,5,2);
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return a.compareTo(b);
}
});
for (int l:list){
System.out.println("比较的结果"+l);
}
12235
1.8之后的lamdba表达式的写法:(参数)->{逻辑}/表达式
由于run方法中没有参数所以()中不用写参数
Collections.sort(list,(a,b)->a.compareTo(b));
for (int l:list){
System.out.println("比较的结果"+l);
}
ambda表达式 使⽤场景(前提):⼀个接⼝中只包含⼀个⽅法(接口中特有的方法),则可以使⽤Lambda表达式,这样 的接⼝称之为“函数接⼝” 语法: (params) -> expression
规则:
第⼀部分为括号内⽤逗号分隔的形式参数,参数是函数式接⼝⾥⾯⽅法的参数;第⼆部分为⼀个箭
头符号:->;第三部分为⽅法体,可以是表达式和代码块
参数列表 :
括号中参数列表的数据类型可以省略不写
括号中的参数只有⼀个,那么参数类型和()都可以省略不写
⽅法体:
如果{}中的代码只有⼀⾏,⽆论有返回值,可以省略{},return,分号,要⼀起省略,其他
则需要加上
之前的接口中会有类型能够进行推断
本质:以匿名内部类的⽅式进⾏实现
自定义Lamdba
- 定义⼀个函数式接⼝ 需要标注此接⼝ @FunctionalInterface,否则万⼀团队成员在接⼝上加 了其他⽅法则容易出故障
- 编写⼀个⽅法,输⼊需要操做的数据和接⼝
- 在调⽤⽅法时传⼊数据 和 lambda 表达式,⽤来操作数据
需求,定义⼀个可以使⽤加减乘除的接⼝ 以前需要定义4个⽅法
@FunctionalInterface
public interface OperFunction<R,T> {
R operator(T t1, T t2);
}
实现使用的方法:
public static void main(String[] args) throws Exception {
System.out.println(operator(20, 5, (Integer x, Integer y) -> {
return x * y;
}));
System.out.println(operator(20, 5, (x, y) -> x + y));
System.out.println(operator(20, 5, (x, y) -> x - y));
System.out.println(operator(20, 5, (x, y) -> x / y));
}
public static Integer operator(Integer x, Integer y,
OperFunction<Integer, Integer> of) {
return of.operator(x, y);
}