云迈博客

您现在的位置是:首页 > 灌水专栏 > 正文

灌水专栏

java steam常用方式

caoxu2022-05-05灌水专栏251
Map˂String,List˃collect=timeConfig.stream().map(this::getTimes)

Map<String, List> collect =
timeConfig.stream().map(this::getTimes)
.collect(Collectors.groupingBy(SysTimeConfig::getWeek));

    List<Object> collect1 = collect.entrySet().stream().map(i -> {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("week", WeekEnum.match(i.getKey()).getVal());
        map.put("time", i.getValue());
        return map;
    }).collect(Collectors.toList());

流的常用创建方法
1.1 使用Collection下的 stream() 和 parallelStream() 方法

List list = new ArrayList<>();
Stream stream = list.stream(); //获取一个顺序流
Stream parallelStream = list.parallelStream(); //获取一个并行流
// 创建List集合
List list = new ArrayList<>();
list.add(“张老三”);
list.add(“张小三”);
list.add(“李四”);
list.add(“赵五”);
list.add(“张六”);
list.add(“王八”);
Stream stream1 = list.stream();

 2.根据Set集合获取流
         // 创建List集合
    Set<String> set = new HashSet<>();
    list.add("张老三");
    list.add("张小三");
    list.add("李四");
    list.add("赵五");
    list.add("张六");
    list.add("王八");
    Stream<String> stream2 = set.stream();

     3.根据Map集合获取流
             // 创建Map集合
    Map<Integer,String> map = new HashMap<>();
    map.put(1,"张老三");
    map.put(2,"张小三");
    map.put(3,"李四");
    map.put(4,"赵五");
    map.put(5,"张六");
    map.put(6,"王八");

    // 3.1根据Map集合的键获取流
    Set<Integer> map1 = map.keySet();
    Stream<Integer> stream3 = map1.stream();
    // 3.2根据Map集合的值获取流
    Collection<String> map2 = map.values();
    Stream<String> stream4 = map2.stream();
    // 3.3根据Map集合的键值对对象获取瑞
    Set<Map.Entry<Integer, String>> map3 = map.entrySet();
    Stream<Map.Entry<Integer, String>> stream5 = map3.stream();

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~