Lambda表达式Stream性能问题

本文探讨了在处理普通数据类型时,Lambda表达式配合Stream API与传统for循环的性能对比。实验结果显示,在复杂数据处理场景下,parallelStream与普通for循环的性能表现相近。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//1.Stream处理普通数据类型

 

 

  public static void main(String[] args) {
    Random random = new Random();
    //1测试基本类型的数据;整数
    List<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < 1000000; i++) {
      list.add(random.nextInt(Integer.MAX_VALUE));
    }
//一.简单数据类型的stream操作
//1.stream
    testStream(list);
//2.parallelStream并行
    testparallelStream(list);
//3.普通for
    testfor(list);
//4.增强for
    testStrongfor(list);
//5.迭代器
    testIterator(list);

  }

  //流操作
  public static void testStream(List<Integer> list) {
    long start = System.currentTimeMillis();
    //获取最大值
    Optional<Integer> optional = list.stream().max(Integer::compare);
    System.out.println(optional.get());
    long end = System.currentTimeMillis();
    System.out.println("stream消耗" + (end - start) + "ms");
  }

  //并行stream
  public static void testparallelStream(List<Integer> list) {
    long start = System.currentTimeMillis();
    //获取最大值
    Optional<Integer> optional = list.parallelStream().max(Integer::compare);
    System.out.println(optional.get());
    long end = System.currentTimeMillis();
    System.out.println("并行stream消耗" + (end - start) + "ms");
  }


  //普通for循环
  public static void testfor(List<Integer> list) {
    long start = System.currentTimeMillis();
    //获取最大值
    int max = Integer.MAX_VALUE;
    for (int i = 0; i < list.size(); i++) {
      if (list.get(i) > max) {
        max = list.get(i);
      }
    }
    System.out.println(max);
    long end = System.currentTimeMillis();
    System.out.println("普通for循环消耗" + (end - start) + "ms");
  }

  //增强for循环foreach
  public static void testStrongfor(List<Integer> list) {
    long start = System.currentTimeMillis();
    //获取最大值
    int max = Integer.MAX_VALUE;
    for (Integer integer : list) {
      if (integer > max) {
        max = integer;
      }
    }
    System.out.println(max);
    long end = System.currentTimeMillis();
    System.out.println("增强for循环foreach" + (end - start) + "ms");
  }

  //迭代器
  public static void testIterator(List<Integer> list) {
    long start = System.currentTimeMillis();
    //获取最大值
    Iterator<Integer> it = list.iterator();
    int max = it.next();
    while (it.hasNext()) {
      int current = it.next();
      if (current > max) {
        max = current;
      }
    }
    System.out.println(max);

    long end = System.currentTimeMillis();
    System.out.println("迭代器消耗" + (end - start) + "ms");
  }

 

 

//比较复杂的数据Steam中  pararallelStream和普通for循环性能基本接近

 public static void main(String[] args) {
    //生成复杂数据列表
    Random random = new Random();
    //2.复杂类型数据类型,对象
    List<Product> list = new ArrayList<Product>();
    for (int i = 0; i < 1000000; i++) {
      list.add(new Product("pro" + i, i, random.nextInt(Integer.MAX_VALUE)));
    }
    //1.stream
    testStream(list);
    //2.并行stream
    testParallelStream(list);
    //3.普通For循环
    testfor(list);
    //4.增强for循环
    testStrongerfor(list);
    //5.iterator
    testIterator(list);


  }

  //stream
  public static void testStream(List<Product> list) {
    long start = System.currentTimeMillis();
    //获取最大值
    Optional<Product> optional = list.stream().max((x, y) -> x.getHot() - y.getHot());
    System.out.println(optional.get());
    long end = System.currentTimeMillis();
    System.out.println("stream" + (end - start) + "ms");
  }


  //并行stream
  public static void testParallelStream(List<Product> list) {
    long start = System.currentTimeMillis();
    //获取最大值
    Optional<Product> optional = list.parallelStream().max((x, y) -> x.getHot() - y.getHot());
    System.out.println(optional.get());
    long end = System.currentTimeMillis();
    System.out.println("并行stream" + (end - start) + "ms");
  }


  //普通for循环
  public static void testfor(List<Product> list) {
    long start = System.currentTimeMillis();
    //获取最大值
    Product maxHot = list.get(0);
    for (int i = 0; i < list.size(); i++) {
      Product current = list.get(i);
      if (current.getHot() > maxHot.getHot()) {
        maxHot = current;
      }
      System.out.println(maxHot);
    }

    long end = System.currentTimeMillis();
    System.out.println("普通for循环消耗" + (end - start) + "ms");
  }


  //增强for循环
  public static void testStrongerfor(List<Product> list) {
    long start = System.currentTimeMillis();
    //获取最大值
    Product maxHot = list.get(0);
    for (Product product : list) {
      if (maxHot.getHot() < product.getHot()) {
        maxHot = product;
      }
    }
    System.out.println(maxHot);
    long end = System.currentTimeMillis();
    System.out.println("增强for循环消耗" + (end - start) + "ms");
  }


  //iterator
  public static void testIterator(List<Product> list) {
    long start = System.currentTimeMillis();
    //获取最大值
    Iterator<Product> it = list.iterator();
    Product maxHot = it.next();
    while (it.hasNext()) {
      Product current = it.next();
      if (current.getHot() > maxHot.getHot()) {
        maxHot = current;
      }
    }
    System.out.println(maxHot);
    long end = System.currentTimeMillis();
    System.out.println("iterator消耗" + (end - start) + "ms");
  }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值