java中两个列表的交集
我使用for和if循环通过比较列表的内部JSON来查找列表的交点。我正在寻找使用CollectionUtils或Java8或其他类似解决方案的解决方案java中两个列表的交集,java,collections,java-8,intersection,Java,Collections,Java 8,Intersection,我使用for和if循环通过比较列表的内部JSON来查找列表的交点。我正在寻找使用CollectionUtils或Java8或其他类似解决方案的解决方案 private List<IBXLocation> compareIbxLocationDetails(List<IBXLocation> serviceIbxsForLoggedInUser, List<IBXLocation> serviceIbxsForUser) { List&l
private List<IBXLocation> compareIbxLocationDetails(List<IBXLocation> serviceIbxsForLoggedInUser,
List<IBXLocation> serviceIbxsForUser) {
List<IBXLocation> finalList=new ArrayList();
for (IBXLocation ibxForLoggedInUser : serviceIbxsForLoggedInUser) {
String ibxSelected=ibxForLoggedInUser.getIbx();
boolean ibxFound = false;
ibxLoop:for (IBXLocation permittedIBXForUser : serviceIbxsForUser) {
if (ibxSelected.equals(permittedIBXForUser.getIbx())) {
IBXLocation newIbx = new IBXLocation(ibxSelected);
List<Cage> newCageList=new ArrayList();
if (!CollectionUtils.isEmpty(ibxForLoggedInUser.getCageDetails())) {
for (Cage selectedCage : ibxForLoggedInUser.getCageDetails()) {
String loggedInSelectedCageStr = selectedCage.getCage();
for (Cage permittedCage : permittedIBXForUser.getCageDetails()) {
if (loggedInSelectedCageStr.equals(permittedCage.getCage())) {
newCageList.add(permittedCage);
}
}
newIbx.setCageDetails(newCageList);
}
finalList.add(newIbx);
}
ibxFound = true;
break ibxLoop;
}
}
}
return finalList;
}
private List compareIbxLocationDetails(列表服务ibxsforloggedinuser,
列表服务IBXSFORUSER){
List finalList=new ArrayList();
for(IBXLocation ibxForLoggedInUser:serviceIbxsForLoggedInUser){
字符串ibxSelected=ibxForLoggedInUser.getIbx();
布尔值ibxFound=false;
ibxLoop:for(IBXLocation permittedIBXForUser:servicebxsforuser){
if(ibxSelected.equals(permittedIBXForUser.getIbx())){
IBXLocation newIbx=新IBXLocation(ibxSelected);
List newCageList=newarraylist();
如果(!CollectionUtils.isEmpty(ibxForLoggedInUser.getCageDetails())){
对于(Cage-selectedCage:ibxForLoggedInUser.getCageDetails()){
字符串loggedInSelectedCageStr=selectedCage.getCage();
对于(Cage PermittedPage:permittedIBXForUser.getCageDetails()){
if(loggedInSelectedCageStr.equals(permitedCage.getCage())){
添加(许可证年龄);
}
}
newIbx.setCageDetails(newCageList);
}
finalList.add(newIbx);
}
ibxFound=true;
打破ibxLoop;
}
}
}
回归终结者;
}
您可以使用此
Set ibxForLoggedInUserToSet= new HashSet<IBXLocation>(ibxForLoggedInUser);
for(IBXLocation per: serviceIbxsForUser){
if (ibxForLoggedInUserToSet.contains(per)){
finalList.add(per);
}
}
Set ibxForLoggedInUserToSet=newhashset(ibxForLoggedInUser);
for(IBXLocation per:serviceIbxsForUser){
if(ibxForLoggedInUserToSet.contains(per)){
最后添加(每个);
}
}
在org.apache.commons.collections4.ListUtils
中有一种方法公共静态列表交叉点(列表您可以利用设置
来获取交叉点:
public <T> intersect(List<T> firstList, List<T> secondList) {
Set<T> result = new HashSet<>();
result.addAll(firstList);
result.addAll(secondList);
return result;
}
public intersect(列表第一列表,列表第二列表){
Set result=new HashSet();
结果:addAll(firstList);
结果:addAll(第二个列表);
返回结果;
}
行newIbx.setCageDetails(newCageList);
可能是一个bug。它将不断覆盖值。您可能应该将其移到循环外的finalList.add(newIbx);
此代码按预期工作,我正在寻找使用collectionUtils或Java 8来处理相同代码的解决方案。