java stream功能使用

/ java / 0 条评论 / 2283浏览

sorted

    #排序
    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("4");
    list.add("3");
    list.add("5");
    
    #默认是升序
    list.stream().sorted().forEach(System.out::println);

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

    #降序
    list.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);

filter

    #过滤

    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");

    #等于1的才输出
    list.stream().filter("1"::equals).forEach(System.out::println);

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

    list.stream().filter(s -> "1".equals(s)).forEach(System.out::println);

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

    list.stream().filter(new Predicate<String>() {
        @Override
        public boolean test(String s) {
            return "1".equals(s);
        }
    }).forEach(System.out::println);

map

    #获取其中的元素,并可以进行元素包含的操作
    Person p1 = new Person();
    p1.setName("aaa");
    p1.setSex("男");

    Person p2 = new Person();
    p2.setName("bbb");
    p2.setSex("男");

    List<Person> list = new ArrayList<>();
    list.add(p1);
    list.add(p2);

    list.stream().map(Person::getName).collect(Collectors.toList()).forEach(System.out::println);

    list.stream().map(person -> {
        return person.getSex();
    }).collect(Collectors.toList()).forEach(System.out::println);

    List<String> list1 = new ArrayList<>();
    list1.add("a1");
    list1.add("b2");
    list1.add("c3");
    list1.add("d4");
    list1.stream().map(String::toUpperCase).forEach(System.out::println);

mapToInt

    #转换为int
    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.stream().mapToInt(new ToIntFunction<String>() {
        @Override
        public int applyAsInt(String value) {
            return Integer.parseInt(value);
        }
    }).forEach(System.out::println);

    list.stream().mapToInt(Integer::parseInt).forEach(System.out::println);

mapToDouble

    #值转换为double
    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.stream().mapToDouble(new ToDoubleFunction<String>() {
        @Override
        public double applyAsDouble(String value) {
            return Double.valueOf(value);
        }
    }).forEach(System.out::println);

    list.stream().mapToDouble(Double::parseDouble).forEach(System.out::println);

mapToLong

    #值转换为long
    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.stream().mapToLong(new ToLongFunction<String>() {
        @Override
        public long applyAsLong(String value) {
            return Long.parseLong(value);
        }
    }).forEach(System.out::println);

    list.stream().mapToLong(Long::parseLong).forEach(System.out::println);

peek

    #中间操作,不会改变后续流的内容
    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("4");
    list.add("3");

    list.stream().peek(new Consumer<String>() {
        @Override
        public void accept(String s) {
            System.out.println(s);
            System.out.println();
        }
    }).sorted(Comparator.reverseOrder()).forEach(System.out::println);

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

    Stream.of("one", "two", "three", "four").filter(s -> s.length() > 3).peek(s -> System.out.println("filter value = " + s)).
            map(String::toUpperCase).peek(s -> System.out.println("map value = " + s)).collect(Collectors.toList());

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

    Stream.of("one", "two", "three", "four").filter(s -> s.length() > 3).peek(s -> s = s.toUpperCase()).
            collect(Collectors.toList()).forEach(System.out::println);

distinct

    #去重
    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("4");
    list.add("3");
    list.add("3");

    list.stream().distinct().forEach(System.out::println);

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

    list.stream().sequential().distinct().forEach(System.out::println);

limit

    #截取几位
    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("4");
    list.add("3");

    list.stream().limit(2).forEach(System.out::println);
    list.stream().sequential().limit(2).forEach(System.out::println);

skip

    #跳过前几位
    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("4");
    list.add("3");

    list.stream().skip(2).forEach(System.out::println);

    list.stream().sequential().skip(2).forEach(System.out::println);

    list.stream().unordered().skip(2).forEach(System.out::println);

