3

I have two Lists. One I created from Database and one from Csv file. Now I want to collect records in a list that contain both in database and csv file. I wrote code like below

BiPredicate<Trainee, Trainee> sameTrainee = (dbTrainee, csvTrainee) -> {

    String dbTraineeFirstName = dbTrainee.getFirstName();
    String dbTraineeLastName = dbTrainee.getLastName();
    String dbTraineeEmail = dbTrainee.getEmail();
    LocalDateTime dbTraineeCompletionDate = dbTrainee.getSessionDateTime();
    String text = dbTraineeCompletionDate.format(DATE_TIME_FORMATTER); 
    LocalDateTime dbTraineeSessionDateTime = LocalDateTime.parse(text);
    String dbTraineePhoneNumber = dbTrainee.getPhoneNumber();
    String dbTraineeSsn = dbTrainee.getSocialSecurityLastFour();
    String dbTraineeStreetOne = dbTrainee.getStreetOne();
    String dbTraineeCity = dbTrainee.getCity();

    String csvTraineeFirstName = csvTrainee.getFirstName();
    String csvTraineeLastName = csvTrainee.getLastName();
    String csvTraineeEmail = csvTrainee.getEmail();
    LocalDateTime csvTraineeSessionDateTime = csvTrainee.getSessionDateTime();
    String csvTraineePhoneNumber = csvTrainee.getPhoneNumber();
    String csvTraineeSsn = csvTrainee.getSocialSecurityLastFour();
    String csvTraineeStreetOne = csvTrainee.getStreetOne();
    String csvTraineeCity = csvTrainee.getCity();

    int dbTraineeSsnLength = dbTraineeSsn.length();
    int csvTraineeSsnLength = csvTraineeSsn.length();

    if (dbTraineeSsnLength != csvTraineeSsnLength) {
        if (dbTraineeSsnLength == 4 && dbTraineeSsn.startsWith("0")) {
            String dbTraineeSsnLast3Digits = dbTraineeSsn.substring(dbTraineeSsn.length() - 3);
            if (csvTraineeSsnLength == 3 && csvTraineeSsn.endsWith(dbTraineeSsnLast3Digits)) {
                csvTraineeSsn = "0" + csvTraineeSsn;
            }
        }
    }

    return dbTraineeFirstName.equals(csvTraineeFirstName) 
            && dbTraineeLastName.equals(csvTraineeLastName)
            && dbTraineeEmail.equals(csvTraineeEmail) 
            && dbTraineeSessionDateTime.equals(csvTraineeSessionDateTime)
            && dbTraineePhoneNumber.equals(csvTraineePhoneNumber)
            && dbTraineeSsn.equals(csvTraineeSsn) 
            && dbTraineeStreetOne.equals(csvTraineeStreetOne)
            && dbTraineeCity.equals(csvTraineeCity);
};

and called it like

List<Trainee> foundInBothList = dbMonthlyTraineeList.stream()
                    .filter(dbTrainee -> csvTraineeList.stream()
                        .anyMatch(csvTrainee -> {
                            return sameTrainee.test(dbTrainee, csvTrainee);
                        })
                    ).collect(Collectors.toList());

List<Trainee> notInFileList = dbMonthlyTraineeList.stream()
                    .filter(dbTrainee -> csvTraineeList.stream()
                        .noneMatch(csvTrainee -> {
                            return sameTrainee.test(dbTrainee, csvTrainee);
                        })
                    ).collect(Collectors.toList());

It works fine. But as my BiPredicate is getting long and untidy. So I made a class and collect all the predicates in a Collection like below

public class PlcbMonthlyReportStatisticsBiPredicates {

    public static BiPredicate<Trainee, Trainee> isValidFirstName() {
        return (dbTrainee, csvTrainee) -> {
            String dbTraineeFirstName = dbTrainee.getFirstName();
            String csvTraineeFirstName = csvTrainee.getFirstName();
            return dbTraineeFirstName.equals(csvTraineeFirstName);
        };
    }

