@Test public void testMoveEntryInInsertSortedMap_SecondToLast_Down() { final Map<String, Integer> map = new LinkedHashMap<>(3); map.put("1", 1); map.put("2", 2); map.put("3", 3); map.put("4", 4); CollectionUtil.moveEntryInInsertSortedMap(map, "3", true); Assert.assertEquals(Arrays.asList("1", "2", "4", "3"), new ArrayList<>(map.keySet())); }
public static <K, V> void moveEntryInInsertSortedMap(final Map<K, V> insertSortedMap, final K entryKey, final boolean increaseIndexByOne) { final List<K> keyList = new ArrayList<>(insertSortedMap.keySet()); final int index = keyList.indexOf(entryKey); final int indexToSwitchWith; if (increaseIndexByOne) { indexToSwitchWith = index + 1; } else { indexToSwitchWith = index - 1; } final int totalEntryCount = keyList.size(); if (index == -1 || indexToSwitchWith == -1 || indexToSwitchWith == totalEntryCount) { throw new IllegalArgumentException(); } final Map<K, V> groupsCopy = new LinkedHashMap<>(insertSortedMap); insertSortedMap.keySet().retainAll(keyList.subList(0, Math.min(index, indexToSwitchWith))); final K entryToSwitchWith = keyList.get(indexToSwitchWith); if (increaseIndexByOne) { insertSortedMap.put(entryToSwitchWith, groupsCopy.get(entryToSwitchWith)); insertSortedMap.put(entryKey, groupsCopy.get(entryKey)); } else { insertSortedMap.put(entryKey, groupsCopy.get(entryKey)); insertSortedMap.put(entryToSwitchWith, groupsCopy.get(entryToSwitchWith)); } final int firstTrailingRetainedIndex = Math.max(index, indexToSwitchWith) + 1; if (firstTrailingRetainedIndex < totalEntryCount) { groupsCopy.keySet().retainAll(keyList.subList(firstTrailingRetainedIndex, totalEntryCount)); insertSortedMap.putAll(groupsCopy); } }
CollectionUtil { public static <K, V> void moveEntryInInsertSortedMap(final Map<K, V> insertSortedMap, final K entryKey, final boolean increaseIndexByOne) { final List<K> keyList = new ArrayList<>(insertSortedMap.keySet()); final int index = keyList.indexOf(entryKey); final int indexToSwitchWith; if (increaseIndexByOne) { indexToSwitchWith = index + 1; } else { indexToSwitchWith = index - 1; } final int totalEntryCount = keyList.size(); if (index == -1 || indexToSwitchWith == -1 || indexToSwitchWith == totalEntryCount) { throw new IllegalArgumentException(); } final Map<K, V> groupsCopy = new LinkedHashMap<>(insertSortedMap); insertSortedMap.keySet().retainAll(keyList.subList(0, Math.min(index, indexToSwitchWith))); final K entryToSwitchWith = keyList.get(indexToSwitchWith); if (increaseIndexByOne) { insertSortedMap.put(entryToSwitchWith, groupsCopy.get(entryToSwitchWith)); insertSortedMap.put(entryKey, groupsCopy.get(entryKey)); } else { insertSortedMap.put(entryKey, groupsCopy.get(entryKey)); insertSortedMap.put(entryToSwitchWith, groupsCopy.get(entryToSwitchWith)); } final int firstTrailingRetainedIndex = Math.max(index, indexToSwitchWith) + 1; if (firstTrailingRetainedIndex < totalEntryCount) { groupsCopy.keySet().retainAll(keyList.subList(firstTrailingRetainedIndex, totalEntryCount)); insertSortedMap.putAll(groupsCopy); } } }
CollectionUtil { public static <K, V> void moveEntryInInsertSortedMap(final Map<K, V> insertSortedMap, final K entryKey, final boolean increaseIndexByOne) { final List<K> keyList = new ArrayList<>(insertSortedMap.keySet()); final int index = keyList.indexOf(entryKey); final int indexToSwitchWith; if (increaseIndexByOne) { indexToSwitchWith = index + 1; } else { indexToSwitchWith = index - 1; } final int totalEntryCount = keyList.size(); if (index == -1 || indexToSwitchWith == -1 || indexToSwitchWith == totalEntryCount) { throw new IllegalArgumentException(); } final Map<K, V> groupsCopy = new LinkedHashMap<>(insertSortedMap); insertSortedMap.keySet().retainAll(keyList.subList(0, Math.min(index, indexToSwitchWith))); final K entryToSwitchWith = keyList.get(indexToSwitchWith); if (increaseIndexByOne) { insertSortedMap.put(entryToSwitchWith, groupsCopy.get(entryToSwitchWith)); insertSortedMap.put(entryKey, groupsCopy.get(entryKey)); } else { insertSortedMap.put(entryKey, groupsCopy.get(entryKey)); insertSortedMap.put(entryToSwitchWith, groupsCopy.get(entryToSwitchWith)); } final int firstTrailingRetainedIndex = Math.max(index, indexToSwitchWith) + 1; if (firstTrailingRetainedIndex < totalEntryCount) { groupsCopy.keySet().retainAll(keyList.subList(firstTrailingRetainedIndex, totalEntryCount)); insertSortedMap.putAll(groupsCopy); } }  }
CollectionUtil { public static <K, V> void moveEntryInInsertSortedMap(final Map<K, V> insertSortedMap, final K entryKey, final boolean increaseIndexByOne) { final List<K> keyList = new ArrayList<>(insertSortedMap.keySet()); final int index = keyList.indexOf(entryKey); final int indexToSwitchWith; if (increaseIndexByOne) { indexToSwitchWith = index + 1; } else { indexToSwitchWith = index - 1; } final int totalEntryCount = keyList.size(); if (index == -1 || indexToSwitchWith == -1 || indexToSwitchWith == totalEntryCount) { throw new IllegalArgumentException(); } final Map<K, V> groupsCopy = new LinkedHashMap<>(insertSortedMap); insertSortedMap.keySet().retainAll(keyList.subList(0, Math.min(index, indexToSwitchWith))); final K entryToSwitchWith = keyList.get(indexToSwitchWith); if (increaseIndexByOne) { insertSortedMap.put(entryToSwitchWith, groupsCopy.get(entryToSwitchWith)); insertSortedMap.put(entryKey, groupsCopy.get(entryKey)); } else { insertSortedMap.put(entryKey, groupsCopy.get(entryKey)); insertSortedMap.put(entryToSwitchWith, groupsCopy.get(entryToSwitchWith)); } final int firstTrailingRetainedIndex = Math.max(index, indexToSwitchWith) + 1; if (firstTrailingRetainedIndex < totalEntryCount) { groupsCopy.keySet().retainAll(keyList.subList(firstTrailingRetainedIndex, totalEntryCount)); insertSortedMap.putAll(groupsCopy); } }  static boolean containsInstance(final Collection<? extends T> collection, final T instance); static int indexOfInstance(final Collection<? extends T> collection, final T instance); static Map<T, AtomicInteger> countOccurrences(final Collection<T> collection); static void moveEntryInInsertSortedMap(final Map<K, V> insertSortedMap, final K entryKey, final boolean increaseIndexByOne); static String toString(final Collection<?> collection, final String separator); }
CollectionUtil { public static <K, V> void moveEntryInInsertSortedMap(final Map<K, V> insertSortedMap, final K entryKey, final boolean increaseIndexByOne) { final List<K> keyList = new ArrayList<>(insertSortedMap.keySet()); final int index = keyList.indexOf(entryKey); final int indexToSwitchWith; if (increaseIndexByOne) { indexToSwitchWith = index + 1; } else { indexToSwitchWith = index - 1; } final int totalEntryCount = keyList.size(); if (index == -1 || indexToSwitchWith == -1 || indexToSwitchWith == totalEntryCount) { throw new IllegalArgumentException(); } final Map<K, V> groupsCopy = new LinkedHashMap<>(insertSortedMap); insertSortedMap.keySet().retainAll(keyList.subList(0, Math.min(index, indexToSwitchWith))); final K entryToSwitchWith = keyList.get(indexToSwitchWith); if (increaseIndexByOne) { insertSortedMap.put(entryToSwitchWith, groupsCopy.get(entryToSwitchWith)); insertSortedMap.put(entryKey, groupsCopy.get(entryKey)); } else { insertSortedMap.put(entryKey, groupsCopy.get(entryKey)); insertSortedMap.put(entryToSwitchWith, groupsCopy.get(entryToSwitchWith)); } final int firstTrailingRetainedIndex = Math.max(index, indexToSwitchWith) + 1; if (firstTrailingRetainedIndex < totalEntryCount) { groupsCopy.keySet().retainAll(keyList.subList(firstTrailingRetainedIndex, totalEntryCount)); insertSortedMap.putAll(groupsCopy); } }  static boolean containsInstance(final Collection<? extends T> collection, final T instance); static int indexOfInstance(final Collection<? extends T> collection, final T instance); static Map<T, AtomicInteger> countOccurrences(final Collection<T> collection); static void moveEntryInInsertSortedMap(final Map<K, V> insertSortedMap, final K entryKey, final boolean increaseIndexByOne); static String toString(final Collection<?> collection, final String separator);  }