### Question:
HotParameterLeapArray extends LeapArray<ParamMapBucket> { public Map<Object, Double> getTopValues(RollingParamEvent event, int number) { currentWindow(); List<ParamMapBucket> buckets = this.values(); Map<Object, Integer> result = new HashMap<Object, Integer>(); for (ParamMapBucket b : buckets) { Set<Object> subSet = b.ascendingKeySet(event); for (Object o : subSet) { Integer count = result.get(o); if (count == null) { count = b.get(event, o); } else { count += b.get(event, o); } result.put(o, count); } } Set<Entry<Object, Integer>> set = result.entrySet(); List<Entry<Object, Integer>> list = new ArrayList<Entry<Object, Integer>>(set); Collections.sort(list, new Comparator<Entry<Object, Integer>>() { @Override public int compare(Entry<Object, Integer> a, Entry<Object, Integer> b) { return (b.getValue() == null ? 0 : b.getValue()) - (a.getValue() == null ? 0 : a.getValue()); } }); Map<Object, Double> doubleResult = new HashMap<Object, Double>(); int size = list.size() > number ? number : list.size(); for (int i = 0; i < size; i++) { Map.Entry<Object, Integer> x = list.get(i); if (x.getValue() == 0) { break; } doubleResult.put(x.getKey(), ((double)x.getValue()) / getIntervalInSecond()); } return doubleResult; } HotParameterLeapArray(int sampleCount, int intervalInMs); @Override ParamMapBucket newEmptyBucket(long timeMillis); void addValue(RollingParamEvent event, int count, Object value); Map<Object, Double> getTopValues(RollingParamEvent event, int number); long getRollingSum(RollingParamEvent event, Object value); double getRollingAvg(RollingParamEvent event, Object value);  }### Answer:
@Test public void testGetTopValues() { int intervalInSec = 2; int a1 = 3, a2 = 5; String paramPrefix = "param-"; HotParameterLeapArray leapArray = mock(HotParameterLeapArray.class); when(leapArray.getIntervalInSecond()).thenReturn((double) intervalInSec); final ParamMapBucket b1 = generateBucket(a1, paramPrefix); final ParamMapBucket b2 = generateBucket(a2, paramPrefix); List<ParamMapBucket> buckets = new ArrayList<ParamMapBucket>() {{ add(b1); add(b2); }}; when(leapArray.values()).thenReturn(buckets); when(leapArray.getTopValues(any(RollingParamEvent.class), any(int.class))).thenCallRealMethod(); Map<Object, Double> top2Values = leapArray.getTopValues(RollingParamEvent.REQUEST_PASSED, a1 - 1); assertEquals((double)5 * 10 / intervalInSec, top2Values.get(paramPrefix + 5), 0.01); assertEquals((double)3 * 20 / intervalInSec, top2Values.get(paramPrefix + 3), 0.01); Map<Object, Double> top4Values = leapArray.getTopValues(RollingParamEvent.REQUEST_PASSED, a2 - 1); assertEquals(a2 - 1, top4Values.size()); assertFalse(top4Values.containsKey(paramPrefix + 1)); Map<Object, Double> topMoreValues = leapArray.getTopValues(RollingParamEvent.REQUEST_PASSED, a2 + 1); assertEquals("This should contain all parameters but no more than " + a2, a2, topMoreValues.size()); }