package com.sofwoin;
import javafx.print.Collation;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @packageName: com.sofwoin
* @author: wentao
* @date: 2023/11/17 20:12
* @version: 1.0
* @email [email protected]
* @description: TODO
*/
public class Test {
public static void main(String[] args) {
// ctrl+alt+m 选择几行代码自动封装成方法
// test1();
// test2();
// test3();
// test4();
// test5();
// test6();
// test7();
// test8();
// test9();
// test10();
// test11();
// test12();
// test13();
// test14();
// test15();
// test16();
// test17();
// test18();
// test19();
// test20();
// test21();
// test22();
// test23();
// test24();
// test25();
// test26();
// test27();
// test28();
// test29();
test30();
}
private static void test30() {
List<Author> authors = getAuthors();
authors.stream()
.map(author -> author.getAge())
.map(age -> age + 10)
.filter(age->age>18)
.map(age->age+2)
.forEach(System.out::println);
List<Author> authors2 = getAuthors();
authors2.stream()
.mapToInt(e -> e.getAge())
.map(age -> age + 10)
.filter(age->age>18)
.map(age->age+2)
.forEach(System.out::println);
}
private static void test29() {
//类名::new
// List<Author> authors = getAuthors();
// authors.stream()
// .map(author -> author.getName())
// .map(name->new StringBuilder(name))
// .map(sb->sb.append("-三更").toString())
// .forEach(str-> System.out.println(str));
List<Author> authors = getAuthors();
authors.stream()
.map(author -> author.getName())
.map(StringBuilder::new)
.map(sb->sb.append("-三更").toString())
.forEach(str-> System.out.println(str));
}
private static void test28() {
// 类名::方法名
// subAuthorName("三更草堂", new UseString() {
// @Override
// public String use(String str, int start, int length) {
// return str.substring(start,length);
// }
// });
subAuthorName("三更草堂", (str, start, length) -> str.substring(start,length));
}
interface UseString{
String use(String str,int start,int length);
}
public static String subAuthorName(String str, UseString useString){
int start = 0;
int length = 1;
return useString.use(str,start,length);
}
private static void test27() {
//对象名::方法名
List<Author> authors = getAuthors();
Stream<Author> authorStream = authors.stream();
StringBuilder sb = new StringBuilder();
// authorStream.map(author -> author.getName())
// .forEach(name->sb.append(name));
authorStream.map(author -> author.getName())
.forEach(sb::append);
}
private static void test26() {
//类名::方法名
List<Author> authors = getAuthors();
Stream<Author> authorStream = authors.stream();
// authorStream.map(author -> author.getAge())
// .map(age->String.valueOf(age));
authorStream.map(author -> author.getAge())
.map(String::valueOf);
}
private static void test25() {
Optional.ofNullable(null); //不会报错
// Optional.of(null); //会报错
Optional<Author> authorOptional = Optional.ofNullable(getAuthors().get(0));
Author author1 = authorOptional.orElseGet(() -> new Author());
Author author2= authorOptional.orElse(new Author());
}
private static void test24() {
// 使用reduce求所有作者中年龄的最小值
Optional<Integer> reduce = getAuthors().stream()
.map(author -> author.getAge())
.distinct()
.reduce((result, element) -> element < result ? element : result);
reduce.ifPresent(System.out::println);
}
private static void test23() {
// 使用reduce求所有作者中年龄的最小值
Integer min = getAuthors().stream()
.map(author -> author.getAge())
.distinct()
.reduce(Integer.MAX_VALUE, (result, element) -> element < result ? element : result);
System.out.println(min);
}
private static void test22() {
// 使用reduce求所有作者中年龄的最大值
Integer max = getAuthors().stream()
.map(author -> author.getAge())
.reduce(Integer.MIN_VALUE, (result, element) -> element > result ? element : result);
System.out.println(max);
}
private static void test21() {
// 例子:使用reduce求所有作者年龄的和
Integer sum = getAuthors().stream()
.map(author -> author.getAge())
.reduce(0, (integer, element) -> integer + element);
System.out.println(sum);
}
private static void test20() {
// 获取一个年龄最小的作家,并输出他的姓名。
Optional<Author> first = getAuthors().stream()
.findFirst();
first.ifPresent(System.out::println);
}
private static void test19() {
// 获取任意一个年龄大于18的作家,如果存在就输出他的名字
Optional<Author> any = getAuthors().stream()
.findAny();
System.out.println(any.get());
}
private static void test18() {
// 判断作家是否都没有超过100岁的。
boolean flag = getAuthors().stream()
.distinct()
.noneMatch(author -> author.getAge() > 100);
System.out.println(flag);
}
private static void test17() {
// 判断是否所有的作家都是成年人
boolean flag = getAuthors().stream()
.distinct()
.allMatch(author -> author.getAge() >= 18);
System.out.println(flag);
}
private static void test16() {
// 判断是否有年龄在29以上的作家
boolean flag = getAuthors().stream()
.distinct()
.anyMatch(author -> author.getAge() > 29);
System.out.println(flag);
}
private static void test15() {
// 获取一个Map集合,map的key为作者名,value为List<Book>
List<Author> authors = getAuthors();
Map<String, List<Book>> map = authors.stream()
.distinct()
.collect(Collectors.toMap(author -> author.getName(), author -> author.getBooks()));
System.out.println(map);
}
private static void test14() {
// 获取一个所有书名的Set集合。
Set<String> collect = getAuthors().stream()
.flatMap(author -> author.getBooks().stream())
.map(book -> book.getName())
.collect(Collectors.toSet());
System.out.println(collect);
}
private static void test13() {
// 获取一个存放所有作者名字的List集合。
List<String> collect = getAuthors().stream()
.distinct()
.map(author -> author.getName())
.col
没有合适的资源?快使用搜索试试~ 我知道了~
函数式编程-Stream流-三更 Author、Book、测试代码
共3个文件
java:3个
0 下载量 65 浏览量
2023-11-22
22:35:23
上传
评论
收藏 4KB ZIP 举报
温馨提示
函数式编程-Stream流-三更 Author、Book、测试代码
资源推荐
资源详情
资源评论
收起资源包目录
sofwoin.zip (3个子文件)
sofwoin
Book.java 427B
Test.java 15KB
Author.java 594B
共 3 条
- 1
资源评论
Tokey_W
- 粉丝: 117
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功