init
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/*
|
||||
在jdk中,提供了一些常用的函数式接口供开发人员使用
|
||||
常用的函数式接口_Consumer
|
||||
void accept(T t) 对给定的参数执行此操作。
|
||||
|
||||
注意:
|
||||
1.Consumer表示的是一个消费,只接收参数,没有返回值
|
||||
2.什么时候要使用Consumer的接口??
|
||||
当我们程序员,要定义一个函数式接口,接口中的方法只接收一个参数,没有返回值的方法,直接使用Consumer
|
||||
|
||||
需求:定义出一个使用常用函数式接口Consumer作为参数,并对字符串进行消费操作的方法(大写打印,小写打印)
|
||||
*/
|
||||
public class ConsumerDemo02 {
|
||||
public static void main(String[] args) {
|
||||
/*print(new Consumer<String>() {
|
||||
@Override
|
||||
public void accept(String s) {
|
||||
System.out.println(s.toUpperCase());
|
||||
}
|
||||
},"aBcD");*/
|
||||
print(s->System.out.println(s.toUpperCase()),"aBcD");
|
||||
|
||||
/*print(new Consumer<String>() {
|
||||
@Override
|
||||
public void accept(String str) {
|
||||
System.out.println(str.toLowerCase());
|
||||
}
|
||||
},"aBcD");*/
|
||||
print(ss -> System.out.println(ss.toLowerCase()), "aBcD");
|
||||
}
|
||||
|
||||
//定义出一个使用常用函数式接口Consumer作为参数,并对字符串进行消费操作的方法(大写打印,小写打印)
|
||||
public static void print(Consumer<String> consumer, String str) {
|
||||
//消费字符串
|
||||
consumer.accept(str);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/*
|
||||
default Consumer<T> andThen(Consumer<? super T> after) 返回一个组成的 Consumer ,依次执行此操作,然后执行 after操作。
|
||||
需求:我要使用消费者Consumer接口,对同一个字符串进行大写,和小写的消费操作
|
||||
*/
|
||||
public class ConsumerDemo03 {
|
||||
public static void main(String[] args) {
|
||||
printStr(s-> System.out.println(s.toUpperCase()),s-> System.out.println(s.toLowerCase()),"AbCd");
|
||||
}
|
||||
|
||||
public static void printStr(Consumer<String> c1, Consumer<String> c2,String s) {
|
||||
/*c1.accept(s);
|
||||
c2.accept(s);*/
|
||||
c1.andThen(c2).accept(s);
|
||||
}
|
||||
}
|
||||
52
s_day07/src/com/inmind/functional_interface_02/Demo01.java
Normal file
52
s_day07/src/com/inmind/functional_interface_02/Demo01.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
/*
|
||||
.自定义函数式接口:有且仅有一个必须重写的抽象方法的接口
|
||||
|
||||
在java中可以使用一个注解@FunctionalInferface,来验证一个接口是否是函数式接口
|
||||
|
||||
函数式接口的使用:
|
||||
1.作为参数类型
|
||||
2.返回值类型
|
||||
*/
|
||||
public class Demo01 {
|
||||
public static void main(String[] args) {
|
||||
int a = 10;
|
||||
int b = 20;
|
||||
//调用使用函数式接口的方法
|
||||
/*int result = getValue(new MyInterface() {
|
||||
@Override
|
||||
public int getSum(int a, int b) {
|
||||
//求和的功能
|
||||
return a + b;
|
||||
}
|
||||
}, a, b);*/
|
||||
// int result = getValue((int i, int j)->{ return i + j;},a,b);
|
||||
int result = getValue(( i, j)-> i + j,a,b);
|
||||
System.out.println(result);
|
||||
//-------------------------------------------------
|
||||
//调用使用函数式接口作为返回值的方法
|
||||
MyInterface myInterface = getMyInterface();
|
||||
int value = getValue(myInterface, a, b);
|
||||
System.out.println(value);
|
||||
}
|
||||
|
||||
//定义一个有整数返回值,参数列表式函数式接口,2个整数的静态方法
|
||||
public static int getValue(MyInterface myInterface, int a, int b) {
|
||||
int result = myInterface.getSum(a, b);
|
||||
return result;
|
||||
}
|
||||
|
||||
//定义出一个函数式接口作为返回值类型,参数列表定义为无参
|
||||
public static MyInterface getMyInterface() {
|
||||
/*MyInterface myInterface = new MyInterface() {
|
||||
@Override
|
||||
public int getSum(int a, int b) {
|
||||
return a*b;
|
||||
}
|
||||
};*/
|
||||
// MyInterface myInterface = (int a, int b)->{return a*b;};
|
||||
/*MyInterface myInterface = ( a, b)-> a*b;
|
||||
return myInterface;*/
|
||||
return ( a, b)-> a*b;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/*
|
||||
17.常用的函数式接口-Function
|
||||
Interface Function<T,R>:就是一个数据工厂,用来数据转换
|
||||
参数类型:
|
||||
T:原料的类型--参数
|
||||
R:产品的类型--返回值
|
||||
抽象方法:
|
||||
R apply(T t) 将此函数应用于给定的参数。
|
||||
|
||||
//需求:"100"--->100
|
||||
T:String
|
||||
R:Integer
|
||||
*/
|
||||
public class FunctionDemo10 {
|
||||
public static void main(String[] args) {
|
||||
String s = "100";
|
||||
// method(t->{return Integer.parseInt(t);},s);
|
||||
method(t-> Integer.parseInt(t),s);
|
||||
}
|
||||
//接收一个字符串,使用数据工厂Function,转换数据为Integer
|
||||
public static void method(Function<String,Integer> function,String str) {
|
||||
Integer result = function.apply(str);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/*
|
||||
18. Function中的andThen方法
|
||||
default <V> Function<T,V> andThen(Function<? super R,? extends V> after)
|
||||
返回一个组合函数,首先将该函数应用于其输入,然后将 after函数应用于结果。
|
||||
|
||||
需求:
|
||||
1."10"->10
|
||||
2.10->100
|
||||
分析:
|
||||
1.一个Function将“10”->10
|
||||
2.一个Function将10->100
|
||||
*/
|
||||
public class FunctionDemo11 {
|
||||
public static void main(String[] args) {
|
||||
String str = "10";
|
||||
method(s -> Integer.parseInt(s),i->i*10,str);
|
||||
}
|
||||
|
||||
public static void method(Function<String, Integer> f1,Function<Integer, Integer> f2,String s) {
|
||||
/*//1.一个Function将“10”->10
|
||||
Integer result1 = f1.apply(s);
|
||||
//2.一个Function将10->100
|
||||
Integer result2 = f2.apply(result1);
|
||||
System.out.println(result2);*/
|
||||
// System.out.println(f2.apply(f1.apply(s)));
|
||||
//使用f1的apply方法对s进行转换,再将它的结果作为f2的apply方法的参数进行转换
|
||||
System.out.println(f1.andThen(f2).andThen(f2).andThen(f2).apply(s));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/*
|
||||
请使用Function进行函数模型的拼接,按照顺序需要执行的多个函数操作为:
|
||||
"赵丽颖,20"
|
||||
1. 将字符串截取数字年龄部分,得到字符串;
|
||||
2. 将上一步的字符串转换成为int类型的数字;(String->integer)
|
||||
3. 将上一步的int数字累加100,得到结果int数字。(integer->integer)
|
||||
*/
|
||||
public class FunctionTest12 {
|
||||
public static void main(String[] args) {
|
||||
String str = "赵丽颖,20";
|
||||
method(s -> Integer.parseInt(s.split(",")[1]),i->i+100,str);
|
||||
}
|
||||
|
||||
public static void method(Function<String, Integer> f1, Function<Integer, Integer> f2, String s) {
|
||||
/*Integer result1 = f1.apply(s);
|
||||
Integer result2 = f2.apply(result1);
|
||||
System.out.println(result2);*/
|
||||
|
||||
Integer result = f1.andThen(f2).apply(s);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
//函数式接口:有且仅有一个必须重写的抽象方法的接口
|
||||
@FunctionalInterface
|
||||
public interface MyInterface {
|
||||
//有参有返回值的方法
|
||||
int getSum(int a, int b);
|
||||
/*boolean equals(Object obj);
|
||||
String toString();
|
||||
int hashCode();*/
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface MyInterface1 {
|
||||
void method1(int i);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface MyInterface2 {
|
||||
void method1(String i);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface MyInterface3 {
|
||||
void method1(ArrayList<Integer> i);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/*
|
||||
12.常用的函数式接口_Predicate
|
||||
Interface Predicate<T>
|
||||
boolean test(T t) 用来判断或者筛选数据
|
||||
Predicate函数式接口的作用:用来进行数据筛选,判断指定的数据是否符合业务
|
||||
|
||||
什么时候要使用Predicat呢??
|
||||
当我们要定义一个有一个参数,并且返回值类型为boolean的函数式接口时,直接使用Predicate
|
||||
|
||||
需求:使用Predicate接口判断指定的字符串长度是否>3(包含java)
|
||||
*/
|
||||
public class PredicateDemo05 {
|
||||
public static void main(String[] args) {
|
||||
//字符串长度是否>3
|
||||
/*method(new Predicate<String>() {
|
||||
|
||||
@Override
|
||||
public boolean test(String s) {
|
||||
return s.length()>3;
|
||||
}
|
||||
},"abcd");*/
|
||||
System.out.println(method(s -> s.length() > 3, "abcd"));
|
||||
|
||||
//字符串包含java
|
||||
System.out.println(method(ss -> ss.contains("java"), "woaijava"));
|
||||
}
|
||||
//定义一个返回值为boolean,参数是Predicate接口,String的静态方法
|
||||
public static boolean method(Predicate<String> predicate,String str) {
|
||||
boolean result = predicate.test(str);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/*
|
||||
Predicate的and方法
|
||||
|
||||
需求:对一个字符串,使用2个predicate,判断它是否以h开头,它的长度是否大于3
|
||||
|
||||
boolean test(T)
|
||||
*/
|
||||
public class PredicateDemo06 {
|
||||
public static void main(String[] args) {
|
||||
// method(s->{return s.startsWith("h");},s->s.length()>3,"hello java");
|
||||
method(s->s.startsWith("h"),s->s.length()>3,"hello java");
|
||||
}
|
||||
|
||||
public static void method(Predicate<String> p1, Predicate<String> p2,String str) {
|
||||
/*boolean results1 = p1.test(str);
|
||||
boolean results2 = p2.test(str);
|
||||
boolean result = results1 && results2;
|
||||
System.out.println(result);*/
|
||||
System.out.println(p1.and(p2).test(str));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/*
|
||||
Predicate的or方法
|
||||
|
||||
需求:对一个字符串,使用2个predicate,判断它是否以h开头或者它的长度是否大于3
|
||||
|
||||
boolean test(T)
|
||||
*/
|
||||
public class PredicateDemo07 {
|
||||
public static void main(String[] args) {
|
||||
method(s->s.startsWith("h"),s->s.length()>3,"hello");
|
||||
}
|
||||
|
||||
public static void method(Predicate<String> p1, Predicate<String> p2,String str) {
|
||||
/*boolean results1 = p1.test(str);
|
||||
boolean results2 = p2.test(str);
|
||||
boolean result = results1 || results2;
|
||||
System.out.println(result);*/
|
||||
System.out.println(p1.or(p2).test(str));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/*
|
||||
Predicate的negate方法
|
||||
|
||||
需求:对一个字符串,使用1个predicate,判断它是否以h开头,结果取反
|
||||
|
||||
boolean test(T)
|
||||
*/
|
||||
public class PredicateDemo08 {
|
||||
public static void main(String[] args) {
|
||||
method(s->s.startsWith("h"),"hello");
|
||||
}
|
||||
|
||||
public static void method(Predicate<String> p1,String str) {
|
||||
/*boolean result = p1.test(str);
|
||||
System.out.println(!result);*/
|
||||
System.out.println(p1.negate().test(str));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/*
|
||||
数组当中有多条“姓名+性别”的信息如下,请通过Predicate接口的拼装将符合要求的字符串筛选到集合ArrayList中,需要同时满足两个条件:
|
||||
|
||||
1. 必须为女生;
|
||||
2. 姓名为4个字
|
||||
|
||||
分析:
|
||||
1.一个predicate判断是否是女生
|
||||
2.一个predicate判断名字是否长度为4
|
||||
*/
|
||||
public class PredicateTest09 {
|
||||
public static void main(String[] args) {
|
||||
String[] array = { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男", "赵丽颖,女" };
|
||||
//boolean test(T t) :t->{return xxx;}
|
||||
mehtod(s->{
|
||||
String[] strs = s.split(",");
|
||||
return strs[1].equals("女");
|
||||
},s->s.split(",")[0].length()==4,array);
|
||||
}
|
||||
|
||||
|
||||
public static void mehtod(Predicate<String> p1, Predicate<String> p2,String[] arr) {
|
||||
//创建出保存数据的集合
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
//遍历数组,判断是否满足2个条件,满足则保存到集合
|
||||
for (String s : arr) {
|
||||
if (p1.and(p2).test(s)) {//判断2个条件是否满足
|
||||
list.add(s);
|
||||
}
|
||||
}
|
||||
System.out.println(list);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.inmind.functional_interface_02;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/*
|
||||
11.常用的函数式接口_Supplier
|
||||
|
||||
Interface Supplier<T>:表示生产者
|
||||
T:泛型,用来决定生产者生产的数据类型
|
||||
T get() 获得结果。
|
||||
|
||||
当我们开发人员,要定义一个无参,有返回值的函数式接口时,直接使用Supplier
|
||||
|
||||
*/
|
||||
public class SupplierDemo04 {
|
||||
public static void main(String[] args) {
|
||||
//给我生产一个字符串
|
||||
// getValue(()->{return "Hello World";});
|
||||
getValue(()->"Hello World");
|
||||
//给我生产一个整数
|
||||
// getValue(()->{return 100;});
|
||||
getValue(()-> 100);
|
||||
}
|
||||
//定义出一个用Supplier作为参数,将结果打印
|
||||
public static void getValue(Supplier supplier){
|
||||
System.out.println(supplier.get());
|
||||
}
|
||||
}
|
||||
50
s_day07/src/com/inmind/lambda_01/Demo01.java
Normal file
50
s_day07/src/com/inmind/lambda_01/Demo01.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package com.inmind.lambda_01;
|
||||
/*
|
||||
使用匿名内部类实现多线程
|
||||
|
||||
匿名内部类的语法:
|
||||
new 父类|父接口(){
|
||||
要重写的方法
|
||||
}
|
||||
线程的启动方式有2种:
|
||||
1.继承Thread
|
||||
2.实现Runnable接口(重点)
|
||||
-----------------------------------------------------------------
|
||||
函数式编程思想:重点关注做什么,而不是怎么做
|
||||
|
||||
为了编写线程任务代码,我们必须面向对象,new Runnable(),再重写run()(public void run()),其实
|
||||
我们只是要编写run方法中的方法体而已。
|
||||
|
||||
我们可以这么理解,函数式编程思想:做什么就是要重写的方法的方法体,而之前的对象创建,重写方法的语法都省略掉
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
Lambda的格式和前提条件
|
||||
函数式编程思想:重点关注做什么,而不是怎么做,在这个概念中做什么,实际上就是一个方法的方法体。
|
||||
针对一个方法而言,它有3个核心:1.参数列表 2.方法体 3.返回值
|
||||
Lambda表达式的所需语法的符号:();->;{}
|
||||
|
||||
():参数列表
|
||||
->:参数列表用来调用哪段代码
|
||||
{}:方法体,根据是否有返回值,动态添加return
|
||||
|
||||
Lambda表达式的前提条件:
|
||||
1.需要一个有且仅有一个必须实现抽象方法的接口(函数式接口)
|
||||
2.接口作为参数或者变量类型
|
||||
|
||||
*/
|
||||
public class Demo01 {
|
||||
public static void main(String[] args) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(Thread.currentThread().getName()+"线程启动");
|
||||
}
|
||||
}).start();
|
||||
//-------------------------以下就使用函数式编程思想优化以上代码(lambda表达式)------------------------------
|
||||
new Thread(() -> {
|
||||
System.out.println(Thread.currentThread().getName() + "线程启动");
|
||||
}).start();
|
||||
///------------------------------以下为lambda的简写形式----------------------
|
||||
new Thread(() -> System.out.println(Thread.currentThread().getName() + "线程启动")).start();
|
||||
}
|
||||
}
|
||||
42
s_day07/src/com/inmind/lambda_01/Demo02.java
Normal file
42
s_day07/src/com/inmind/lambda_01/Demo02.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.inmind.lambda_01;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
/*
|
||||
5.使用Lambda表达式简化有参数有返回值的方法(比较器)
|
||||
使用比较器,对学生对象按成绩降序排序
|
||||
|
||||
|
||||
Lambda表达式的前提条件:
|
||||
1.需要一个有且仅有一个必须实现抽象方法的接口(函数式接口中可以有多个抽象方法)
|
||||
2.接口作为参数或者变量类型
|
||||
|
||||
扩展:为什么在一个接口中某些抽象方法不需要被重写???
|
||||
在一个类中单继承和多实现,单继承的方法优先于接口的方法定义,导致父类的方法如果有跟
|
||||
接口中定义的重名方法,那就相当于是默认重写 extends Object
|
||||
|
||||
|
||||
*/
|
||||
public class Demo02 {
|
||||
public static void main(String[] args) {
|
||||
ArrayList<Student> students = new ArrayList<>();
|
||||
students.add(new Student("Jack", 21));
|
||||
students.add(new Student("Tom", 22));
|
||||
students.add(new Student("Marty", 18));
|
||||
System.out.println(students);
|
||||
|
||||
/*Collections.sort(students, new Comparator<Student>() {
|
||||
@Override
|
||||
public int compare(Student o1, Student o2) {
|
||||
return o2.getAge() - o1.getAge();
|
||||
}
|
||||
});*/
|
||||
//函数式编程思想lambda来实现
|
||||
// Collections.sort(students,(Student o1, Student o2)->{ return o1.getAge() - o2.getAge();});
|
||||
//-------------------------------以下为lambda的简写形式----------------------------------
|
||||
Collections.sort(students,(o1, o2)-> o2.getAge() - o1.getAge());
|
||||
|
||||
System.out.println(students);
|
||||
}
|
||||
}
|
||||
35
s_day07/src/com/inmind/lambda_01/Student.java
Normal file
35
s_day07/src/com/inmind/lambda_01/Student.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.inmind.lambda_01;
|
||||
|
||||
public class Student {
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Student(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Student{" +
|
||||
"name='" + name + '\'' +
|
||||
", age=" + age +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
21
s_day07/src/com/inmind/stream_03/CollectDemo13.java
Normal file
21
s_day07/src/com/inmind/stream_03/CollectDemo13.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
33.Stream收集的练习
|
||||
请通过Stream流的方式,将下面数组当中的元素添加(收集)到List集合当中:
|
||||
*/
|
||||
public class CollectDemo13 {
|
||||
public static void main(String[] args) {
|
||||
Integer[] arr = {1,2,3,4,5};
|
||||
//获取Stream的2种方法:1.单列集合的stream 2.Stream.of(T...)
|
||||
/*Stream<Integer> stream = Stream.of(arr);
|
||||
List<Integer> list = stream.collect(Collectors.toList());*/
|
||||
//Stream对数据进行高效的拷贝操作
|
||||
List<Integer> list = Stream.of(arr).collect(Collectors.toList());
|
||||
System.out.println(list);
|
||||
}
|
||||
}
|
||||
19
s_day07/src/com/inmind/stream_03/ConcatDemo08.java
Normal file
19
s_day07/src/com/inmind/stream_03/ConcatDemo08.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
28.Stream流中的常用方法_concat
|
||||
static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b)
|
||||
合并2个流,生成一个新的流
|
||||
*/
|
||||
public class ConcatDemo08 {
|
||||
public static void main(String[] args) {
|
||||
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 5);
|
||||
Stream<Integer> stream2 = Stream.of( 6, 7, 8, 9, 10);
|
||||
|
||||
Stream<Integer> stream = Stream.concat(stream1, stream2);
|
||||
// stream.forEach(System.out::println);
|
||||
stream.forEach(s-> System.out.println(s));
|
||||
}
|
||||
}
|
||||
15
s_day07/src/com/inmind/stream_03/CountDemo05.java
Normal file
15
s_day07/src/com/inmind/stream_03/CountDemo05.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
Stream流中的常用方法_count
|
||||
long count() 返回此流中的元素数
|
||||
*/
|
||||
public class CountDemo05 {
|
||||
public static void main(String[] args) {
|
||||
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
|
||||
long count = stream.count();
|
||||
System.out.println(count);
|
||||
}
|
||||
}
|
||||
46
s_day07/src/com/inmind/stream_03/Demo01.java
Normal file
46
s_day07/src/com/inmind/stream_03/Demo01.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
20.传统操作集合和Stream流操作集合的对比
|
||||
|
||||
需求:
|
||||
1. 首先筛选所有姓张的人;
|
||||
2. 然后筛选名字有三个字的人;
|
||||
3. 最后进行对结果进行打印输出。
|
||||
|
||||
|
||||
*/
|
||||
public class Demo01 {
|
||||
public static void main(String[] args) {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("张无忌");
|
||||
list.add("周芷若");
|
||||
list.add("赵敏");
|
||||
list.add("张强");
|
||||
list.add("张三丰");
|
||||
|
||||
ArrayList<String> zhangList = new ArrayList<>();
|
||||
//1. 首先筛选所有姓张的人;
|
||||
for (String s : list) {
|
||||
if (s.startsWith("张")) {
|
||||
zhangList.add(s);
|
||||
}
|
||||
}
|
||||
System.out.println(zhangList);
|
||||
ArrayList<String> newList = new ArrayList<>();
|
||||
//2. 然后筛选名字有三个字的人;
|
||||
for (String s : zhangList) {
|
||||
if (s.length() == 3) {
|
||||
newList.add(s);
|
||||
}
|
||||
}
|
||||
//3. 最后进行对结果进行打印输出
|
||||
System.out.println(newList);
|
||||
System.out.println("------------------------------------------");
|
||||
//---------------------------------Stream流----------------------------------
|
||||
list.stream().filter(s->s.startsWith("张")).filter(s->s.length() == 3).forEach(s-> System.out.println(s));
|
||||
}
|
||||
}
|
||||
22
s_day07/src/com/inmind/stream_03/FilterDemo04.java
Normal file
22
s_day07/src/com/inmind/stream_03/FilterDemo04.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
24.Stream流中的常用方法_filter
|
||||
Stream<T> filter(Predicate<? super T> predicate) 对指定的流进行过滤操作
|
||||
知识点回顾:
|
||||
Predicate:接收一个参数,返回一个boolean的结果,用来进行数据筛选
|
||||
抽象方法:boolean test(T t) ---> t->{return ...}
|
||||
|
||||
filter的执行原理:它会将stream流中的每个元素,作为以上Lambda表达式的参数,执行方法体,如果返回的是true,那就会把当前元素保存在新流中,如果是false,舍弃数据
|
||||
|
||||
*/
|
||||
public class FilterDemo04 {
|
||||
public static void main(String[] args) {
|
||||
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
|
||||
//只要大于3的数据
|
||||
Stream<Integer> stream1 = stream.filter(t -> t > 3);
|
||||
stream1.forEach(s-> System.out.println(s));
|
||||
}
|
||||
}
|
||||
26
s_day07/src/com/inmind/stream_03/ForeachDemo03.java
Normal file
26
s_day07/src/com/inmind/stream_03/ForeachDemo03.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
23.Stream流中的常用方法_forEach
|
||||
void forEach(Consumer<? super T> action) 对此流的每个元素执行操作。
|
||||
|
||||
知识点回顾:
|
||||
Consumer:接收一个参数,没有返回值
|
||||
抽象方法:void accept(T t) ---> t->{}
|
||||
|
||||
foreach的执行原理:它会将Stream流中的每个元素,作为以上Consumer的参数执行方法体
|
||||
|
||||
*/
|
||||
public class ForeachDemo03 {
|
||||
public static void main(String[] args) {
|
||||
//对流中每个元素进行打印消费
|
||||
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
|
||||
/*stream.forEach(i->{
|
||||
System.out.println(i);
|
||||
});*/
|
||||
|
||||
stream.forEach(i->System.out.println(i));
|
||||
}
|
||||
}
|
||||
16
s_day07/src/com/inmind/stream_03/LimitDemo06.java
Normal file
16
s_day07/src/com/inmind/stream_03/LimitDemo06.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
26.Stream流中的常用方法_limit
|
||||
Stream<T> limit(long maxSize) 返回由此流的元素组成的流,截短长度不能超过 maxSize 。
|
||||
将制定的流的前几个元素,组成一个新的流
|
||||
*/
|
||||
public class LimitDemo06 {
|
||||
public static void main(String[] args) {
|
||||
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7);
|
||||
Stream<Integer> stream1 = stream.limit(5);
|
||||
stream1.forEach(s-> System.out.println(s));
|
||||
}
|
||||
}
|
||||
29
s_day07/src/com/inmind/stream_03/MapDemo09.java
Normal file
29
s_day07/src/com/inmind/stream_03/MapDemo09.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
29.Stream中的常用方法_map
|
||||
将一种类型的流的数据,全部映射(转换)成另一种数据类型的流的方法
|
||||
<R> Stream<R> map(Function<? super T,? extends R> mapper)
|
||||
|
||||
知识点回顾:
|
||||
Function<T,R>:数据工厂,用来数据转换
|
||||
T:参数
|
||||
R:返回值
|
||||
抽象方法: R apply(T t)
|
||||
lambda表达式:t->{return }
|
||||
map方法的执行过程:将流中的每个元素,作为参数传入以上Lambda表达式中,将转换的结果全部保存到新的流
|
||||
|
||||
map:可以进行数据的拷贝,或者将保存了对象数据的字符串直接转为自定义对象
|
||||
*/
|
||||
public class MapDemo09 {
|
||||
public static void main(String[] args) {
|
||||
//{"10","100","12","32","55"}-->整数流
|
||||
Stream<String> stream = Stream.of("10", "100", "12", "32", "55");
|
||||
Stream<Integer> stream1 = stream.map(s -> {
|
||||
return Integer.parseInt(s);
|
||||
});
|
||||
stream1.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
16
s_day07/src/com/inmind/stream_03/Person.java
Normal file
16
s_day07/src/com/inmind/stream_03/Person.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person{" +
|
||||
"name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
15
s_day07/src/com/inmind/stream_03/SkipDemo07.java
Normal file
15
s_day07/src/com/inmind/stream_03/SkipDemo07.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
27.Stream流中的常用方法_skip
|
||||
Stream<T> skip(long n)
|
||||
跳过指定流的前几个元素,将剩下的元素组成新的流
|
||||
*/
|
||||
public class SkipDemo07 {
|
||||
public static void main(String[] args) {
|
||||
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7);
|
||||
stream.skip(5).forEach(s-> System.out.println(s));
|
||||
}
|
||||
}
|
||||
53
s_day07/src/com/inmind/stream_03/StreamDemo02.java
Normal file
53
s_day07/src/com/inmind/stream_03/StreamDemo02.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
22.获取Stream流的两种方式(重点)
|
||||
Stream流的获取方式:
|
||||
1.单列集合Collection的stream方法,得到单列集合的stream流
|
||||
Collection接口
|
||||
default Stream<E> stream() 返回一个序列 Stream与此集合作为其来源。
|
||||
2.Stream接口的of方法,将多个数据组成一个流
|
||||
Stream接口
|
||||
static <T> Stream<T> of(T... values) 返回其元素是指定值的顺序排序流。
|
||||
*/
|
||||
public class StreamDemo02 {
|
||||
public static void main(String[] args) {
|
||||
//1.单列集合获取流
|
||||
//list集合
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
list.add("张无忌");
|
||||
list.add("周芷若");
|
||||
list.add("赵敏");
|
||||
list.add("张三丰");
|
||||
Stream<String> stream = list.stream();
|
||||
System.out.println(stream);
|
||||
//set集合
|
||||
HashSet<String> set = new HashSet<>();
|
||||
set.add("张无忌");
|
||||
set.add("周芷若");
|
||||
set.add("赵敏");
|
||||
set.add("张三丰");
|
||||
Stream<String> stream1 = set.stream();
|
||||
System.out.println(stream1);
|
||||
|
||||
//Map双列集合没有定义stream方法,只能通过转为单列集合间接获取流
|
||||
//间接获取流:1.所有的键的集合keySet() 2.所有的值的集合values() 3.所有的键值对对象的集合
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put("刘备", "孙尚香");
|
||||
Set<String> keys = map.keySet();
|
||||
Stream<String> stream2 = keys.stream();
|
||||
|
||||
Collection<String> values = map.values();
|
||||
Stream<String> stream3 = values.stream();
|
||||
|
||||
Set<Map.Entry<String, String>> entries = map.entrySet();
|
||||
Stream<Map.Entry<String, String>> stream4 = entries.stream();
|
||||
|
||||
//2.Stream的of方法
|
||||
Stream<Integer> stream5 = Stream.of(1, 2, 3, 4, 5);
|
||||
Stream<String> stream6 = Stream.of("a", "b", "c", "d");
|
||||
}
|
||||
}
|
||||
26
s_day07/src/com/inmind/stream_03/StreamToArrayDemo11.java
Normal file
26
s_day07/src/com/inmind/stream_03/StreamToArrayDemo11.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
31.把Stream中的数据收集到数组中
|
||||
Object[] toArray() 返回一个包含此流的元素的数组。
|
||||
<A> A[] toArray(IntFunction<A[]> generator)
|
||||
*/
|
||||
public class StreamToArrayDemo11 {
|
||||
public static void main(String[] args) {
|
||||
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
Object[] array = stream.toArray();
|
||||
System.out.println(Arrays.toString(array));
|
||||
//能不能在收集数据到数组中时,就确定类型??
|
||||
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
Integer[] array1 = stream1.toArray(i -> new Integer[i]);
|
||||
System.out.println(Arrays.toString(array1));
|
||||
|
||||
Stream<String> stream2 = Stream.of("1", "2", "3", "4", "5", "6", "7", "8", "9");
|
||||
// String[] array2 = stream2.toArray(s -> new String[s]);
|
||||
String[] array2 = stream2.toArray(String[]::new);
|
||||
System.out.println(Arrays.toString(array2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
32.把Stream中的数据收集到集合
|
||||
collect(Collector collector) 使用 Collector对此流的元素执行 mutable reduction操作。
|
||||
Collector:表示收集者是一个接口,它的实现类对象,可以使用工具类直接获取
|
||||
Collectors:
|
||||
toList:List集合收集者
|
||||
toSet:Set集合收集者
|
||||
*/
|
||||
public class StreamToCollectionDemo12 {
|
||||
public static void main(String[] args) {
|
||||
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
List<Integer> list = stream.collect(Collectors.toList());
|
||||
System.out.println(list);
|
||||
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9,9,9,9,9);
|
||||
Set<Integer> sets = stream1.collect(Collectors.toSet());
|
||||
System.out.println(sets);
|
||||
|
||||
}
|
||||
}
|
||||
115
s_day07/src/com/inmind/stream_03/Test10.java
Normal file
115
s_day07/src/com/inmind/stream_03/Test10.java
Normal file
@@ -0,0 +1,115 @@
|
||||
package com.inmind.stream_03;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
现在有两个ArrayList集合存储队伍当中的多个成员姓名,要求使用传统的for循环(或增强for循环)依次进行以下若干操作步骤:
|
||||
|
||||
1. 第一个队伍只要名字为3个字的成员姓名;
|
||||
2. 第一个队伍筛选之后只要前3个人;
|
||||
3. 第二个队伍只要姓张的成员姓名;
|
||||
4. 第二个队伍筛选之后不要前2个人;
|
||||
5. 将两个队伍合并为一个队伍;
|
||||
6.根据姓名创建Person对象
|
||||
7. 打印整个队伍的Person信息。
|
||||
*/
|
||||
public class Test10 {
|
||||
public static void main(String[] args) {
|
||||
List<String> one = new ArrayList<>();
|
||||
one.add("迪丽热巴");
|
||||
one.add("宋远桥");
|
||||
one.add("苏星河");
|
||||
one.add("老子");
|
||||
one.add("庄子");
|
||||
one.add("孙子");
|
||||
one.add("洪七公");
|
||||
one.add("洪八公");
|
||||
|
||||
List<String> two = new ArrayList<>();
|
||||
two.add("古力娜扎");
|
||||
two.add("张无忌");
|
||||
two.add("张三丰");
|
||||
two.add("赵丽颖");
|
||||
two.add("张二狗");
|
||||
two.add("张天爱");
|
||||
two.add("张三");
|
||||
|
||||
//1. 第一个队伍只要名字为3个字的成员姓名;
|
||||
//2. 第一个队伍筛选之后只要前3个人;
|
||||
ArrayList<String> newOne = new ArrayList<>();
|
||||
for (String s : one) {
|
||||
if (s.length() == 3) {
|
||||
newOne.add(s);
|
||||
}
|
||||
}
|
||||
ArrayList<String> newOne1 = new ArrayList<>();
|
||||
for (int i = 0; i < newOne.size(); i++) {
|
||||
if (i < 3) {
|
||||
newOne1.add(newOne.get(i));
|
||||
}
|
||||
}
|
||||
System.out.println(newOne1);
|
||||
|
||||
//3. 第二个队伍只要姓张的成员姓名;
|
||||
//4. 第二个队伍筛选之后不要前2个人;
|
||||
ArrayList<String> newTwo = new ArrayList<>();
|
||||
for (String s : two) {
|
||||
if (s.startsWith("张")) {
|
||||
newTwo.add(s);
|
||||
}
|
||||
}
|
||||
ArrayList<String> newTwo1 = new ArrayList<>();
|
||||
for (int i = 0; i < newTwo.size(); i++) {
|
||||
if (i >= 2) {
|
||||
newTwo1.add(newTwo.get(i));
|
||||
}
|
||||
}
|
||||
System.out.println(newTwo1);
|
||||
|
||||
//5. 将两个队伍合并为一个队伍;
|
||||
newOne1.addAll(newTwo1);
|
||||
System.out.println(newOne1);
|
||||
|
||||
//6.根据姓名创建Person对象
|
||||
ArrayList<Person> personList = new ArrayList<>();
|
||||
for (String s : newOne1) {
|
||||
personList.add(new Person(s));
|
||||
}
|
||||
|
||||
//7. 打印整个队伍的Person信息。
|
||||
for (Person person : personList) {
|
||||
System.out.println(person);
|
||||
}
|
||||
System.out.println("----------------------------------");
|
||||
//---------------------------------Stream------------------------------
|
||||
//1. 第一个队伍只要名字为3个字的成员姓名;
|
||||
Stream<String> stream = one.stream();
|
||||
Stream<String> oneStream1 = stream.filter(s -> s.length() == 3);
|
||||
//2. 第一个队伍筛选之后只要前3个人;
|
||||
Stream<String> oneStream2 = oneStream1.limit(3);
|
||||
|
||||
//3. 第二个队伍只要姓张的成员姓名;
|
||||
Stream<String> twoStream1 = two.stream().filter(s -> s.startsWith("张"));
|
||||
//4. 第二个队伍筛选之后不要前2个人;
|
||||
Stream<String> twoStream2 = twoStream1.skip(2);
|
||||
|
||||
//5. 将两个队伍合并为一个队伍;
|
||||
Stream<String> newStream = Stream.concat(oneStream2, twoStream2);
|
||||
//6.根据姓名创建Person对象
|
||||
Stream<Person> personStream = newStream.map(str -> {
|
||||
return new Person(str);
|
||||
});
|
||||
//7. 打印整个队伍的Person信息。
|
||||
personStream.forEach(System.out::println);
|
||||
//--------------------------------------------------------------------
|
||||
System.out.println("-----------------------------------------------------------------");
|
||||
Stream.concat(
|
||||
one.stream().filter(s -> s.length() == 3).limit(3),
|
||||
two.stream().filter(s -> s.startsWith("张")).skip(2))
|
||||
.map(s->new Person(s))
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
101
s_day07/src/com/inmind/test_04/StreamTest.java
Normal file
101
s_day07/src/com/inmind/test_04/StreamTest.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package com.inmind.test_04;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
需求说明
|
||||
本案例模拟学生成绩管理系统,需要实现以下业务功能:
|
||||
|
||||
生成测试学生数据(包含姓名、年龄、成绩)
|
||||
打印所有学生的完整信息
|
||||
筛选并打印成绩及格(≥60 分)的学生
|
||||
统计 18 岁以上且成绩及格的学生数量
|
||||
提取并打印所有学生的姓名
|
||||
将学生成绩转换为等级(A/B/C/D)并展示
|
||||
获取并展示第 3-5 名学生信息(按原始顺序)
|
||||
合并 "及格学生" 和 "前 2 名学生" 两个数据集并展示
|
||||
|
||||
|
||||
技术点说明
|
||||
Supplier 接口:用于提供学生数据列表,体现 "供给型" 功能
|
||||
Consumer 接口:用于消费学生对象(打印信息),体现 "消费型" 功能
|
||||
Predicate 接口:用于定义筛选条件(成绩及格、成年),体现 "判断型" 功能,包含条件组合(and)
|
||||
Function 接口:用于数据转换(学生→姓名、成绩→等级),体现 "功能型" 功能
|
||||
Stream 方法:
|
||||
forEach:遍历元素
|
||||
filter:按条件筛选
|
||||
count:统计元素数量
|
||||
map:元素转换
|
||||
skip:跳过指定数量元素
|
||||
limit:限制获取元素数量
|
||||
concat:合并两个流
|
||||
*/
|
||||
public class StreamTest {
|
||||
public static void main(String[] args) {
|
||||
//使用Supplier生成学生数据
|
||||
Supplier<List<Student>> supplier = () ->{
|
||||
List<Student> list = new ArrayList<Student>();
|
||||
list.add(new Student("张三",18,85.5));
|
||||
list.add(new Student("李四",19,75.5));
|
||||
list.add(new Student("王五",20,65.5));
|
||||
list.add(new Student("赵六",22,85.5));
|
||||
list.add(new Student("田七",21,45.5));
|
||||
list.add(new Student("孙八",18,93.5));
|
||||
list.add(new Student("周九",19,95.5));
|
||||
list.add(new Student("吴十",20,35.5));
|
||||
return list;
|
||||
};
|
||||
|
||||
//获取学生列表
|
||||
List<Student> students = supplier.get();
|
||||
//使用Consumer打印所有学生的完整信息
|
||||
Consumer<Student> printConsumer = student->System.out.println(student);
|
||||
|
||||
//使用消费者对学生列表消费
|
||||
students.stream().forEach(printConsumer);
|
||||
System.out.println("----------------------------------------");
|
||||
//使用Predicate得到筛选并打印成绩及格(≥60 分)的学生
|
||||
// Predicate<Student> predicate = student->{return student.getScore()>= 60;};
|
||||
Predicate<Student> predicate = student->student.getScore()>= 60;
|
||||
students.stream().filter(predicate).forEach(printConsumer);
|
||||
System.out.println("----------------------------------------");
|
||||
//统计 18 岁以上且成绩及格的学生数量
|
||||
Predicate<Student> isAdult = student -> student.getAge()>18;
|
||||
long count = students.stream().filter(predicate.and(isAdult)).count();
|
||||
System.out.println("18 岁以上且成绩及格的学生数量:"+count);
|
||||
//使用Function提取并打印所有学生的姓名(数据转换)
|
||||
Function<Student,String> toNameFunction = student-> student.getName();
|
||||
students.stream().map(toNameFunction).forEach(s->System.out.println(s));
|
||||
System.out.println("----------------------------------------");
|
||||
//使用Function将学生成绩转换为等级(A/B/C/D)并展示
|
||||
Function<Student,String> toScoreFunction = student-> {
|
||||
String level = "A";
|
||||
Double score = student.getScore();
|
||||
if (score >= 90) {
|
||||
level = "A";
|
||||
}else if (score >= 80) {
|
||||
level = "B";
|
||||
}else if (score >= 60) {
|
||||
level = "C";
|
||||
}else{
|
||||
level = "D";
|
||||
}
|
||||
return level;
|
||||
};
|
||||
students.stream().map(toScoreFunction).forEach(s->System.out.println(s));
|
||||
System.out.println("----------------------------------------");
|
||||
//获取并展示第 3-5 名学生信息(按原始顺序)
|
||||
students.stream().skip(2).limit(3).forEach(printConsumer);
|
||||
System.out.println("----------------------------------------");
|
||||
//合并 "及格学生" 和 "前 2 名学生" 两个数据集并展示
|
||||
Stream<Student> stream1 = students.stream().filter(predicate);
|
||||
Stream<Student> stream2 = students.stream().limit(2);
|
||||
Stream.concat(stream1, stream2).forEach(printConsumer);
|
||||
}
|
||||
}
|
||||
44
s_day07/src/com/inmind/test_04/Student.java
Normal file
44
s_day07/src/com/inmind/test_04/Student.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package com.inmind.test_04;
|
||||
|
||||
public class Student {
|
||||
// 包含姓名、年龄、成绩)
|
||||
private String name;
|
||||
private Integer age;
|
||||
private Double score;
|
||||
|
||||
public Student(String name, Integer age, Double score) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
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 Double getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void setScore(Double score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "姓名:"+name+",年龄:"+age+",成绩:"+score;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user