栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Stream:对集合进行操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Stream:对集合进行操作

Collection有stream()方法
ArrayArrays.stream(...)
MapMap.entrySet().stream()
Stream中的方法
方法说明
Stream filter(Predicate predicate);数据过滤,保留满足条件的
Stream map(Function mapper);数据映射,T → R
Stream distinct();数据去重,与对象的equals()方法有关
Stream sorted();数据排序,对象需实现Comparable接口
Stream sorted(Comparator comparator);数据排序,Comparator
Stream peek(Consumer action); 数据流结束之后才会执行
Stream limit(long maxSize);数据过滤
Stream skip(long n);跳过n个数据
void forEach(Consumer action);数据流结束操作
A[] toArray(IntFunction generator);转变为A[]数组
 T reduce(T identity, BinaryOperator accumulator);
R collect(Collector collector);使用收集器收集数据
 Optional min(Comparator comparator);最小值
 Optional max(Comparator comparator);最大值
long count();计数
boolean anyMatch(Predicate predicate);
 boolean allMatch(Predicate predicate);
 boolean noneMatch(Predicate predicate);
 Optional findFirst();Optional ,Stream中的一个数据
Optional findAny();
 public static Stream of(T t);
public static Stream of(T... values);
数据排序Comparator数据收集Collectors
    public static void main(String[] args) {
        Map map = new HashMap<>();
        map.put("A",5);
        map.put("B",1);
        map.put("C",6);

        String collect = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .map(Map.Entry::getKey)
                .collect(Collectors.joining(","));

        System.out.println(collect);


    }
 public static void main(String[] args) {
        Map map = new HashMap<>();
        map.put("A",5);
        map.put("B",1);
        map.put("C",6);
        map.put("D",1);

        Map>> collect = map.entrySet().stream()
                .collect(Collectors.groupingBy(Map.Entry::getValue));


    }
    public static void main(String[] args) {
        Map map = new HashMap<>();
        map.put("A",5);
        map.put("B",1);
        map.put("C",6);
        map.put("D",1);

        Map collect = map.entrySet().stream()
                .collect(Collectors.groupingBy(Map.Entry::getValue, Collectors.counting()));

    }
  public static void main(String[] args) {
        Map map = new HashMap<>();
        map.put("A",5);
        map.put("B",1);
        map.put("C",6);
        map.put("D",1);

        Map> collect = map.entrySet().stream()
                .collect(Collectors.groupingBy(Map.Entry::getValue,
                        Collectors.mapping(Map.Entry::getKey, Collectors.toSet())));

    }
Optional

    public static void main(String[] args) {
        boolean present = Optional.ofNullable(null).isPresent();
        String o = Optional.ofNullable("B").filter("A"::equals).orElse("***");

        System.out.println(present);
        System.out.println(o);

    }

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/631490.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号