Steam流

操作数组或集合的一个知识点
public static void main(String[] args) {
        //认识steam流
        List<String> list=new ArrayList<>();
        Collections.addAll(list,"张三1","张四","王五","赵五");

//找出姓张 并且名字是三个字的存在一个新集合中
        List<String> newList =new ArrayList<>();
        for(String s : list){
            if(s.startsWith("张") && s.length()==3){
                newList.add(s);
            }


        }
        System.out.println(newList);
        System.out.println("--------------------------");

    //使用steam流用来实现对象集合数据的操作
        List<String> listS = list.stream().filter(s -> s.startsWith("张") && s.length() == 3).collect(Collectors.toList());
        System.out.println(listS);






     //如何获取List集合的Stream流
        List<String> list1=new ArrayList<>();
        Collections.addAll(list1,"张三1","张四","王五","赵五");
        System.out.println(list1.stream());


        //如何获取Set集合的stream流
        Set<String> strings = new HashSet<>();
        System.out.println(strings.stream());

        //如何获取Map集合的Stream流
        HashMap<String, String> map = new HashMap<>();
        System.out.println(map.keySet().stream());

        System.out.println(map.values().stream());

        System.out.println(map.entrySet().stream());


        //如何获取属猪的stream流

        String[] arr={"java1","java2","java3","java4"};
        System.out.println(Arrays.stream(arr));
        System.out.println(Stream.of(arr));





    }
最后修改:2023 年 05 月 13 日
如果觉得我的文章对你有用,请随意赞赏