Skip to content

Commit

Permalink
Check whether we have a next element instead of accessing it and catc…
Browse files Browse the repository at this point in the history
…hing the exception. Exceptions are notoriously slow in Java, so we want to avoid that.
  • Loading branch information
StevenArzt authored and JesusFreke committed Mar 8, 2022
1 parent 78a8293 commit 8533431
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions dexlib2/src/main/java/org/jf/util/CollectionUtils.java
Expand Up @@ -78,11 +78,10 @@ public static <T> int compareAsIterable(@Nonnull Comparator<? super T> comparato
Iterator<? extends T> elements2 = it2.iterator();
for (T element1: it1) {
T element2;
try {
element2 = elements2.next();
} catch (NoSuchElementException ex) {
if (!elements2.hasNext()) {
return 1;
}
element2 = elements2.next();
int res = comparator.compare(element1, element2);
if (res != 0) return res;
}
Expand All @@ -97,11 +96,10 @@ public static <T extends Comparable<? super T>> int compareAsIterable(@Nonnull I
Iterator<? extends T> elements2 = it2.iterator();
for (T element1: it1) {
T element2;
try {
element2 = elements2.next();
} catch (NoSuchElementException ex) {
if (!elements2.hasNext()) {
return 1;
}
element2 = elements2.next();
int res = element1.compareTo(element2);
if (res != 0) return res;
}
Expand Down

0 comments on commit 8533431

Please sign in to comment.