一.lambda表达式
1.lambda表达式写法
//原生匿名内部类写法
new Callable<String>() {
@Override
public String call() throws Exception {
return null;
}
}.call();
//lambda表达式写法
Callable <String> callable=()-> {
System.out.println("函数式接口");
return "sada";
};
System.out.println(callable.call());
//lambda表达式写法 带return
Callable <String> callable1=()-> {return "123";};
System.out.println(callable1.call());
//lambda表达式写法 不带return 默认会加上return
Callable <String> callable2=()-> "321";
System.out.println(callable2.call());
2.自定义函数式接口
//自定义函数式接口
StudentDao studentDao = new StudentDao() {
@Override
public void insert(Student student) {
System.out.println(student);
}
};
studentDao.insert(new Student());
//lambda表达式写法
StudentDao studentDao1=(Student student)->{
System.out.println("插入学生的数据");
};
studentDao1.insert(new Student());
//lambda表达式精简写法 自动推断student的值
StudentDao studentDao2=(student)->System.out.println("插入学生的数据");
studentDao2.insert(new Student());
3.jdk提供的函数式接口供我们使用
/**
* jdk提供的函数时接口供我们使用
*/
//Function接口 一个输入,一个输出 (输入输出一般是不同的类型)
Function<String,Integer> f1=(str)->str.length();
System.out.println(f1.apply("asdadas"));
//Supplier接口 没有输入,一个输出
Supplier<String> s1=()->"输出一个值";
System.out.println(s1.get());
//Consumer接口 一个输入,没有输出
Consumer<Integer> c1=(num)-> System.out.println(num);
c1.accept(20000);
//BiConsumer接口 两个输入,没有输出
BiConsumer<String,String> b1=(str,str1)-> System.out.println(str+str1);
b1.accept("abc","cba");
//UnaryOperator接口 代表一个输入,一个输出 (输入输出是相同类型)
UnaryOperator <String> u1=(str)->str+"mnb";
System.out.println(u1.apply("cba124"));
//BiFunction接口 代表两个输入,一个输出。(输入输出一般是不用类型)
BiFunction<String,String,Integer> bf1=(str,str1)->321;
System.out.println(bf1.apply("asd", "asdsa"));
//BinaryOperator接口 两个输入一个输出,(输入输出是相同类型)
BinaryOperator<String> bo1=(abc,nju)->abc+nju;
System.out.println(bo1.apply("cba","nba"));
//快速遍历一个 list集合的元素
List<String> list= Arrays.asList("a","b","c");
list.forEach(System.out::println);
4.方法引用的使用方法
/**
* 方法引用用法
*/
//静态方法引用
Supplier sf=Fun::get;
System.out.println(sf.get());
//静态方法引用
Consumer<Integer> consumer=Fun::get1;
consumer.accept(10);
//静态方法引用
Function<String,Integer> function=Fun::get2;
System.out.println(function.apply("200"));
//实例方法引用
Supplier sf1=new Fun()::get3;
System.out.println(sf1.get());
//实例方法引用
Consumer<Integer> consumer1=new Fun()::get4;
consumer1.accept(2035);
//对象方法引用 无参
Consumer<Ji> consumer2=(Ji ji)->new Fun().get5();
consumer2.accept(new Ji());
//对象方法引用 带参
Consumer<Ji> consumer3=(Ji ji)->new Ji().get6(ji);;
consumer3.accept(new Ji());
//对象方法引用 无参
Consumer<Fun> consumer4=Fun::get5;
consumer4.accept(new Fun());
//构造方法引用(无参)
Supplier supplier12=()->new Fun();
supplier12.get();
//构造方法引用(无参)
Supplier supplier123=Fun::new;
supplier123.get();
//构造方法引用(无参)
Supplier supplierList= ArrayList::new; List <String> a = (ArrayList)supplierList.get();
//构造方法引用(带参数)
Consumer<String> consumer56=(str)->new Fun(str);
consumer56.accept("afshh");
//构造方法引用(带参数)
Consumer<String> consumerfun=Fun::new;
consumerfun.accept("afggJQK");
Function<String,Fun> stringFunFunction=(str)->new Fun(str);
stringFunFunction.apply("俺是个");
Function<String,Fun> stringFunFunction1=Fun::new;
stringFunFunction1.apply("小傻瓜");
class Fun{
public Fun(){
System.out.println("无参构造函数");
}
public Fun(String str){
System.out.println("String参构造函数"+str);
}
public static String get(){
return "hssjkghj";
}
public static void get1(Integer i){
System.out.println(i);
}
public static Integer get2(String i){
return Integer.parseInt(i);
}
public String get3(){
return "sadsasda";
}
public void get4(Integer i){
System.out.println(i);
}
public void get5(){
System.out.println("get5");
}
public void get6(Abcd abcd){
System.out.println("get6");
}
}
class Abcd{}
class Ji extends Abcd{
public void get6(Abcd abcd){
System.out.println("get6 ji");
}
}
二,Sterm流
/**
* Stream流生成的一些方式
*/
//遍历一个数组
String age[]={"abc","cba","nba"};
Stream<String> age1 = Stream.of(age);
age1.forEach(System.out::println);
//遍历一个集合
Arrays.asList("3", "7", "8", "ghj")
.stream()
.forEach(System.out::println);
int i=0;
//Stream用法3
Stream.generate(()->7).limit(10).forEach(System.out::println);
//Stream用法4
Stream.iterate(3,x->x+1)
.limit(10)
.forEach(System.out::println);
//Stream用法5
String str12="afgdfhd";
str12.chars().forEach(System.out::println);
//用Stream处理集合元素 输出集合中的偶数并求和
System.out.println(Arrays.asList(3,4,6,7)
.stream()
.filter((x)->x%2==0)
.mapToInt(x->x)
.sum());
//用Stream处理集合元素 输出集合中的元素 (串行地情况下,一般会返回第一个结果,如果是并行的情况,那就不能确保是第一个)
System.out.println(Arrays.asList(1,4,0,3,6,7)
.stream()
.filter((x)->x%2==0)
.findAny()
.get());
//用Stream处理集合元素 输出集合中的第一个偶数
System.out.println(Arrays.asList(1,0,3,4,6,7)
.stream()
.filter((x)->x%2==0)
.findFirst()
.get());
//求集合中的最大值
System.out.println(Arrays.asList(2,7,8,9,112,1)
.stream()
.max((g,b)->g-b)
.get());
System.out.println("-------------------------------------");
//求集合中的最大值方法二
System.out.println(Arrays.asList(2,7,8,9,112,1)
.stream()
.sorted((h,j)->j-h)
.findFirst()
.get());
System.out.println("-------------------------------------");
//求集合中的最小值
System.out.println(Arrays.asList(2,7,8,9,112,1)
.stream()
.min((g,b)->g-b)
.get());
System.out.println("-------------------------------------");
//求集合中的最小值方法二 先排序后拿出第一个值
System.out.println(Arrays.asList(2,7,8,9,112,1)
.stream()
.sorted()
.findFirst()
.get());
//排序字符串 自然排序
Arrays.asList("abc","acb","aba","t","o")
.stream()
.sorted()
.forEach(System.out::println);
System.out.println("-------------------------------------");
//排序字符串 根据长度排序
Arrays.asList("abc","acb","absa","t","o")
.stream()
.sorted((h,l)->h.length()-l.length())
.forEach(System.out::println);
System.out.println("-------------------------------------");
/**
* 对集合处理之后返回一个 集合 collect
*/
List<String> collect = Arrays.asList("abc", "acb", "absa", "t", "rete")
.stream()
.sorted((r,t)->r.length()-t.length())
.collect(Collectors.toList());
collect.forEach(System.out::println);
System.out.println("-------------------------------------");
/**
* 对list集合去重后 返回一个集合 distinct
*/
List<String> collect1 = Arrays.asList("abc", "abc", "abc", "t", "rete")
.stream()
.distinct()
.collect(Collectors.toList());
collect1.forEach(System.out::println);
System.out.println("-------------------------------------");
/**
* 对set操作后 返回一个集合 set集合是无序不可重复的 collect
*/
Set<String> collect2 = Arrays.asList("abc", "abc", "abc", "t", "rete")
.stream()
.collect(Collectors.toSet());
collect2.forEach(System.out::println);
System.out.println("-------------------------------------");
/**
* 打印20-30这样的数据 skip跳过19位数 limit限制50次操作
*/
Stream.iterate(1,x->x+1).limit(50).skip(19).limit(11).forEach(System.out::println);
System.out.println("-------------------------------------");
/**
* 将一串字符串 "22,23,25,20,19,15,65" 进行求和
*/
String str3="22,23,25,20,19,15,65";
System.out.println(Stream.of(str3.split(",")).mapToInt(f -> Integer.valueOf(f)).sum());
System.out.println(Stream.of(str3.split(",")).mapToInt(Integer::valueOf).sum());
System.out.println(Stream.of(str3.split(",")).map(x->Integer.valueOf(x)).mapToInt(x->x).sum());
System.out.println(Stream.of(str3.split(",")).map(Integer::valueOf).mapToInt(x->x).sum());
System.out.println("重点****************************************重点");
/**
*用Stream操作集合中存储对象元素
*/
String name="Java,Python,C#";
Stream.of(name.split(",")).map(x->new Persion(x)).forEach(System.out::println);
Stream.of(name.split(",")).map(Persion::new).forEach(System.out::println);
System.out.println("-------------------------------------");
/**
* 操作静态方法build
*/
Stream.of(name.split(",")).map(Persion::build).forEach(System.out::println);
System.out.println("-------------------------------------");
/**
*打印出 str4数值字符串序列的值,并求和 peek打印
*/
String str4="22,23,25,20,19,15,65";
int sum = Stream.of(str4.split(",")).peek(System.out::println).mapToInt(Integer::valueOf).sum();
System.out.println(sum);
System.out.println("-------------------------------------");
/**
* allmatch 所有元素都满足条件才会返回 true anyMatch其中一个满足条件就会返回true
*/
System.out.println(Arrays.asList(0, 2, 7, 9, 2).stream().allMatch(x -> x > 0));
System.out.println(Arrays.asList(0, 2, 7, 9, 2).stream().anyMatch(x -> x > 0));
class Persion{
private static String name;
public Persion() {
}
public static Persion build(String age){
name=age;
return new Persion();
}
public Persion(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Persion{" +
"name='" + name + '\'' +
'}';
}
}
版权声明:本文为qq_27303687原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。