编程语言
首页 > 编程语言> > Java 7 和 Java 8 对 list 的双层循环处理

Java 7 和 Java 8 对 list 的双层循环处理

作者:互联网

一、准备

Java 处理 List 的双层循环,一般都是当两个 List 某个值满足某条件时候,进行相应的处理。需求:两个 List 对象当 id 相同的时候就组建成美好的家庭,Husband 对象与 Wife 对象组建成 Family 对象。

Husband 对象如下:

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "丈夫")
public class Husband {
    @ApiModelProperty(value = "家庭ID")
    private int familyId;
    @ApiModelProperty(value = "丈夫名称")
    private String husbandName;
    @ApiModelProperty(value = "妻子名称")
    private String wifeName;
}

Wife 对象如下:

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "妻子")
public class Wife {
    @ApiModelProperty(value = "家庭ID")
    private int familyId;
    @ApiModelProperty(value = "丈夫名称")
    private String husbandName;
    @ApiModelProperty(value = "妻子名称")
    private String wifeName;
}

Family 对象如下:

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "家庭")
public class Family {
    @ApiModelProperty(value = "家庭ID")
    private int familyId;
    @ApiModelProperty(value = "丈夫名称")
    private String husbandName;
    @ApiModelProperty(value = "妻子名称")
    private String wifeName;
}

二、对比

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class FamilyDemo {
    public static void main(String[] args) {
        List<Wife> wives = new ArrayList<>();
        List<Husband> husbands = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            wives.add(new Wife(i, i + "的妻子", "000" + i));
        }
        for (int i = 0; i < 10000; i++) {
            husbands.add(new Husband(i, "我是" + i, "000" + i));
        }
        FamilyDemo familyDemo = new FamilyDemo();
        familyDemo.testJava7(wives, husbands);
        familyDemo.testJava8(wives, husbands);
    }

    public void testJava7(List<Wife> wives, List<Husband> husbands) {
        List<Family> families = new ArrayList<>();
        for (int i = 0; i < wives.size(); i++) {
            Wife wife = wives.get(i);
            for (int j = 0; j < husbands.size(); j++) {
                Husband husband = husbands.get(j);
                if (wife.getFamilyId() == husband.getFamilyId()) {
                    Family family = new Family();
                    family.setFamilyId(husband.getFamilyId());
                    family.setHusbandName(husband.getHusbandName());
                    family.setWifeName(husband.getWifeName());
                    families.add(family);
                }
            }
        }
        System.out.println("Java7结合家庭数量:" + families.size());
        System.out.println(families);
    }

    public void testJava8(List<Wife> wives, List<Husband> husbands) {
        List<Family> families = new ArrayList<>();
        //将list转为Map,这里key必须是唯一的,即familyId
        Map<Integer, Wife> wifeMap = wives.stream().collect(Collectors.toMap(w -> w.getFamilyId(), w -> w));
        families = husbands.stream().map(h -> {
            Family family = new Family();
            Wife wife = wifeMap.get(h.getFamilyId());
            family.setFamilyId(wife.getFamilyId());
            family.setHusbandName(wife.getHusbandName());
            family.setWifeName(wife.getWifeName());
            return family;
        }).collect(Collectors.toList());
        System.out.println("Java8完美结合家庭数量:" + families.size());
        families.stream().forEach(family -> {
            System.out.println("家庭ID:" + family.getFamilyId());
        });
    }
}

对比下来,stream 效率明显提升了很多,尤其数据量大的时候效率提升得越高。

标签:Java,family,List,list,value,ApiModelProperty,import,lombok,双层
来源: https://blog.csdn.net/ChineseSoftware/article/details/123073107