Skip to content

Commit

Permalink
IQSS#6000. Refactor completeVocabulary() to use Java 8 Stream API, w…
Browse files Browse the repository at this point in the history
…hich should be faster than O(n) linear search.

This is to test if the autoComplete list is loaded faster this way.
  • Loading branch information
poikilotherm committed Aug 1, 2019
1 parent 2a5a936 commit c80aeea
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/main/java/edu/harvard/iq/dataverse/DatasetField.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
Expand Down Expand Up @@ -208,13 +209,11 @@ public void setControlledVocabularyValues(List<ControlledVocabularyValue> contro
* @return List of matching vocabulary items (containing the query string in their l18n string)
*/
public Collection<ControlledVocabularyValue> completeVocabulary(String query) {
Collection<ControlledVocabularyValue> filtered = new ArrayList<>();
for (ControlledVocabularyValue item : this.getDatasetFieldType().getControlledVocabularyValues()) {
if (item.getLocaleStrValue().toLowerCase().contains(query.toLowerCase())) {
filtered.add(item);
}
}
return filtered;
String q = query.toLowerCase();
return this.getDatasetFieldType().getControlledVocabularyValues()
.stream()
.filter(cvv -> cvv.getLocaleStrValue().toLowerCase().contains(q))
.collect(Collectors.toList());
}

/**
Expand Down

0 comments on commit c80aeea

Please sign in to comment.