Reputation: 321
I have two maps with the same size but the type of their values are different.
Now I need to iterate them and generate new map.
I tried stream but the Map.Entry
cannot be resolved. I'm using JDK 11;
Map<TopicPartition, OffsetAndMetadata> consumerGroupOffsets = getConsumerGroupOffsets(groupId);
Map<TopicPartition, Long> topicEndOffsets = getTopicEndOffsets(groupId, consumerGroupOffsets.keySet());
Map<Object, Object> consumerGroupLag = consumerGroupOffsets.entrySet().stream()
.map(entry -> mapEntry(entry.getKey(), new OffsetAndLag(topicEndOffsets.get(entry.getKey()), entry.getValue().offset())))
Upvotes: 0
Views: 441
Reputation: 40034
It's a little confusing where the lag is coming from since it appears you are retrieving offsets only.
But this is how it would be structured to get the long values regardless of what they are called (I guessed at the constructor for the OffsetAndLag
class)
Map<TopicPartition, OffsetAndMetadata> consumerGroupOffsets = ...;
Map<TopicPartition, Long> topicEndOffsets = ...;
consumerGroupOffsets
for the target key.topicEndOffsets
OffsetAndMetadata
) to get the offset (or lag)Map<TopicPartition, OffsetAndLag> consumerGroupLag =
consumerGroupOffsets.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey,
entry -> new OffsetAndLag(
topicEndOffsets
.get(entry.getKey()),
entry.getValue().offset())));
Expected OffsetAndLag
class (or something similar)
static class OffsetAndLag {
public long offset;
public long lag;
public OffsetAndLag(long offset, long lag) {
this.offset = offset;
this.lag = lag;
}
}
Upvotes: 1