【Java】两个 List 遍历匹配数据的优化处理


软件测试技术交流群 : 429183023 

首先,举个例子:例如有一个List<Student>,另外一个是List<Husband>,你现在要筛选出来自同一个家庭的丈夫和妻子,组成一个家庭的数据全集(一个家庭包含丈夫、妻子),这样就是吧数据赋值给另外一个List<Family>(这个例子可能不恰当,欢迎评论给出更好的例子);

说明:代码采用java8的Stream和Lambda处理List,如果对 Stream和 Lambda不太了解的,先查阅另外一篇文章: http://blog.csdn.net/qi_lin7/article/details/52958123

好吧,大家还是结合代码来看吧

Wife.java
import java.io.Serializable;

/**
 * Author: 
 * Date: 2016/11/23.
 * Time: 18:08
 */
public class Wife implements Serializable {
    private int id;
    /**妻子名字**/
    private String wifeName;
    /**家庭ID**/
    private String familyId;

    public Wife() {

    }

    public Wife(int id, String wifeName, String familyId) {
        this.id = id;
        this.wifeName = wifeName;
        this.familyId = familyId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getWifeName() {
        return wifeName;
    }

    public void setWifeName(String wifeName) {
        this.wifeName = wifeName;
    }

    public String getFamilyId() {
        return familyId;
    }

    public void setFamilyId(String familyId) {
        this.familyId = familyId;
    }

    @Override
    public String toString() {
        return "";
    }
}

Husband.java
import java.io.Serializable;

/**
 * Author: 
 * Date: 2016/11/23.
 * Time: 18:08
 */
public class Husband implements Serializable {
    private int id;
    /**丈夫名字**/
    private String husbandName;
    /**家庭ID**/
    private String familyId;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Husband(){
        
    }

    public Husband(int id, String wifeName, String familyId) {
        this.id = id;
        this.husbandName = wifeName;
        this.familyId = familyId;
    }

    public String getHusbandName() {
        return husbandName;
    }

    public void setHusbandName(String husbandName) {
        this.husbandName = husbandName;
    }

    public String getFamilyId() {
        return familyId;
    }

    public void setFamilyId(String familyId) {
        this.familyId = familyId;
    }

    @Override
    public String toString() {
        return "";
    }
}

Family.java
import java.io.Serializable;

/**
 * Author: 
 * Date: 2016/11/23.
 * Time: 18:08
 */
public class Family implements Serializable {
    /**妻子名字**/
    private String wifeName;
    /**丈夫名字**/
    private String husbandName;
    /**家庭ID**/
    private String familyId;

    public String getWifeName() {
        return wifeName;
    }

    public void setWifeName(String wifeName) {
        this.wifeName = wifeName;
    }

    public String getHusbandName() {
        return husbandName;
    }

    public void setHusbandName(String husbandName) {
        this.husbandName = husbandName;
    }

    public String getFamilyId() {
        return familyId;
    }

    public void setFamilyId(String familyId) {
        this.familyId = familyId;
    }

    @Override
    public String toString() {
        return "";
    }
}

TestFamily.java
import com.xxx.bean.Family;
import com.xxx.bean.Husband;
import com.xxx.bean.Wife;
import com.google.common.collect.Lists;
import org.junit.Test;

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();
        wife.add(new Wife(0, "我是张三的妻子", "0001"));
        wife.add(new Wife(0, "我是李四的妻子", "0002"));
        wife.add(new Wife(0, "我是王五的妻子", "0003"));

        List<Husband> husband = Lists.newArrayList();
        husband.add(new Husband(0, "我是张三", "0001"));
        husband.add(new Husband(0, "我是李四", "0002"));
        husband.add(new Husband(0, "我是王五", "0003"));

        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());

        families.stream().forEach(family -> {
            System.out.println("家庭ID:" + family.getFamilyId() + ",丈夫:" + family.getHusbandName() + ",妻子:" + family.getWifeName());
        });
    }

    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;
    }
}


最后输出结果:

家庭ID:0001,丈夫:我是张三,妻子:我是张三的妻子
家庭ID:0002,丈夫:我是李四,妻子:我是李四的妻子
家庭ID:0003,丈夫:我是王五,妻子:我是王五的妻子

这种处理办法,可以将处理此类问题的循环次数由N*N,变为2*N

如果不理解或者有更好的解决办法处理此类问题的















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值