gpt4 book ai didi

java - 如何在 anyMatch() java 8 流中应用复合 BiPredicate

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:17:19 26 4
gpt4 key购买 nike

我有两个列表。一个是我从数据库创建的,一个是从 Csv 文件创建的。现在我想在包含数据库和 csv 文件的列表中收集记录。我写了如下代码

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

并称它为

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

它工作正常。但是随着我的 BiPredicate 变得又长又乱。所以我创建了一个类并将所有谓词收集在一个集合中,如下所示

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

现在我收集了所有谓词,但如何在我的 anyMatch() 和 noneMatch() 中应用这些谓词。我试过这个但是当然会出错

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

我该如何应用它。我的做法对吗?

**编辑


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

}
}

编辑 2(覆盖 hascode() 和 equals() 之后)----------------------------------------------

两者都有:

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

在数据库中找到但在 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);

最佳答案

看来你想多了。为什么不用您的sameTrainee 双谓词代码覆盖equals? (不要忘记也覆盖 hashCode)。

执行此操作后,您可以使用以下方法保留两个列表中的 Trainee:

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

此解决方案的复杂度为 O(n),因此它比您的解决方案的性能要好得多,即 O(n²)。这是因为 contains 操作在 Set 上是恒定时间的。


但是如果你真的想让你的代码通过编译,你只需要在你有错误的地方调用test方法:

return allBiPredicates.stream().reduce(BiPredicate::and)
.orElse((x,y)->true)
.test(dbTrainee, csvTrainee);

关于java - 如何在 anyMatch() java 8 流中应用复合 BiPredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33481807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com