ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java学习_20220622

2022-06-22 22:36:52  阅读:107  来源: 互联网

标签:20220622 Java stream list System 学习 println now out


Java 8新特性

1. 方法引用

方法引用是Lambda表达式的一种简写形式。

若Lambda表达式方法体中只是调用一个特定的已经存在的方法,则可以使用方法引用。

常见形式:

(1)对象::实例方法

//1.对象::实例方法
Consumer<String> consumer = s -> System.out.println(s);
consumer.accept("hello");
Consumer<String> consumer2 = System.out::println;
consumer2.accept("world");

(2)类::静态方法

//2.类::静态方法
Comparator<Integer> comparator =(o1,o2)->Integer.compare(o1,o2);
Comparator<Integer> comparator1 = Integer::compare;

(3)类::实例方法

//3.类::实例方法
Function<Employee,String> function = e ->e.getName();
Function<Employee,String> function1 = Employee::getName;
System.out.println(function1.apply(new Employee("小小",2333)));

(4)类::new

//4.类::new
Supplier<Employee> supplier = () ->new Employee();
Supplier<Employee> supplier1 = Employee:: new;
Employee employee = supplier1.get();
System.out.println(employee);

2. Stream流

流中保存对集合或数组数据的操作。和集合类似,但集合中保存的是数据。

Stream 自己不会存储元素。不会改变源对象,会返回一个持有结果的新Stream。操作是延迟执行的,等到需要结果的时候才执行。

(1)创建Stream

public static void main(String[] args) {
    //(1)Collection 对象中的stream()和parallelStream()方法
    ArrayList<String> arrayList = new ArrayList<>();
    arrayList.add("pingguo");
    arrayList.add("huawei");
    arrayList.add("xiaomi");
    Stream<String> stream = arrayList.stream();
    //遍历
    // stream.forEach(s-> System.out.println(s));
    stream.forEach(System.out::println);
    //(2)Arrays工具类的stream方法
    String[] arr = {"Java","C#","JavaScript","PHP"};
    Stream<String> stream1 = Arrays.stream(arr);
    stream1.forEach(System.out::println);
    //(3)Stream接口中的of iterate、generate方法
    Stream<Integer> integerStream = Stream.of(10, 20, 30, 40, 50);
    integerStream.forEach(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)InStream,LongStream,DoubleStream 的of,range,rangeClosed
    IntStream intStream = IntStream.of(100, 200, 300);
    intStream.forEach(System.out::println);
    IntStream rangeClosed = IntStream.rangeClosed(0, 50);
    rangeClosed.forEach(System.out::println);
}

(2)中间操作

public class Employee {
    private String name;
    private double money;
}
public static void main(String[] args) {
        ArrayList<Employee> list = new ArrayList<>();
        list.add(new Employee("张三",1500));
        list.add(new Employee("李四",4810));
        list.add(new Employee("王五",2500));
        list.add(new Employee("赵六",6200));
}

filter过滤

System.out.println("====filter过滤====");
list.stream()
        .filter(e->e.getMoney()>2000)  //filter过滤
        .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(3) //跳过三个
        .forEach(System.out::println);

 distinct去重复

System.out.println("====distinct去重复====");
list.stream()
        .distinct() //需要在Employee类中重写hashcode和equals方法
        .forEach(System.out::println);

sorted排序

System.out.println("====sorted排序====");
list.stream()
        .sorted((e1,e2)->Double.compare(e1.getMoney(),e2.getMoney()))
        .forEach(System.out::println);

map获取name

System.out.println("====map====");
list.stream()
        .map(e->e.getName())
        .forEach(System.out::println);

parallel 并行流,采用多线程,效率高

System.out.println("====parallel====");
list.parallelStream()
        .forEach(System.out::println);

(3)终止操作

forEach

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());
System.out.println("====count====");
long count = list.stream().count();
System.out.println("员工个数"+count);

终止操作reduce

//终止操作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收集

//终止操作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);
}

3. 日期和时间

旧版本问题:

(1)设计不合理,在java.util和java.sql的包中都有日期类,java.util.Date同时包含日期和时间的,而java.sql.Date仅仅包含日期,此外用于格式化和解析的类在java.text包下;

(2)非线程安全,java.sql.Date是非线程安全的,所有的日期类都是可变的,这是java日期类最大的问题之一;

(2)时区处理玛法,日期类并不提供国际化,没有时区支持。

日期时间的常见操作:

LocalDate、LocalTime、LocalDateTime 

(1)创建指定的日期:LocalDate.of()

(2)获取当前日期:LocalDate.now()

(3)获取对应的日期信息:now.getYear(),now.getDayOfMouth(),now.getDayOfWeek().getValue()....

(4)创建指定的时间: LocalTime.of()

(5)获取当前时间:LocalTime.now()

(6)获取对应的时间信息:now.getHour(),now.getMinute(),now.getSecond()....

(7)创建指定的日期和时间:LocalDateTime.of()

(8)获取当前日期和时间:LocalDateTime.now()

(9)获取对应的日期时间信息:now.getYear(),now.getDayOfMouth(),now.getDayOfWeek().getValue(),now.getHour(),now.getMinute(),now.getSecond()....

日期和时间的修改:

对日期时间的修改,对已存在的LocalDate对象,创建了它的模板,不会修改原来的信息,而是修改副本信息。在多线程场景下是数据安全的。

LocalDateTime now = LocalDateTime.now();
LocalDateTime dateyear = now.withYear(1998);
System.out.println(now);//2022-06-22T21:51:07.026831200
System.out.println(dateyear);//1998-06-22T21:51:07.026831200

在当前日期和时间的基础上,加上或者减去指定的时间

System.out.println(now.plusDays(2));//两天后2022-06-24T21:55:45.627245600
System.out.println(now.minusYears(10));//十年前2012-06-22T21:55:45.627245600

日期时间的比较

LocalDate now1 = LocalDate.now();
LocalDate of = LocalDate.of(2020, 1, 20);
System.out.println(now1.isAfter(of));//now1的时间在of的时间之后 true
System.out.println(now1.isBefore(of));//now1的时间在of的时间之前 false
System.out.println(now1.equals(of)); //两个时间是否相等  false

格式化和解析操作

格式化操作:

LocalDateTime now2 = LocalDateTime.now();
//指定格式 使用系统默认的格式2022-06-22T22:09:23.0564635
DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//将日期时间转换为字符串
String format = now2.format(isoLocalDateTime);
System.out.println(format);

//通过ofPattern方法指定特定的格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format1 = now.format(dateTimeFormatter);
System.out.println(format1);//2022-06-173 22:06:85

解析

//将字符串解析为一个时间类型
LocalDateTime parse = LocalDateTime.parse("1997-05-06 22:15:18", dateTimeFormatter);
System.out.println("parse = " + parse);//parse = 1997-05-06T22:15:18

标签:20220622,Java,stream,list,System,学习,println,now,out
来源: https://www.cnblogs.com/Joyce-mi7/p/16401168.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有