目录
1.Stream关注的是对数据的运算,与CPU打交道,集合关注的是数据的存储,与内存打交道
2.Stream不会存储元素
不会改变源对象,相反,他们会返回一个持有结果的新Stream
操作是延迟执行的
3.执行流程:实例化-》中间操作-》终止操作
4.中间操作链,用于对数据源进行处理 ,一旦执行终止操作,就执行中间操作链,并产生结果。之后,不会再被使用
建立数据Person,便于后续操作。
- package StreamAPI;
-
-
- import java.util.Objects;
-
- public class Friends {
- private String name;
- private int age;
-
- public Friends() {
- }
-
- public Friends(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- /**
- * 获取
- * @return name
- */
- public String getName() {
- return name;
- }
-
- /**
- * 设置
- * @param name
- */
- public void setName(String name) {
- this.name = name;
- }
-
- /**
- * 获取
- * @return age
- */
- public int getAge() {
- return age;
- }
-
- /**
- * 设置
- * @param age
- */
- public void setAge(int age) {
- this.age = age;
- }
-
- public String toString() {
- return "Friends{name = " + name + ", age = " + age + "}";
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Friends friends = (Friends) o;
- return age == friends.age && Objects.equals(name, friends.name);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(name, age);
- }
- }
- package StreamAPI;
-
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class FriendsData {
- public static List
getlist(){ - List
list=new ArrayList<>(); - list.add(new Friends("李博",23));
- list.add(new Friends("蒲汶文",21));
- list.add(new Friends("邱志豪",24));
- return list;
- }
- }
- @Test
- public void test01(){
- List
list=FriendsData.getlist(); - Stream
stream=list.stream(); - //按条件查询
- stream.filter(e->e.getAge()>23).forEach(System.out::println);
- System.out.println("___________________________________");
- //截断流,使其不超过给定数量
- list.stream().limit(2).forEach(System.out::println);
- System.out.println("___________________________________");
- //跳过元素,返回一个除去前n个元素的流
- list.stream().skip(2).forEach(System.out::println);
- System.out.println("___________________________________");
- //筛选
- list.add(new Friends("邱志豪",35));
- list.add(new Friends("邱志豪",35));
- list.stream().distinct().forEach(System.out::println);
- }
- package StreamAPI;
-
- import org.junit.Test;
-
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.List;
- import java.util.stream.Stream;
- //中间操作
- //映射
- public class demo2 {
- //map()接收一个函数作为参数,将元素转换成其他形式或提取信息
- @Test
- public void test01(){
- List
list = Arrays.asList("aa","bb"); - System.out.println(list);
- Stream
stream = list.stream(); - stream.map(str->str.toUpperCase()).forEach(System.out::println);
- }
- //flatmap()接收一个函数作为参数,将流中的每个值都换成一个流,然后把所有流返回
- @Test
- public void test02(){
- List
list = Arrays.asList("aa","bb","cc"); - Stream
stream = list.stream(); - stream.flatMap(demo2::function).forEach(System.out::println);
- }
- public static Stream
function(String str){ - List
list=new ArrayList<>(); - for (Character c : str.toCharArray()) {
- list.add(c);
- }
- return list.stream();
- }
-
-
- }
- @Test
- public void test03(){
- //自然排序
- List
list=Arrays.asList(12,43,45,23,1); - Stream
stream = list.stream(); - stream.sorted().forEach(System.out::println);
- //定制排序
- List
getlist = FriendsData.getlist(); - Stream
stream1 = getlist.stream(); - stream1.sorted((o1,o2)-> o1.getAge()- o2.getAge()).forEach(System.out::println);
- }
aLLMatch(Predicate p)—检查是否匹配所有元素。
anyMatch( Predicate p)—检查是否至少匹配一个元素。noneMatch( Predicate p)—-检查是否没有匹配的元素。findFirst—-返回第一个元素
findAny—返回当前流中的任意元素count—返回流中元素的总个数
max ( Comparator c)—返回流中最大值
min(Comparator c)—返回流中最小值
forEach(Consumer c)—内部迭代
- @Test
- public void test(){
- List
getlist = FriendsData.getlist(); - Stream
stream = getlist.stream(); - boolean b = stream.allMatch(e -> e.getAge() > 10);
- System.out.println(b);
- }
- //reduce():可以将流中的元素反复结合起来,得到一个值。
- @Test
- public void test(){
- List
l1=Arrays.asList(1,2,3,4,5,6); - Stream
stream = l1.stream(); - Integer reduce = stream.reduce(0, Integer::sum);
- System.out.println(reduce);
-
- List
l2 = FriendsData.getlist(); - Stream
stream1 = l2.stream(); - Stream
integerStream = stream1.map(e -> e.getAge()); - Optional
reduce1 = integerStream.reduce((a, b) -> a + b); - System.out.println(reduce1);
-
- }
- //收集
- @Test
- public void test02(){
- List
l2 = FriendsData.getlist(); - Stream
stream1 = l2.stream(); - List
collect = stream1.collect(Collectors.toList()); - System.out.println(collect);
- }