Skip to content

Commit

Permalink
Simplified multi-root select dedupe
Browse files Browse the repository at this point in the history
Very previously required IdentityHashMap as Element.equals was a value test, but is now actually a reference test.
  • Loading branch information
jhy committed Dec 16, 2024
1 parent d6ed470 commit 0a2a2c4
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/main/java/org/jsoup/select/Selector.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.jspecify.annotations.Nullable;

import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.HashSet;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -145,7 +145,8 @@ public static Stream<Element> selectStream(Evaluator evaluator, Element root) {
}

/**
Find elements matching the query.
Find elements matching the query, across multiple roots. Elements will be deduplicated (in the case of
overlapping hierarchies).
@param query CSS selector
@param roots root elements to descend into
Expand All @@ -156,17 +157,14 @@ public static Elements select(String query, Iterable<Element> roots) {
Validate.notNull(roots);
Evaluator evaluator = QueryParser.parse(query);
Elements elements = new Elements();
IdentityHashMap<Element, Boolean> seenElements = new IdentityHashMap<>();
// dedupe elements by identity, not equality
HashSet<Element> seenElements = new HashSet<>(); // dedupe elements by identity, as .equals is ==

for (Element root : roots) {
final Elements found = select(evaluator, root);
for (Element el : found) {
if (seenElements.put(el, Boolean.TRUE) == null) {
elements.add(el);
}
}
selectStream(evaluator, root)
.filter(seenElements::add)
.forEach(elements::add);
}

return elements;
}

Expand Down

0 comments on commit 0a2a2c4

Please sign in to comment.