Skip to content

Commit 2147131

Browse files
committed
Refactor '.uniq' method: add iterator
1 parent f1a9eed commit 2147131

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

src/main/java/io/github/dgroup/enumerable4j/Enumerable.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
*/
2424
package io.github.dgroup.enumerable4j;
2525

26-
import java.util.ArrayList;
2726
import java.util.Collection;
2827
import java.util.HashSet;
29-
import java.util.List;
3028
import java.util.Set;
3129
import java.util.function.BinaryOperator;
3230
import java.util.function.Consumer;
@@ -372,15 +370,9 @@ default <Y> Enumerable<X> uniq(Function<? super X, ? extends Y> fnc) {
372370
out = new Empty<>();
373371
} else {
374372
final Set<Y> keys = new HashSet<>(0);
375-
final List<X> values = new ArrayList<>(0);
376-
for (final X val : this) {
377-
final Y key = fnc.apply(val);
378-
if (!keys.contains(key)) {
379-
keys.add(key);
380-
values.add(val);
381-
}
382-
}
383-
out = new Linked<>(values.iterator());
373+
out = new Linked<>(
374+
this.stream().filter(val -> keys.add(fnc.apply(val))).iterator()
375+
);
384376
}
385377
return out;
386378
}

0 commit comments

Comments
 (0)