java8的新特性之List集合双层for循环效率优化

白色玫瑰 程序猿

时间: 2023-07-11 阅读: 1 字数:4972

{}
首先先来一个案例简单模拟一下: List<String> wifes = new ArrayList<String>(); List<String> husbands= new ArrayList<String&...,...

目录

首先先来一个案例简单模拟一下:

List<String> wifes = new ArrayList<String>();
List<String> husbands= new ArrayList<String>();
for(int i=0;i<8000;i++){
     wifes.add(new Wife(i, i+"的妻子", "000"+i));
}
for(int i=0;i<8000;i++){
     husbands.add(new Husband(i, "我是"+i, "000"+i));
}

//现在需要让妻子和丈夫进行配对组成一个家庭,按照familyId进行配对
在1.8之前你会这么写:
for(int i=0;i<wifes.size();i++){
      System.out.println("i:"+i);
   for(int j=0;j<husbands.size();j++){
      System.out.println("j:"+j);
        if(妻子的familyid==丈夫的familyid){
            System.out.println("匹配成功:"+i+":"+j);
        }
   }
}
已8000*8000的两个集合进行配对。那就要循环8000*8000次。按20s循环一轮。需要20*8000秒

如果按照jdk1.8的lamada表达式进行匹配的话。这个测试结果是ms级别的

// 将list转为Map,这里key一定要为唯一值,map(familyId,wife对象)
Map<String, Wife> wifeMap = wife.stream().collect(
      Collectors.toMap(w -> w.getFamilyId(),
            w -> w));
// 匹配家庭
families = husband.stream().map(h -> {
   //从wifeMap里面根据husband的familyId去匹配。匹配上后组装成家庭对象然后放入集合。最后生成新的家庭集合
   return toFamily(wifeMap.get(h.getFamilyId()), h);
}).collect(Collectors.toList());

这是个测试案例:亲测如下:

package com.nuanshui.frms.report.manage;


import com.google.common.collect.Lists;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Author:
 * Date: 2016/11/23.
 * Time: 18:17
 */
public class TestFamily {
   @Test
   public void familyTest() {
      List<Wife> wife = Lists.newArrayList();
      List<Husband> husband = Lists.newArrayList();
      for(int i=0;i<8000;i++){
         wife.add(new Wife(i, i+"的妻子", "000"+i));
      }

      for(int i=0;i<8000;i++){
         husband.add(new Husband(i, "我是"+i, "000"+i));
      }
      Long startTime =System.currentTimeMillis();
      System.out.println("当前时间:"+startTime);
      List<Family> families = Lists.newArrayList();
      // 将list转为Map,这里key一定要为唯一值
      Map<String, Wife> wifeMap = wife.stream().collect(
            Collectors.toMap(w -> w.getFamilyId(),
                  w -> w));
      // 匹配家庭
      families = husband.stream().map(h -> {
         return toFamily(wifeMap.get(h.getFamilyId()), h);
      }).collect(Collectors.toList());
      /*for(int i=0;i<wife.size();i++){
         System.out.println("i:"+i);
         for(int j=0;j<husband.size();j++){
            System.out.println("j:"+j);
            if(wife.get(i).getFamilyId().equals(husband.get(j).getFamilyId())){
               System.out.println("匹配成功:"+i+":"+j);
            }
         }
      }*/
      System.out.println("循环完成");
      families.stream().forEach(family -> {
         System.out.println("家庭ID:" + family.getFamilyId() + ",丈夫:" + family.getHusbandName() + ",妻子:" + family.getWifeName());
      });
      Long endTime =System.currentTimeMillis();
      System.out.println("结束时间:"+endTime);
      System.out.println("------------耗时:---------"+(endTime-startTime)+"ms");
   }

   private Family toFamily(Wife wife, Husband husband) {
      Family family = new Family();
      family.setFamilyId(wife.getFamilyId());
      family.setHusbandName(husband.getHusbandName());
      family.setWifeName(wife.getWifeName());
      return family;
   }

   @Test
   public void test1(){
      //String.valueOf()
      BigDecimal c = new BigDecimal(651000);
      BigDecimal d = new BigDecimal(312);
      BigDecimal num3 = c.divide(d,10,BigDecimal.ROUND_HALF_DOWN);
      System.out.println(num3);
      BigDecimal b = (new BigDecimal(651000).divide(new BigDecimal(312)).setScale(1, BigDecimal.ROUND_HALF_UP));
      System.out.println(b);

   }
}

结果只要252ms;

这种效率天差地别。

原文地址:https://blog.csdn.net/liyingying111111/article/details/82464380?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168904454516800225524999%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168904454516800225524999&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-24-82464380-null-null.142^v88^control_2,239^v2^insert_chatgpt&utm_term=java%E4%BC%98%E5%8C%96

本文章网址:https://www.sjxi.cn/detil/09bc2efc30854e88b379cdd9da4cef88

打赏作者

本站为非盈利网站,如果您喜欢这篇文章,欢迎支持我们继续运营!

最新评论
当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号

×

(穷逼博主)在线接单

QQ: 1164453243

邮箱: abcdsjx@126.com

前端项目代做
前后端分离
Python 爬虫脚本
Java 后台开发
各种脚本编写
服务器搭建
个人博客搭建
Web 应用开发
Chrome 插件编写
Bug 修复