Java8又称JDK1.8新特性
- 支持Lambda表达式
- 函数式接口
- 新的Stream API
- 新的日期API
- 其他特性等
Lambda表达式
- 特殊匿名内部类,允许把函数作为一个方法的参数传递,将代码像数据一样传递
- 基本语法:
- <函数式接口> <变量名> = (参数1,参数2…)->{//方法体}
//Lambda表达式
Runnable runnable2=()->System.out.println("子线程执行了2.........");
new Thread(runnable2).start();
new Thread(()->System.out.println("子线程执行了3.........")).start();
-----------------------------------------------
//Lambda表达式
Comparator<String> com2=(String o1, String o2)-> {
return o1.length()-o2.length();
};
Comparator<String> com3=(o1,o2)->o1.length()-o2.length();
函数式接口
- 如果一个接口只有一个抽象方法,该接口就是函数式接口,可以使用Lambda表达式
- @Functionallnterface注解检测接口是否符号函数式接口
@FunctionalInterface
public interface Usb {
void service();
}
------------------------------------------------------------------------
public class Demo2 {
public static void main(String[] args) {
//匿名内部类
Usb mouse=new Usb() {
@Override
public void service() {
System.out.println("鼠标开始工作了..........");
}
};
Usb fan=()->System.out.println("风扇开始工作了..........");
run(mouse);
run(fan);
}
public static void run(Usb usb) {
usb.service();
}
}
- 常见函数式接口
- Consumer
- Supplier
- Function<T,R>
- Predicate
//Consumer 消费型接口
public static void happy(Consumer<Double> consumer,double money) {
consumer.accept(money);
}
//Supplier 供给型接口
public static int[] getNums(Supplier<Integer> supplier,int count) {
int[] arr=new int[count];
for(int i=0;i<count;i++) {
arr[i]=supplier.get();
}
return arr;
}
//Function函数型接口
public static String handlerString(Function<String, String> function,String str) {
return function.apply(str);
}
//Predicate 断言型接口
public static List<String> filterNames(Predicate<String> predicate,List<String> list){
List<String> resultList=new ArrayList<String>();
for (String string : list) {
if(predicate.test(string)) {
resultList.add(string);
}
}
return resultList;
}
Stream流
- 与集合类似,但集合中保存的是数据,而Stream保存的是对集合或数组中数据的操作
- 特点
- Stream自己不会存储元素
- 不会改变源对象,相反,会返回一个持有结果的新Stream
- 操作是延迟执行的,会等到需要结果时才执行
- 使用步骤:
- 创建
- 中间操作
- 终止操作
创建
//(1)Collection对象中的stream()和parallelStream()方法
ArrayList<String> arrayList=new ArrayList<>();
arrayList.add("apple");
arrayList.add("huawei");
arrayList.add("xiaomi");
Stream<String> stream = arrayList.parallelStream();
//遍历
// stream.forEach(s->System.out.println(s));
stream.forEach(System.out::println);
//(2)Arrays工具类的stream方法
String[] arr= {"aaa","bbb","ccc"};
Stream<String> stream2=Arrays.stream(arr);
stream2.forEach(System.out::println);
//(3)Stream接口中的of iterate 、generate 方法
Stream<Integer> stream3 = Stream.of(10,20,30,40,50);
stream3.forEach(System.out::println);
//迭代流
System.out.println("-----迭代流------");
Stream<Integer> iterate = Stream.iterate(0, x->x+2);
iterate.limit(5).forEach(System.out::println);
System.out.println("--------生成流----------");
//生成流
Stream<Integer> generate = Stream.generate(()->new Random().nextInt(100));
generate.limit(10).forEach(System.out::println);
//(4)IntStream,LongStream,DoubleStream 的of 、range、rangeClosed
IntStream stream4 = IntStream.of(100,200,300);
stream4.forEach(System.out::println);
IntStream range = IntStream.rangeClosed(0, 50);
range.forEach(System.out::println);
中间操作
public static void main(String[] args) {
ArrayList<Employee> list=new ArrayList<>();
list.add(new Employee("小王", 15000));
list.add(new Employee("小张", 12000));
list.add(new Employee("小李", 18000));
list.add(new Employee("小孙", 20000));
list.add(new Employee("小刘", 25000));
//list.add(new Employee("小刘", 25000));
//中间操作1 filter过滤 2 limit 限制 3 skip 跳过 4 distinct 去掉重复 5 sorted排序
//filter过滤
System.out.println("------filter-------");
list.stream()
.filter(e->e.getMoney()>15000)
.forEach(System.out::println);
//limit限制
System.out.println("----limit------");
list.stream()
.limit(2)
.forEach(System.out::println);
//skip跳过
System.out.println("-----skip------");
list.stream()
.skip(2)
.forEach(System.out::println);
System.out.println("------distinct--------");
//distinct去重复
list.stream()
.distinct()
.forEach(System.out::println);
System.out.println("---------sorted---------");
//sorted排序
list.stream()
.sorted((e1,e2)->Double.compare(e1.getMoney(), e2.getMoney()))
.forEach(System.out::println);
//中间操作2 map
System.out.println("---------map--------");
list.stream()
.map(e->e.getName())
.forEach(System.out::println);
//中间操作3 parallel 采用多线程 效率高
System.out.println("---------map--------");
list.parallelStream()
.forEach(System.out::println);
}
终止操作
public static void main(String[] args) {
ArrayList<Employee> list = new ArrayList<>();
list.add(new Employee("小王", 15000));
list.add(new Employee("小张", 12000));
list.add(new Employee("小李", 18000));
list.add(new Employee("小孙", 20000));
list.add(new Employee("小刘", 25000));
//终止操作 foreach
list.stream()
.filter(e->{
System.out.println("过滤了....");
return e.getMoney()>15000;
})
.forEach(System.out::println);
//终止操作 min max count
System.out.println("-----min-----");
Optional<Employee> min = list.stream()
.min((e1,e2)->Double.compare(e1.getMoney(), e2.getMoney()));
System.out.println(min.get());
System.out.println("-----max-----");
Optional<Employee> max = list.stream()
.max((e1,e2)->Double.compare(e1.getMoney(), e2.getMoney()));
System.out.println(max.get());
long count = list.stream().count();
System.out.println("员工个数:"+count);
//终止操作 reduce 规约
//计算所有员工的工资和
System.out.println("--------reduce---------");
Optional<Double> sum = list.stream()
.map(e->e.getMoney())
.reduce((x,y)->x+y);
System.out.println(sum.get());
//终止方法 collect收集
//获取所有的员工姓名,封装成一个list集合
System.out.println("------collect------");
List<String> names = list.stream()
.map(e->e.getName())
.collect(Collectors.toList());
for (String string : names) {
System.out.println(string);
}
}
新的时间API
- 之前时间API存在问题:线程安全问题、设计混乱
- 本地化日期时间API:
- LocalDate
- LocalTime
- LocalDateTime
- Instant:时间戳
- ZoneId:时区
- Date ,Instant, LocalDateTime的转换
- DateTimeFormatter:格式化类
System.out.println("-------------Date --->Instant---->LocalDateTime-----------");
Date date=new Date();
Instant instant3 = date.toInstant();
System.out.println(instant3);
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant3, ZoneId.systemDefault());
System.out.println(localDateTime);
//1 LocalDateTime --->Instant---->Date
System.out.println("-------------LocalDateTime --->Instant---->Date-----------");
Instant instant4 = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
System.out.println(instant4);
Date from = Date.from(instant4);
System.out.println(from);
------------------------------------------------------------
//创建DateTimeFormatter
DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
//(1)把时间格式化成字符串
String format = dtf.format(LocalDateTime.now());
System.out.println(format);
//(2)把字符串解析成时间
LocalDateTime localDateTime = LocalDateTime.parse("2020/03/10 10:20:35", dtf);
System.out.println(localDateTime);