SortedList extends ArrayList<MutablePair<K, Integer>> { public void increment(int foundIndex) { int newFrequency = get(foundIndex).getRight() + 1; get(foundIndex).setRight(newFrequency); int currentIndex = foundIndex - 1; while (currentIndex >= 0 && get(currentIndex).getRight() < newFrequency) currentIndex--; if (currentIndex + 1 != foundIndex) Collections.swap(this, currentIndex + 1, foundIndex); }  boolean addNewValue(K value); void increment(int foundIndex);  }
@Test public void testIncrement() { sortedList.addNewValue("value1"); sortedList.addNewValue("value2"); sortedList.addNewValue("value3"); Assert.assertEquals(sortedList.get(0).left, "value1"); sortedList.increment(2); Assert.assertEquals(sortedList.get(0).left, "value3"); sortedList.increment(2); sortedList.increment(1); Assert.assertEquals(sortedList.get(0).left, "value1"); }