软件测试技术交流群 : 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,丈夫:我是王五,妻子:我是王五的妻子
家庭ID:0002,丈夫:我是李四,妻子:我是李四的妻子
家庭ID:0003,丈夫:我是王五,妻子:我是王五的妻子
这种处理办法,可以将处理此类问题的循环次数由N*N,变为2*N