根据 属性 在 java 中的多个 属性 上从对象列表中分离重复项和非重复项 8
segregate duplicates and non duplicates from a list of objects based on property on multiple property in java 8
我想根据 属性 customerId 和 customerRegistration 使用 java 8
从对象列表中分离重复项和非重复项
segregate meaning
如果我有 3 条记录具有相同的 customerId 和 customerRegistration,则第一个记录应添加到非重复列表中,第二条记录应添加到重复列表中。
我在下面的 pojo class 中创建了基于 customerId 和 customerRegistration 的 hashcode 和 equals 方法。
public class Asset {
private String customerName;
private int customerId;
private String Type;
private String customerRegistration;
private int assetTagList;
private String rso;
private String active;
private int billableUser;
private int billableAsset;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Asset asset = (Asset) o;
return customerId == asset.customerId && Objects.equals(customerRegistration, asset.customerRegistration);
}
@Override
public int hashCode() {
return Objects.hash(customerId, customerRegistration);
}
}
我在 java7 中写了下面的逻辑,它可以正常工作,有什么方法可以使用流来编写(将下面代码中的 forloop 逻辑转换为流)?
public Map<String ,List<Asset>> execute1() throws IOException {
List<Asset> assetList=new ArrayList<>();
Set<Asset> set=new HashSet<>();
List<Asset> duplicateList=new ArrayList<>();
List<Asset> nonDuplicateList=new ArrayList<>();
Map<String ,List<Asset>> map = new HashMap<>();
Asset asset1 = new Asset("French Customer1", 673, "Vehicle", "KNH 9009", 175, "FR", "Yes", 0, 0);
Asset asset2 = new Asset("French Customer2", 673, "Vehicle", "KNH 9009", 175, "FR", "Yes", 0, 0);
Asset asset3 = new Asset("French Customer3", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset4= new Asset("French Customer4", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset5= new Asset("French Customer4", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset6 = new Asset("French Customer5", 674, "Vehicle", "KNH 9008", 175, "FR", "Yes", 0, 0);
assetList.add(asset1);
assetList.add(asset2);
assetList.add(asset3);
assetList.add(asset4);
assetList.add(asset5);
assetList.add(asset6);
for (Asset assetTemp:assetList){
if(set.add(assetTemp)){
nonDuplicateList.add(assetTemp);
}
else {
duplicateList.add(assetTemp);
}
}
map.put("duplicate",duplicateList);
map.put("nonDuplicateList",nonDuplicateList);
return map
}
期望输出如下(来自上面 execute1 函数中定义的输入资产列表)
nonDuplicateList should have (asset1,asset3,asset6):
Asset asset1 = new Asset("French Customer1", 673, "Vehicle", "KNH 9009", 175, "FR", "Yes", 0, 0);
Asset asset3 = new Asset("French Customer3", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset6 = new Asset("French Customer5", 674, "Vehicle", "KNH 9008", 175, "FR", "Yes", 0, 0);
duplicateList should have:(asset2,asset4,asset5)
Asset asset2 = new Asset("French Customer2", 673, "Vehicle", "KNH 9009", 175, "FR", "Yes", 0, 0);
Asset asset4= new Asset("French Customer4", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset5= new Asset("French Customer4", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
您可以根据添加到集合中的元素对元素进行分组 returns 对或错:
public static Map<String ,List<Asset>> execute2() throws IOException {
List<Asset> assetList=new ArrayList<>();
Asset asset1 = new Asset("French Customer1", 673, "Vehicle", "KNH 9009", 175, "FR", "Yes", 0, 0);
Asset asset2 = new Asset("French Customer2", 673, "Vehicle", "KNH 9009", 175, "FR", "Yes", 0, 0);
Asset asset3 = new Asset("French Customer3", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset4= new Asset("French Customer4", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset5= new Asset("French Customer4", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset6 = new Asset("French Customer5", 674, "Vehicle", "KNH 9008", 175, "FR", "Yes", 0, 0);
assetList.add(asset1);
assetList.add(asset2);
assetList.add(asset3);
assetList.add(asset4);
assetList.add(asset5);
assetList.add(asset6);
Set<Asset> set=new HashSet<>();
return assetList.stream().collect(Collectors.groupingBy(x -> set.add(x) ? "nonDuplicateList" : "duplicate"));
}
您可以使用与 Set<Asset> set
相同的方法,并使用 partitioningBy
收集器将其收集到地图中。即使在给定的示例中它不会造成太大问题,请阅读 this 答案以了解 为什么您不应该使用收集器来改变现有集合。
assetList.stream().collect(Collectors.partitioningBy(set::add));
这将是 return 具有 Boolean
键的映射,其中 true
将是 non-duplicates。
我想根据 属性 customerId 和 customerRegistration 使用 java 8
从对象列表中分离重复项和非重复项segregate meaning
如果我有 3 条记录具有相同的 customerId 和 customerRegistration,则第一个记录应添加到非重复列表中,第二条记录应添加到重复列表中。
我在下面的 pojo class 中创建了基于 customerId 和 customerRegistration 的 hashcode 和 equals 方法。
public class Asset {
private String customerName;
private int customerId;
private String Type;
private String customerRegistration;
private int assetTagList;
private String rso;
private String active;
private int billableUser;
private int billableAsset;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Asset asset = (Asset) o;
return customerId == asset.customerId && Objects.equals(customerRegistration, asset.customerRegistration);
}
@Override
public int hashCode() {
return Objects.hash(customerId, customerRegistration);
}
}
我在 java7 中写了下面的逻辑,它可以正常工作,有什么方法可以使用流来编写(将下面代码中的 forloop 逻辑转换为流)?
public Map<String ,List<Asset>> execute1() throws IOException {
List<Asset> assetList=new ArrayList<>();
Set<Asset> set=new HashSet<>();
List<Asset> duplicateList=new ArrayList<>();
List<Asset> nonDuplicateList=new ArrayList<>();
Map<String ,List<Asset>> map = new HashMap<>();
Asset asset1 = new Asset("French Customer1", 673, "Vehicle", "KNH 9009", 175, "FR", "Yes", 0, 0);
Asset asset2 = new Asset("French Customer2", 673, "Vehicle", "KNH 9009", 175, "FR", "Yes", 0, 0);
Asset asset3 = new Asset("French Customer3", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset4= new Asset("French Customer4", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset5= new Asset("French Customer4", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset6 = new Asset("French Customer5", 674, "Vehicle", "KNH 9008", 175, "FR", "Yes", 0, 0);
assetList.add(asset1);
assetList.add(asset2);
assetList.add(asset3);
assetList.add(asset4);
assetList.add(asset5);
assetList.add(asset6);
for (Asset assetTemp:assetList){
if(set.add(assetTemp)){
nonDuplicateList.add(assetTemp);
}
else {
duplicateList.add(assetTemp);
}
}
map.put("duplicate",duplicateList);
map.put("nonDuplicateList",nonDuplicateList);
return map
}
期望输出如下(来自上面 execute1 函数中定义的输入资产列表)
nonDuplicateList should have (asset1,asset3,asset6):
Asset asset1 = new Asset("French Customer1", 673, "Vehicle", "KNH 9009", 175, "FR", "Yes", 0, 0);
Asset asset3 = new Asset("French Customer3", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset6 = new Asset("French Customer5", 674, "Vehicle", "KNH 9008", 175, "FR", "Yes", 0, 0);
duplicateList should have:(asset2,asset4,asset5)
Asset asset2 = new Asset("French Customer2", 673, "Vehicle", "KNH 9009", 175, "FR", "Yes", 0, 0);
Asset asset4= new Asset("French Customer4", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset5= new Asset("French Customer4", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
您可以根据添加到集合中的元素对元素进行分组 returns 对或错:
public static Map<String ,List<Asset>> execute2() throws IOException {
List<Asset> assetList=new ArrayList<>();
Asset asset1 = new Asset("French Customer1", 673, "Vehicle", "KNH 9009", 175, "FR", "Yes", 0, 0);
Asset asset2 = new Asset("French Customer2", 673, "Vehicle", "KNH 9009", 175, "FR", "Yes", 0, 0);
Asset asset3 = new Asset("French Customer3", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset4= new Asset("French Customer4", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset5= new Asset("French Customer4", 673, "Vehicle", null, 175, "FR", "Yes", 0, 0);
Asset asset6 = new Asset("French Customer5", 674, "Vehicle", "KNH 9008", 175, "FR", "Yes", 0, 0);
assetList.add(asset1);
assetList.add(asset2);
assetList.add(asset3);
assetList.add(asset4);
assetList.add(asset5);
assetList.add(asset6);
Set<Asset> set=new HashSet<>();
return assetList.stream().collect(Collectors.groupingBy(x -> set.add(x) ? "nonDuplicateList" : "duplicate"));
}
您可以使用与 Set<Asset> set
相同的方法,并使用 partitioningBy
收集器将其收集到地图中。即使在给定的示例中它不会造成太大问题,请阅读 this 答案以了解 为什么您不应该使用收集器来改变现有集合。
assetList.stream().collect(Collectors.partitioningBy(set::add));
这将是 return 具有 Boolean
键的映射,其中 true
将是 non-duplicates。