BucketMessageGroups implements MessageGroups<C> { @Override public Map<SimpleString, C> toMap() { if (buckets != null && size > 0) { Map<SimpleString, C> map = new HashMap<>(size); for (int bucket = 0; bucket < buckets.length; bucket++) { C value = buckets[bucket]; if (value != null) { map.put(toGroupBucketIntKey(bucket), value); } } return map; } else { return Collections.emptyMap(); } } BucketMessageGroups(int bucketCount); @Override void put(SimpleString key, C consumer); @Override C get(SimpleString key); @Override C remove(SimpleString key); @Override boolean removeIf(Predicate<? super C> filter); @Override void removeAll(); @Override int size(); @Override Map<SimpleString, C> toMap();  }
@Test public void testToMap() { MessageGroups<String> messageGroups = new BucketMessageGroups<>(2); messageGroups.put(toGroupBucketIntKey(1), "world"); assertEquals(1, messageGroups.toMap().size()); messageGroups.put(toGroupBucketIntKey(2), "world2"); assertEquals(2, messageGroups.toMap().size()); Collection<String> values = messageGroups.toMap().values(); assertTrue(values.contains("world")); assertTrue(values.contains("world2")); messageGroups.put(toGroupBucketIntKey(3), "world3"); messageGroups.put(toGroupBucketIntKey(4), "world4"); values = messageGroups.toMap().values(); assertFalse(values.contains("world")); assertFalse(values.contains("world2")); assertTrue(values.contains("world3")); assertTrue(values.contains("world4")); }