Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

segregate duplicates and non duplicates from a list of objects based on property on multiple property in java 8

I want to segregate duplicates and non duplicates from a list of objects based on property customerId and customerRegistration using java 8

segregate meaning

if I have 3 record with same customerId and customerRegistration, first record should add into nonduplicate list and 2 nd 3 record should be add in to duplicate list.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have below pojo class created hashcode and equals method based on customerId and customerRegistration.

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

}

I have written below logic in java7 which will work fine ,is there any way I can write using stream(convert forloop logic in the below code to stream)?

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
}

expecting output like below(from input assetList which is defined in the above execute1 function)

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

>Solution :

You can group the elements depending on adding to your set returns true or false:

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"));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading