首页 > 其他分享 >两种获取Stream流的方式

两种获取Stream流的方式

时间:2022-09-26 13:23:02浏览次数:49  
标签:map 两种 Stream stream Collection 获取 new

所有的Collection集合都可以通过stream默认方法获取流

Stream接口的静态方法of可以获取数组对应的流

public static void main(String[] args) {
        //把集合转换为Stream流
        ArrayList<String> list = new ArrayList<>();
        Stream<String> stream1 = list.stream();

        HashSet<String> set = new HashSet<>();
        Stream<String> stream2 = set.stream();

        HashMap<String, String> map = new HashMap<>();
        //获取键,存储到一个Set集合中
        Set<String> keySet = map.keySet();
        Stream<String> stream3 = keySet.stream();

        //获取值,存储到一个Collection集合中
        Collection<String> values = map.values();
        Stream<String> stream4 = values.stream();

        //获取键值对(键与值的映射关系 entrySet)
        Set<Map.Entry<String, String>> entries = map.entrySet();
        Stream<Map.Entry<String, String>> stream5 = entries.stream();

        //把数组转换为Stream流
        Stream<Integer> stream6 = Stream.of(1, 2, 3, 4, 5);
        //可变参数可以传递数组
        Integer[]arr={1,2,3,4,5};
        Stream<Integer> stream7 = Stream.of(arr);
        String[] arr2={"a","bb","ccc"};
        Stream<String> stream8 = Stream.of(arr2);
    }

 

标签:map,两种,Stream,stream,Collection,获取,new
From: https://www.cnblogs.com/xuche/p/16730540.html

相关文章