package wang; import java.awt.*; import java.lang.reflect.Array; import java.util.*; import java.util.List; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.IntStream; public class dome { public static void main(String[] args) { ListpersonList = new ArrayList<>(); personList.add(new Person("欧阳雪",18,"中国",'F')); personList.add(new Person("Tom",24,"美国",'M')); personList.add(new Person("Harley",22,"英国",'F')); personList.add(new Person("向天笑",20,"中国",'M')); personList.add(new Person("李康",22,"中国",'M')); personList.add(new Person("小梅",20,"中国",'F')); personList.add(new Person("何雪",21,"中国",'F')); personList.add(new Person("李康",22,"中国",'M')); //findFirst match // Optional first = personList.stream().filter(item -> item.getSex() == 'M').findFirst(); /* boolean b = personList.stream().filter(item -> item.getSex() == 'M').allMatch(item -> item.getAge() > 20);//所有都满足 System.out.println(b);*/ /* Supplier supplier = ()->{ return new Person(); };*/ Supplier supplier = ()->{ return new Person(); }; System.out.println(supplier.get()); //对象方法的引用 //Function function = item->item.length(); /* Function function = String::length; System.out.println(function.apply("niihao"));*/ //比较两个字符串是否相等 /* BiFunction biFunction = (t,u)->{ return t.equals(u); };*/ /* BiFunction biFunction = String::equals; System.out.println(biFunction.apply("haha","heihei"));*/ //collect搜索终止 //求年龄大于20且性别为M /*List collect = personList.stream() .filter(item -> item.getAge() > 20) .filter(item -> item.getSex() == 'M') .collect(Collectors.toList()); System.out.println(collect);*/ //求集合中年龄的和 /*Optional reduce = personList.stream() .map(item -> item.getAge()) .reduce((a, b) -> a + b);*/ /* Integer reduce = personList.stream() .map(item -> item.getAge()) .reduce(10, (a, b) -> a + b); System.out.println(reduce);*/ //查找最大值 /*Optional max = personList.stream() .max((o1, o2) -> o1.getAge() - o2.getAge()); System.out.println(max.get());*/ //按照年龄排序 /*personList.stream() .sorted(((o1, o2) -> o1.getAge()- o2.getAge())) .forEach(System.out::println);*/ /* boolean b = personList.stream().filter(item -> item.getAge() > 20).allMatch(item -> item.getSex() == 'M'); System.out.println(b);*/ /*Optional reduce = personList.stream().map(item -> item.getAge()).reduce((a, b) -> a + b); System.out.println(reduce.get());*/ //personList.stream().filter(item->item.getAge()==18).forEach(item-> System.out.println(item)); /*personList.stream().map(item->{ Map map = new HashMap(); map.put("name",item.getName()); map.put("age",item.getAge()); return map; }).forEach(System.out::println);*/ /* ArrayList list = new ArrayList<>(); Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张强", "张三丰","何线程"); list.stream() .filter(item->item.startsWith("张")) .filter(item->item.length()==3) .forEach(item-> System.out.println(item)); int[] arr={2,1,23,1,5,4}; IntStream stream = Arrays.stream(arr); stream.forEach(item-> System.out.println(item));*/ } } class Person { private String name; private Integer age; private String country; private char sex; public Person() { } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", country='" + country + '\'' + ", sex=" + sex + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public Person(String name, Integer age, String country, char sex) { this.name = name; this.age = age; this.country = country; this.sex = sex; } }
package date; import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class dome { public static void main(String[] args) { LocalDate now = LocalDate.now();//当前日期 LocalDate of = LocalDate.of(2020,1,2);//自定义日期 LocalTime now1 = LocalTime.now();//当前时间 LocalTime of1 = LocalTime.of(12, 25, 12,16546);//自定义时间 LocalDateTime now2 = LocalDateTime.now();//当前日期时间 LocalDateTime of2 = LocalDateTime.of(2020, 5, 4, 12, 12, 12, 12);//自定义日期时间 Duration between = Duration.between(of2, now2);//时间差Duration DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");//自定义类型 LocalDate parse = LocalDate.parse("2021-12-11", dateTimeFormatter);//字符串转日期 String format = parse.format(dateTimeFormatter);//日期转字符串 System.out.println(between.toDays()); } }