lambda表达式操作集合数据

 //多个数据
    Stream stream = Stream.of("文泽稳", "朱润", "马军");
    //数组
    String[] persons = new String[]{"xueqi", "biyao"};
    Stream<String> stream1 = Arrays.stream(persons);
    //集合
    List<String> list = new ArrayList<>();
    list.add("少林");
    list.add("武当");
    list.add("崆峒");
    list.add("华山");
    list.add("青城");
    list.add("峨眉");
    Stream stream2 = list.stream();
    //Map
    Map<String, Integer> map = new HashMap();
    map.put("wangqi", 21);
    map.put("wenzewen", 27);
    map.put("laowen", 59);
    Set entries = map.entrySet();
    Stream stream3 = map.entrySet().stream();

    //Stream对象  ----->转换得到指定的对象、
    //数组
    Object[] objects =  stream.toArray(String[]::new);

    //字符串
    String s = stream.collect(Collectors.joining()).toString();
    System.out.println(s);

    //列表
    List<String> asList = (List<String>) stream.collect(Collectors.toList());
    System.out.println(asList.size());
    System.out.println(asList);

    //set集合
    Set<String> sets = (Set<String>) stream.collect(Collectors.toSet());
    System.out.println(sets.size());
    System.out.println(sets);

    //map
    Map<String, String> hashMap = (Map<String, String>) stream
        .collect(Collectors.toMap(x -> x, y -> "value:" + y));
    System.out.println(hashMap);
  }

 

发布了71 篇原创文章 · 获赞 8 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览