有两个合集: 第一个包含 500k 个条目的列表包含
public class Entity {
protected Integer field1;
protected String field2;
protected String field3;
protected String field4;
protected Integer field5;
protected Integer field6;
protected Integer field7;
protected Integer field8;
protected Integer field9;
protected Integer field10;
protected Integer field11;
protected Integer field12;
protected Integer field13;
protected LocalDate field14;
}
第二个Set up to 10k entries,包含
public class Restriction {
private Integer field1;
private Integer field5;
private Integer field7;
private Integer field10;
}
问题:
需要在内存中连接两个集合,通过Restriction键,其中可能包含null字段,Entity不包含null字段。如果输出
,我希望收集实体我尝试使用哈希码进行快速搜索,但由于空字段的存在,该选项无法按预期工作。结果很慢,而只使用 equals 方法(例如,通过 List)
List<Entity> result = new ArrayList<>();
Collection<Entity> values = getRepository().read();
Set<Entity> restriction = filter.getRestrictions()
.stream()
.map(Entity::new)
.collect(Collectors.toSet());
for (Entity value : values) {
if (restriction.contains(value)) {
result.add(value);
}
}