reduce

    #运算
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(3);
    list.add(4);

    System.out.println(list.stream().reduce(1, new BinaryOperator<Integer>() {
        @Override
        public Integer apply(Integer integer, Integer integer2) {
            return (integer * integer2);
        }
    }).toString());

    Optional<Integer> data = list.stream().reduce((integer, integer2) -> integer * integer2);

    System.out.println(data.get());

    data = Optional.ofNullable(list.stream().reduce(0, (a, b) -> a - b));
    System.out.println("减法 0-1-2-3-4=:"+data.get());

    data = Optional.ofNullable(list.stream().reduce(1, (a, b) -> a * b));
    System.out.println("乘法 1*2*3*4=:"+data.get());

    data = Optional.ofNullable(list.stream().reduce(1, (a, b) -> a / b));
    System.out.println("除法 1/2/3/4=:"+data.get());

    data = Optional.ofNullable(list.stream().reduce(1, (a, b) -> a + b));
    System.out.println("加法 1+1+2+3+4:"+data.get());

    data = Optional.ofNullable(list.stream().reduce(1, (a, b) -> a % b));

    System.out.println("取余 1%1%2%3%4:"+data.get());

    data = Optional.ofNullable(list.stream().reduce(1, (a, b) -> Math.abs(b - a)));

    System.out.println("绝对值 |1-1-2-3-4|=:"+data.get());

collect

    #返回集合
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(4);
    list.add(3);
    list.add(3);

    Collection<Integer> collect = list.stream().collect(Collectors.toCollection(new Supplier<Collection<Integer>>() {
        @Override
        public Collection<Integer> get() {
            return new ArrayList<>();
        }
    }));

    collect.forEach(System.out::println);

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

    list.stream().collect(Collectors.toSet()).forEach(System.out::println);

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

    list.stream().collect(Collectors.toList()).forEach(System.out::println);

max

    #获取最大值
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(4);
    list.add(3);
    list.add(3);

    System.out.println(list.stream().max(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1.compareTo(o2);
        }
    }).get());

    System.out.println(list.stream().max((o1, o2) -> {
        return o1.compareTo(o2);
    }).get());

    System.out.println(list.stream().max(Integer::compareTo).get());

min

    #获取最小值
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(4);
    list.add(3);
    list.add(3);

    System.out.println(list.stream().min(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1.compareTo(o2);
        }
    }).get());

    System.out.println(list.stream().min((o1, o2) -> {
        return o1.compareTo(o2);
    }).get());

    System.out.println(list.stream().min(Integer::compareTo).get());

count

    #统计数量
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(4);
    list.add(3);
    list.add(3);

    System.out.println(list.stream().count());
    System.out.println(list.stream().filter(s -> s.compareTo(2) > 0).count());

    list.stream().mapToLong(e -> 1L).forEach(System.out::println);

anyMatch

    #是否匹配上至少一位
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(4);
    list.add(3);
    list.add(3);

    System.out.println(list.stream().anyMatch(new Predicate<Integer>() {
        @Override
        public boolean test(Integer integer) {
            return integer == 2;
        }
    }));

    System.out.println(list.stream().anyMatch(s -> s == 2));

allMatch

    #是否匹配所有
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(4);
    list.add(3);
    list.add(3);

    System.out.println(list.stream().allMatch(new Predicate<Integer>() {
        @Override
        public boolean test(Integer integer) {
            return integer == 3;
        }
    }));

    System.out.println(list.stream().allMatch(s -> s == 3));

noneMatch

    #是否一位都匹配不上
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(4);
    list.add(3);
    list.add(3);

    System.out.println(list.stream().noneMatch(new Predicate<Integer>() {
        @Override
        public boolean test(Integer integer) {
            return integer == 3;
        }
    }));

    list.stream().noneMatch(s -> s == 3);

findFirst

    #获取第一条
    List<Integer> list = new ArrayList<>();
    list.add(5);
    list.add(1);
    list.add(2);

    System.out.println(list.stream().findFirst().get());

findAny

    #随机获取
    List<Integer>list=newArrayList<>();
    list.add(5);
    list.add(1);
    list.add(2);

    #串行、数据量少的情况下,一般是第一个
    System.out.println(list.stream().findAny().get());

    #并行的情况下,一般是随机获取,重复获取不变
    System.out.println(IntStream.range(1,100).parallel().findAny().getAsInt());
评论已关闭.