Stream流

  • allMatch()
/**
 * allMatch()流中的元素必须全部符合筛选条件,才能返回true;
 * 否则,返回false。
 */
public class AllMatchTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        boolean res = list.stream().allMatch(e -> e > 0);
        System.out.println(res);
    }
}
  • anyMatch()
/**
 * anyMatch()判断流中是否存在满足条件的元素,任意一个匹配就行
 */
public class AnyMatchTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        boolean res = list.stream().anyMatch(e -> e > 3);
        System.out.println(res);
    }
}
  • count()
/**
 * count()计算流中的元素个数
 */
public class CountTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        Long ans = list.stream().count();
        System.out.println(ans);
    }
}
  • distinct()
/**
 * distinct()去重
 */
public class DistinctTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,2,3,3,4,5);
        list.stream().distinct().forEach(System.out::println);
    }
}
  • filter()
/**
 * filter()过滤流
 */
public class FilterTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        list.stream().filter(i -> i > 3).forEach(System.out::println);
    }
}
  • forEach()

/**
 * forEach()增强for循环
 */
public class ForEachTest {
    public static void main(String[] args) {
        Random random = new Random();
        random.ints().limit(10).forEach(System.out::println);
    }
}
  • limit()
/**
 * limit()限制输出的元素数量
 */
public class LimitTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        List<Integer> res = list.stream().limit(3).collect(Collectors.toList());
        res.stream().forEach(System.out::println);
    }
}
  • map()
/**
 * map()元素映射
 */
public class MapTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        list.stream().map(i -> {return i * i;}).filter(i -> i > 3).limit(3).forEach(System.out::println);
    }
}
  • max()
/**
 * max()查找流中的最大值
 */
public class MaxTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        Integer ans = list.stream().max((x,y) -> x - y).get();
        System.out.println(ans);
    }
}
  • min()
/**
 * min()找出流中的最小值
 */
public class MinTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        Integer ans = list.stream().min((x,y) -> x - y).get();
        System.out.println(ans);
    }
}
  • of()
/**
 * of()将输入类型转成数据流
 */
public class OfTest {
    public static void main(String[] args) {
        Stream.of(1,2,3,4,5).limit(10).forEach(System.out::println);
    }
}
  • peek()
/**
 * peek()一般用于debug,对于基本类型,不会改变最终的结果,对于引用类型,会对最终结果造成影响。
 * peek()是打印中间过程的
 */
public class PeekTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        list.stream().peek(num -> System.out.println(num * num)).forEach(System.out::println);

        List<DTO> temp = Arrays.asList(new DTO(),new DTO(),new DTO());
        temp.stream().peek(num -> num.setName("yy")).forEach(System.out::println);

    }
    public static class DTO{
        String name = null;
        Integer code = 1;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        @Override
        public String toString() {
            return "DTO{" +
                    "name='" + name + '\'' +
                    ", code=" + code +
                    '}';
        }
    }
}
  • reduce()
/**
 * reduce()两两合并
 */
public class ReduceTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5,6);
        String res = list.stream().reduce("0",(x , y) -> x + "-" + y,(s1,s2) -> s1 + "-" + s2);
        System.out.println(res);
    }
}
  • skip()
/**
 * skip()跳过元素
 */
public class SkipTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        list.stream().skip(2).forEach(System.out::println);
    }
}
  • sorted()
/**
 * sorted()排序
 */
public class SortedTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        list.stream().sorted((x,y) -> y - x).forEach(System.out::println);
        list.stream().forEach(System.out::println);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值