straem流 汇总,过滤,排序使用,输出

package com.stream;

import java.sql.Array;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Fetter
 * @ClassName StreamTest.java
 * @Description TODO
 * @createTime 2022年04月01日 10:00:00
 */
public class StreamTest {
    public static void main(String[] args) {
        // 创建集合并往里面添加元素
        //1.传统方式
        List<Integer> integerList = new ArrayList<>();
        integerList.add(1);
        integerList.add(2);
        Integer[] idArrays = {1, 2, 3, 4, 4, 5};
        String[] nameArray={"张三", "张四", "李四", "王二", "王二", "赵六"};
        List<String> names = Arrays.asList(nameArray);
        //2.使用arrays方式  数组转集合
        List<Integer> idList = Arrays.asList(idArrays);
        // 集合转数组
        Integer[] idArray = idList.toArray(new Integer[idList.size()]);

        System.out.println(idArray[0]);
        for (Integer integer : idArray) {
            System.err.println(integer);
        }
        //3.使用stream流方式
//        使用stream流过滤掉重复的数据
        List<Integer> stramList = idList.stream().distinct().collect(Collectors.toList());

        for (Integer integer : stramList) {
            System.out.println(integer);
        }
        List<Student> students = new ArrayList<>();
        Student student = new Student();
        student.setName("张三");
        student.setScore(70);
        Student student1 = new Student();
        student1.setName("张三");
        student1.setScore(70);
        Student student2 = new Student();
        student2.setName("李四");
        student2.setScore(10);
        Student student3 = new Student();
        student3.setName("王二");
        student3.setScore(50);
        students.add(student);
        students.add(student1);
        students.add(student2);
        students.add(student3);
        // 去掉重复的并排序
        students.forEach(index ->{
            System.out.println(index);
        });
        List<Student> sortedList = students.stream().distinct().sorted(Comparator.comparing(Student::getScore)).collect(Collectors.toList());
        for (Student student4 : sortedList) {
            System.err.println(student4 + "====");
        }

        // 取出某个数据并添加集合
        List<String>nameLIst=sortedList.stream().map(Student::getName).collect(Collectors.toList());
        nameLIst.forEach(a->{
            System.out.println(a);
        });
        // 将list输出为array  String []::new 为指定类型
        String[] newArr=nameLIst.stream().toArray(String[]::new);
        for (String s : newArr) {
            System.err.println(s+"=====>newArr");
        }
   
        // 过滤掉小于30分的人并重新排序
        List<Student>scores=
                sortedList.stream().filter(score->score.getScore()>30).sorted(Comparator.comparing(Student::getScore)).collect(Collectors.toList());
        scores.forEach(score->{
            System.out.println("score:"+score.getScore());
        });

        // 过滤掉不等于1的id集合并去掉重复的
        List<Integer> ids=idList.stream().filter(id ->!id.equals(1)).distinct().collect(Collectors.toList());
        ids.forEach(id->{
            System.err.println(id);
        });
        ids.forEach(id->{
            System.err.println(id);
        });
        List<String> nameList=names.stream().filter(name ->!name.equals("1")).distinct().collect(Collectors.toList());
        for (String s : nameList) {
            System.out.println(s+"name");
        }
        // 过滤掉重复的和名字包含四的
        List<String> newNames=names.stream().filter(name ->!name.contains("四")).distinct().collect(Collectors.toList());
        newNames.forEach(name->{
            System.err.println(name+"======>");
        });
        // 使用stream 流输出
        nameList.stream().forEach(System.out::println);
        // 总结,这里要使用eqauls进行比对,只能使用原集合的类型

        // 获取大于30分的分数总和
        // int 类型用mapToInt double用mapToDouble
        Integer scoreCount=sortedList.stream().filter(score -> score.getScore()>30).mapToInt(score->score.getScore()).sum();
        System.out.println(scoreCount);

        // 转换为map,但是字符串必须为k,v类型数据
        String msc="name:张三,mis:6666,age:20";
        //Stream<String> stream = Stream.of("name:Apple", "mics:Microsoft");
        Stream<String> stream = Stream.of(msc);
        Map<String, String> map = stream
                .collect(Collectors.toMap(
                        // 把元素s映射为key:
                        s -> s.substring(0, s.indexOf(':')),
                        // 把元素s映射为value:
                        s -> s.substring(s.indexOf(':') + 1)));
        System.out.println(map);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值