一 创建Stream
package stream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.Test;
import com.dhy.boot.Employee;
/**
*
* @author Administrator
* stream 的三个操作步奏
* 1.创建stream
*
* 2.中间操作
*
* 3.中止操作
*/
public class StreamTest {
// 创建stream
@Test
public void test1() {
// 1 通过collection接口提供得stream
List<String> list = new ArrayList<>();
Stream<String> stream = list.stream();
//2 通过Arrays中得静态方法获取
Employee[] emps = new Employee[10];
Stream<Employee> stream2 = Arrays.stream(emps);
//3 通过stream类中得静态方法
Stream<String> stream3 = Stream.of("aa","bb");
//4创建无限流 迭代
Stream.iterate(0, x->x+2).limit(10).forEach(System.out::println);
Stream.generate(()->Math.random()).forEach(System.out::println);
}
}
二 筛选和切片
/**
* 筛选与切片
* filter->接受lambda,从流中排除元素
* limit->截断流,使元素不超过数量
* skip(n)->跳过元素,返回一个扔掉前n个元素得流,若流中元素不足n个,返回空流,与limit(n)互补
* distinct->筛选,通过流生成元素得hashCode和equals去重
*/
List<Employee> employees = Arrays.asList(
new Employee("dhy", 18d, 9999d),
new Employee("pf", 58d, 5555d),
new Employee("lt", 26d, 3333d),
new Employee("czk", 36d, 6666d),
new Employee("tm", 12d, 8888d)
);
//内部迭代:有StreamApi完成
@Test
public void test2() {
//中间操作,不会执行任何操作
Stream<Employee> filter = employees.stream().filter(x->{
System.out.println("Stream得中间操作");
return x.getSalary()>35d;
});
//中止操作:一次执行全部内容 惰性求值
filter.forEach(System.out::println);
}
//外部迭代
@Test
public void test3() {
Iterator<Employee> it = employees.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
//limit
@Test
public void test4() {
Stream<Employee> filter = employees.stream().filter(x->x.getSalary()>35d).limit(1);
filter.forEach(System.out::println);
}
//skip
@Test
public void test5() {
Stream<Employee> filter = employees.stream().filter(x->x.getSalary()>3526).skip(1);
filter.forEach(System.out::println);
}
三 映射
// 映射
// map接收一个lambda,将元素转化为其他形式,或者提取信息
// flatMap-接收一个函数作为参数,将流中得值都换为另一个流
@Test
public void test6() {
List<String> list = Arrays.asList("aa", "bb", "ee", "cc");
list.stream().map(x -> x.toUpperCase()).forEach(System.out::println);
System.out.println("````````````````````");
employees.stream().map(Employee::getName).forEach(System.out::println);
System.out.println("````````````````````");
// Stream<Stream<Character>> stream = list.stream().map(StreamTest::filterCharacter);
// stream.forEach((sm->{
// sm.forEach(System.out::println);
// }));
System.out.println("``````可替换上面代码``````````");
list.stream().flatMap(StreamTest::filterCharacter).forEach(System.out::println);;
}
public static Stream<Character> filterCharacter(String str){
List<Character> list = new ArrayList<>();
for (Character character : str.toCharArray()) {
list.add(character);
}
return list.stream();
}
四 排序
@Test
public void test7() {
List<String> list = Arrays.asList("aa", "bb", "ee", "cc");
list.stream().sorted().forEach(System.out::println);
System.out.println("-----------------");
list.stream().sorted((x,y)->y.compareTo(x)).forEach(System.out::println);;
}
五 查找匹配
六 归约和收集
七 并行流