1. 程式人生 > >java8的新特性之List集合雙層for迴圈效率優化

java8的新特性之List集合雙層for迴圈效率優化

首先先來一個案例簡單模擬一下:

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;

這種效率天差地別。