stream的创建方式
public static void main(String[] args) {
List<integer> list = new ArrayList<>(0);
list.add(1);
list.add(2);
list.add(3);
list.add(4);
System.out.println(list);
System.out.println("------------------");
//1.通过collection创建stream
list.stream().forEach((li) -> {
System.out.println(li);
});
System.out.println("------------------");
//方法的引用:是lambda表达式的一种简写的形式。如果lambda表达式方法中只是调用一个特定的已经存在的方法,则可以使用方法引用
//对象::实例对象
//类::静态对象
//类::实例方法
//类::new
list.stream().forEach(System.out::println);
System.out.println("------------------");
//多线程创建的流
list.parallelStream().forEach(System.out::println);
System.out.println("------------------");
//通过Array数组创建的流:
Integer[] arr={1,2,35,5};
Arrays.stream(arr).forEach(System.out::println);
System.out.println("---------------------");
//通过stream的of创建的流
Stream.of("zs","lisi").forEach(System.out::println);
System.out.println("-------------------");
//通过stream的iterate创建的流
//该流用于计算
//iterate的参数1:表示的是初始的值,参数2表示的是计算 Limit表示的是限制
Stream.iterate(3,x->x+1).limit(3).forEach(System.out::println);
System.out.println("----------------");
Stream.generate(()->new Random().nextInt(5)).limit(3).forEach(System.out::println);
}
## stream流的常见实现方式 ##
```java
List<person> list=new ArrayList<person>();
list.add(new Person("sunwukong", 500));
list.add(new Person("tangsen", 20));
list.add(new Person("bajie", 600));
list.add(new Person("shaseng", 660));
list.add(new Person("bajie", 600));
//1.过滤流
//名字的长度大于5
list.stream().filter(person -> person.getName().length()>5).forEach(System.out::println);
System.out.println("--------------------");
//2.跳过前两个的结果
list.stream().skip(2).forEach(System.out::println);
System.out.println("-------------");
//3.去重
list.stream().distinct().forEach(System.out::println);
System.out.println("----------------");
//4.sorted排序
list.stream().sorted((o1,o2)->o1.getAge().compareTo(o2.getAge())).forEach(System.out::println);
System.out.println("--------------");
//5.map映射---转为map集合
list.stream().map(t->t.getName()).forEach(System.out::println);
System.out.println("------------------");
//6.多线程查看
list.stream().parallel().forEach(System.out::println);
System.out.println("---------------------");
//7.min求最小值
System.out.println(list.stream().min((o1,o2)->o1.getAge()-(o2.getAge())));
System.out.println("----------------------");
//8.max的最大值
System.out.println(list.stream().max((o1,o2)->o1.getAge()-o2.getAge()));
System.out.println("---------------------");
//9.求总个数`
System.out.println(list.stream().count());
System.out.println("---------------------");
//10.reduce计算
Optional<integer> reduce = list.stream().map(t -> t.getAge()).reduce((o1, o2) -> o1 +o2 );
System.out.println("计算的结果是:"+reduce);
//11.用collect将stream转为集合的方法
Set<string> collect = list.stream().map(t -> t.getName()).collect(Collectors.toSet());
System.out.println("转为的结果是:"+collect);
}
``
------------
------------
总结:
创建:
- 通过Collection对象的stream()或parallelStream()多线程的方法
- 通过Arrays类的stream()方法。
- 通过stream接口的of()、iterate()、generate()方法
中间操作:
- filter、limit、skip、distinct、sorted、map、parallel
终止操作:
- forEach、min、max、count
- reduce、collect
/*
一、方法引用:若 Lambda 体中的功能,已经有方法提供了实现,可以使用方法引用
(可以将方法引用理解为 Lambda 表达式的另外一种表现形式)
对象的引用 :: 实例方法名
类名 :: 静态方法名
类名 :: 实例方法名
注意:
①方法引用所引用的方法的参数列表与返回值类型,需要与函数式接口中抽象方法的参数列表和返回值类型保持一致!
二、构造器引用 :构造器的参数列表,需要与函数式接口中参数列表保持一致!
类名 :: new
三、数组引用
类型[] :: new;
*/
public class TestMethodRef {
//数组引用
@Test
public void test8(){
Function<Integer, String[]> fun = (args) -> new String[args];
String[] strs = fun.apply(10);
System.out.println(strs.length);
System.out.println("--------------------------");
Function<Integer, Employee[]> fun2 = Employee[] :: new;
Employee[] emps = fun2.apply(20);
System.out.println(emps.length);
}
//构造器引用
@Test
public void test7(){
Function<String, Employee> fun = Employee::new;
BiFunction<String, Integer, Employee> fun2 = Employee::new;
}
@Test
public void test6(){
Supplier<Employee> sup = () -> new Employee();
System.out.println(sup.get());
System.out.println("------------------------------------");
Supplier<Employee> sup2 = Employee::new;
System.out.println(sup2.get());
}
//类名 :: 实例方法名
/**
*②若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格
*式: ClassName::MethodName
*/
@Test
public void test5(){
BiPredicate<String, String> bp = (x, y) -> x.equals(y);
System.out.println(bp.test("abcde", "abcde"));
System.out.println("-----------------------------------------");
BiPredicate<String, String> bp2 = String::equals;
System.out.println(bp2.test("abc", "abc"));
System.out.println("-----------------------------------------");
Function<Employee, String> fun = (e) -> e.show();
System.out.println(fun.apply(new Employee()));
System.out.println("-----------------------------------------");
Function<Employee, String> fun2 = Employee::show;
System.out.println(fun2.apply(new Employee()));
}
//类名 :: 静态方法名
@Test
public void test4(){
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
System.out.println("-------------------------------------");
Comparator<Integer> com2 = Integer::compare;
}
@Test
public void test3(){
BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y);
System.out.println(fun.apply(1.5, 22.2));
System.out.println("--------------------------------------------------");
BiFunction<Double, Double, Double> fun2 = Math::max;
System.out.println(fun2.apply(1.2, 1.5));
}
//对象的引用 :: 实例方法名
@Test
public void test2(){
Employee emp = new Employee(101, "张三", 18, 9999.99);
Supplier<String> sup = () -> emp.getName();
System.out.println(sup.get());
System.out.println("----------------------------------");
Supplier<String> sup2 = emp::getName;
System.out.println(sup2.get());
}
@Test
public void test1(){
PrintStream ps = System.out;
Consumer<String> con = (str) -> ps.println(str);
con.accept("Hello World!");
System.out.println("--------------------------------");
Consumer<String> con2 = ps::println;
con2.accept("Hello Java8!");
Consumer<String> con3 = System.out::println;
}
}
7