    public static BiPredicate<Trainee, Trainee> isValidSsn() {
        return (dbTrainee, csvTrainee) -> {
            String dbTraineeSsn = dbTrainee.getSocialSecurityLastFour();
            String csvTraineeSsn = csvTrainee.getSocialSecurityLastFour();
            ...
            return dbTraineeSsn.equals(csvTraineeSsn);
        };
    }

    ....

    public static List<BiPredicate<Trainee, Trainee>> getAllBiPredicates() {

        List<BiPredicate<Trainee, Trainee>> allPredicates = Arrays.asList(
                isValidFirstName(),
                isValidSsn(),
                ... 
        );  
        return allPredicates;
    }
}

Now I Collect all the predicates but how can I apply these predicates in my anyMatch() and noneMatch(). I tried this but of-cources getting error

List<Trainee> foundInBothList1 = dbMonthlyTraineeList.stream()
    .filter(dbTrainee -> csvTraineeList.stream()
        .anyMatch(csvTrainee -> {
            List<BiPredicate<Trainee, Trainee>> allBiPredicates = getAllBiPredicates();
            return allBiPredicates.stream().reduce(BiPredicate::and).orElse((x,y)->true);  //error

        })
    ).collect(Collectors.toList());

How can I apply this. Is my approach is right?

**Edit


@Entity
public class Trainee {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

    private LocalDateTime sessionDateTime;
    private String firstName;
    ....

    @Override
    public boolean equals(Object otherObject) {

        // Are the same?
        if (this == otherObject) {
            return true;
        }
        // Is otherObject a null reference?
        if (otherObject == null) {
            return false;
        }
        // Do they belong to the same class?
        if (this.getClass() != otherObject.getClass()) {
            return false;
        }

        // Get the reference of otherObject in a otherTrainee variable
        Trainee otherTrainee = (Trainee)otherObject;

        LocalDateTime dbTraineeCompletionDate = this.getSessionDateTime();
        String text = dbTraineeCompletionDate.format(DATE_TIME_FORMATTER); 
        LocalDateTime dbTraineeSessionDateTime = LocalDateTime.parse(text);

        String dbTraineeSsn = this.socialSecurityLastFour;
        String csvTraineeSsn = otherTrainee.getSocialSecurityLastFour();

        int dbTraineeSsnLength = dbTraineeSsn.length();
        int csvTraineeSsnLength = csvTraineeSsn.length();

        if (dbTraineeSsnLength != csvTraineeSsnLength) {
            if (dbTraineeSsnLength == 4 && dbTraineeSsn.startsWith("0")) {
                String dbTraineeSsnLast3Digits = dbTraineeSsn.substring(dbTraineeSsn.length() - 3);
                if (csvTraineeSsnLength == 3 && csvTraineeSsn.endsWith(dbTraineeSsnLast3Digits)) {
                    csvTraineeSsn = "0" + csvTraineeSsn;
                }
            }
        }

        boolean isEqual = (this.firstName.equals(otherTrainee.firstName)
            && this.lastName.equals(otherTrainee.lastName)
            && this.email.equals(otherTrainee.email) 
            && dbTraineeSessionDateTime.equals(otherTrainee.sessionDateTime)
            && this.phoneNumber.equals(otherTrainee.phoneNumber)
            && dbTraineeSsn.equals(csvTraineeSsn) 
            && this.streetOne.equals(otherTrainee.streetOne)
            && this.city.equals(otherTrainee.city)
        );

        return isEqual;
    }

    @Override
    public int hashCode() {

        int hash = 37;
        int code = 0;

        code = (firstName == null ? 0 : firstName.hashCode());
        hash = hash * 59 + code;

        code = (lastName == null ? 0 : lastName.hashCode());
        hash = hash * 59 + code;

        code = (email == null ? 0 : email.hashCode());
        hash = hash * 59 + code;

        code = (sessionDateTime == null ? 0 : sessionDateTime.hashCode());
        hash = hash * 59 + code;

        code = (phoneNumber == null ? 0 : phoneNumber.hashCode());
        hash = hash * 59 + code;

        code = (socialSecurityLastFour == null ? 0 : socialSecurityLastFour.hashCode());
        hash = hash * 59 + code;

        code = (streetOne == null ? 0 : streetOne.hashCode());
        hash = hash * 59 + code;

        code = (city == null ? 0 : city.hashCode());
        hash = hash * 59 + code;

        return hash;

    }
}

