Skip to content

Commit c49ab6b

Browse files
authored
MINOR: Optimize map lookup efficiency with getOrDefault (#20229)
Optimized `getRemainingRecords()` method by replacing inefficient `containsKey() + get()` pattern with `getOrDefault()` to reduce map lookups from 2 to 1 per partition. Reviewers: Chia-Ping Tsai <[email protected]>
1 parent 57e9f98 commit c49ab6b

File tree

1 file changed

+1
-5
lines changed

1 file changed

+1
-5
lines changed

examples/src/main/java/kafka/examples/ExactlyOnceMessageProcessor.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,7 @@ private long getRemainingRecords(KafkaConsumer<Integer, String> consumer) {
216216
}
217217
return consumer.assignment().stream().mapToLong(partition -> {
218218
long currentPosition = consumer.position(partition);
219-
if (fullEndOffsets.containsKey(partition)) {
220-
return fullEndOffsets.get(partition) - currentPosition;
221-
} else {
222-
return 0;
223-
}
219+
return fullEndOffsets.getOrDefault(partition, currentPosition) - currentPosition;
224220
}).sum();
225221
}
226222

0 commit comments

Comments
 (0)