Skip to content

Commit 94d6622

Browse files
authored
Improve perfomance for $.intersection(list1, list2) and $.difference(list1, list2) methods.
1 parent 1fa8569 commit 94d6622

File tree

1 file changed

+12
-10
lines changed
  • src/main/java/com/github/underscore

1 file changed

+12
-10
lines changed

src/main/java/com/github/underscore/$.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,12 +1052,13 @@ public static <E> E[] union(final E[] ... arrays) {
10521052
}
10531053

10541054
public static <E> List<E> intersection(final List<E> list1, final List<E> list2) {
1055-
return filter(list1, new Predicate<E>() {
1056-
@Override
1057-
public Boolean apply(E elem) {
1058-
return contains(list2, elem);
1055+
final List<E> result = newArrayList();
1056+
for (final E item : list1) {
1057+
if (list2.contains(item)) {
1058+
result.add(item);
10591059
}
1060-
});
1060+
}
1061+
return result;
10611062
}
10621063

10631064
@SuppressWarnings("unchecked")
@@ -1086,12 +1087,13 @@ public static <E> E[] intersection(final E[] ... arrays) {
10861087
}
10871088

10881089
public static <E> List<E> difference(final List<E> list1, final List<E> list2) {
1089-
return filter(list1, new Predicate<E>() {
1090-
@Override
1091-
public Boolean apply(E elem) {
1092-
return !contains(list2, elem);
1090+
final List<E> result = newArrayList();
1091+
for (final E item : list1) {
1092+
if (!list2.contains(item)) {
1093+
result.add(item);
10931094
}
1094-
});
1095+
}
1096+
return result;
10951097
}
10961098

10971099
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)