介绍

  1. stream 主要有两种操作
  2. 一种是转换成另外一种流
  3. 另外一种是进行聚合操作

代码

  • 辅助类
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Person {

String name;
Integer age;

}
  • 测试代码
package com.java8;



import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.junit.Test;
import org.w3c.dom.ls.LSOutput;

import java.util.*;
import java.util.function.Supplier;
import java.util.stream.*;

/**
* 流学习
*
* @author yantingrui
* @date 2021/8/23 9:58
*/
public class StreamLearn {

public static void main(String[] args) {

Stream<String> stream = Stream.of("A","B","C","D");
stream.forEach(System.out::println);

}


/**
* 流的创建
* @time 2021/7/19 11:30
*/
public void test(){
//
Stream<String> stream = Stream.of("A","B","C","D");
stream.forEach(System.out::println);

Stream<String> stream1 = Arrays.stream(new String[] { "A", "B", "C" });
stream1.forEach(System.out::println);

Person p1 = new Person("Tom",54);
Person p2 = new Person("Tom",54);
Person p3 = new Person("Tom",54);
ArrayList<Person> arr = new ArrayList<Person>();
arr.add(p1);
arr.add(p2);
arr.add(p3);


}

/**
* 无线序列 创建流
* @time 2021/7/19 11:30
*/
@Test
public void test2(){
Stream<Integer> natual = Stream.generate(new NatualSupplier());
// 注意:无限序列必须先变成有限序列再打印:
natual.limit(20).forEach(System.out::println);
}

class NatualSupplier implements Supplier<Integer> {
int n = 0;
@Override
public Integer get() {
n++;
return n;
}
}

/**
* map 类型
* @time 2021/7/19 11:30
*/
@Test
public void test3(){
Person p1 = new Person("Tom",54);
Person p2 = new Person("Tom",54);
Person p3 = new Person("Tom",54);
ArrayList<Person> arr = new ArrayList<Person>();
arr.add(p1);
arr.add(p2);
arr.add(p3);
arr.stream().map(Person::getName).forEach(var -> System.out.println(var));
}

/**
* filter 类型
* @time 2021/7/19 11:30
*/
@Test
public void test4(){
Person p1 = new Person("Tom1",14);
Person p2 = new Person("Tom2",54);
Person p3 = new Person("Tom3",34);
ArrayList<Person> arr = new ArrayList<Person>();
arr.add(p1);
arr.add(p2);
arr.add(p3);
arr.stream().filter(var -> var.getAge()>30).forEach(var -> System.out.println(var.toString()));
}


/**
* ${todo}
* @time 2021/7/19 11:30
*/
@Test
public void test5(){
int sum = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).reduce(0, (acc, n) -> acc + n);
System.out.println(sum); // 45
}

/**
* ${todo}
* @time 2021/7/19 11:30
*/
@Test
public void test6(){
int sum = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).reduce(0, (acc, n) -> acc + n);
System.out.println(sum); // 45
}

/**
* sorted
* @time 2021/7/19 11:30
*/
@Test
public void test7(){
IntStream ints = new Random(10).ints(1, 10);
ints.limit(10).sorted().forEach(System.out::println);
}


/**
* parallel
*
* @time 2021/7/19 11:30
*/
@Test
public void test8() {
IntStream ints = new Random(10).ints(4, 10);
// count max min Average distinct(去重)
IntStream distinct = ints.limit(5).parallel().distinct();
distinct.forEach(var -> System.out.println(var));

}


/**
* Collect 集合操作
* @time 2021/7/19 11:30
*/
@Test
public void test9() {
List<String> list = Arrays.asList("AA", "BB", "CC", "BB", "CC", "AA", "AA");
long l = list.stream().distinct().count();
System.out.println("No. of distinct elements:"+l);
String output = list.stream().distinct().collect(Collectors.joining(","));
System.out.println(output);

}
}

public static void main(String[] args) {
Person p1 = new Person("Tom",15);
Person p2 = new Person("Tom",15);
Person p3 = new Person("Tom",15);
ArrayList<Person> people = new ArrayList<>();
people.add(p1);
people.add(p2);
people.add(p3);
List<Person> collect = people.stream().map(v -> {
Person person = new Person();
person.setName(v.getName());
person.setAge(900);
return person;
}).collect(Collectors.toList());
System.out.println(JSON.toJSON(collect));
}
// 去重
public static void main(String[] args){
int List[] =[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]
List<Integer> listWithoutDuplicates = List.stream().distinct().collect(Collectors.toList());
System.out.println(listWithoutDuplicates);
}

参考文献

《Java8实战》读书笔记