Edit 2 (After overridng hascode() and equals()) -------------------------------------------------

Found in both:

List<Trainee> foundInBothList1 = dbMonthlyTraineeList.stream()
    .filter(dbTrainee -> csvTraineeList.stream()
        .anyMatch(csvTrainee -> {
            return allBiPredicates.stream().reduce(BiPredicate::and).orElse((x,y)->true).test(dbTrainee, csvTrainee);

        })
    ).collect(Collectors.toList());

    List<Trainee> foundInBothList = new ArrayList<>(dbMonthlyTraineeList);
    //foundInBothList.retainAll(new HashSet<>(csvTraineeList));
    foundInBothList.retainAll(csvTraineeList);

Found in database but not in CSV

List<Trainee> notInCsvFileList1 = dbMonthlyTraineeList.stream()
    .filter(dbTrainee -> csvTraineeList.stream()
        .noneMatch(csvTrainee -> {
            return allBiPredicates.stream().reduce(BiPredicate::and).orElse((x,y)->true).test(dbTrainee, csvTrainee);
        })
    ).collect(Collectors.toList());

//find out that elements of dbMonthlyTraineeList which is not present in arraylist(csvTraineeList).
List<Trainee> notInCsvFileList = new ArrayList<>(dbMonthlyTraineeList);
notInCsvFileList.removeAll(csvTraineeList);
Basit
  • 8,426
  • 46
  • 116
  • 196
  • Hhmm i though just override equals and hashcode() and just check list.contains() but I want to do it with streams :) – Basit Nov 02 '15 at 16:18

1 Answers1

2

It looks like you are over-thinking this. Why not just override equals with your sameTrainee bi-predicate code? (Don't forget to override hashCode also).

After you do this, you can keep the Trainees that are in both lists using:

Set<Trainee> foundInBothList = new HashSet<>(dbMonthlyTraineeList);
foundInBothList.retainAll(new HashSet<>(csvTraineeList));

This solution is O(n) and so it will perform a lot better than your solution, which is O(n²). This is because the contains operation is constant-time on a Set.


But if you really want your code to compile, you just need to call the test method where you have the error:

return allBiPredicates.stream().reduce(BiPredicate::and)
                               .orElse((x,y)->true)
                               .test(dbTrainee, csvTrainee);
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Thanks . actually i was confused with the hascode() methods. That after overriding equals() as you said with my same trainee bipredicate what is the efficient implementation of HashCode do I provide? That's why I didn't override hashcode ... – Basit Nov 02 '15 at 16:24
  • 1
    @Basit Take a look at [this answer](http://stackoverflow.com/a/113600/1743880) to override properly `hashCode`. – Tunaki Nov 02 '15 at 16:26
  • Hi thanks alot. can you check my edit whether I correctly override hashcode() and equals(). Thanks. – Basit Nov 02 '15 at 17:11
  • Thanks a lot for your guidance. I added `edit 2` in my post. One question when I use `foundInBothList.retainAll(new HashSet<>(csvTraineeList));` Then I get empty list but when i just use `foundInBothList.retainAll(csvTraineeList);` then I get correct list. You can see my edit 2, that's why I commented out that line. Why you use `HashSet<>()` and why it is giving empty list with `HashSet`. Thanks – Basit Nov 03 '15 at 09:13
  • I will of-course use the other one (without streams). Just for comparison that bot are returning same result I put both code (streams and without streams). – Basit Nov 03 '15 at 09:14
  • @Basit I used a Set because it provides a constant-time operation for the `contains` method, which is what `retainAll` calls. See my edit. – Tunaki Nov 03 '15 at 10:16
  • hhmm actually `retainAll(newHashSet<>(csvTraineeList))` is giving empty Set or List but just `retainAll(csvTraineeList)` is working fine in List as well as Set.... Don't know why... – Basit Nov 03 '15 at 10:47
  • Thanks a lot for your help :) – Basit Nov 03 '15 at 11:15