Skip to content

Commit

Permalink
small performance improvement EA-3763
Browse files Browse the repository at this point in the history
  • Loading branch information
gsergiu committed Mar 8, 2024
1 parent aa7dae6 commit d1de432
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -99,13 +101,14 @@ public List<EntityRecord> retrieveMultipleByEntityIdsOrCoreference(List<String>
List<EntityRecord> records=entityRecordRepository.findByEntityIdsOrCoreference(entityIds);
//sorting the list in order of the input ids, and exclude duplicates
List<EntityRecord> recordsSorted=new ArrayList<>();
//for improved performance use a hashset to verify if the record was added to the sorted list
Set<String> foundIds = new HashSet<>(entityIds.size());
for (String id : entityIds) {
Optional<EntityRecord> recordIdMatched= records.stream().filter(er -> id.equals(er.getEntityId()) || er.getEntity().getSameReferenceLinks().contains(id)).findFirst();
if(recordIdMatched.isPresent()) {
boolean isDuplicate=recordsSorted.stream().map(er -> er.getEntityId()).toList().contains(recordIdMatched.get().getEntityId());
if(! isDuplicate) {
//if the record was found and was not allready added to the sorted list
if(recordIdMatched.isPresent() && !foundIds.contains(recordIdMatched.get().getEntityId())) {
recordsSorted.add(recordIdMatched.get());
}
foundIds.add(recordIdMatched.get().getEntityId());
}
}
return recordsSorted;
Expand Down

0 comments on commit d1de432

Please sign in to comment.