Skip to content

Instantly share code, notes, and snippets.

@BoBkiNN
Created March 22, 2024 16:05
Show Gist options
  • Save BoBkiNN/f31529cbe10d2289d2668c68ee90f916 to your computer and use it in GitHub Desktop.
Save BoBkiNN/f31529cbe10d2289d2668c68ee90f916 to your computer and use it in GitHub Desktop.
LinkedHashMap wrapper that provides additional methods controlling position of element in a map.
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class OrderedHashMapWrapper<K, V> implements Map<K, V> {
private final LinkedHashMap<K, V> map;
public OrderedHashMapWrapper() {
this.map = new LinkedHashMap<>();
}
public OrderedHashMapWrapper(Map<K, V> map) {
this.map = new LinkedHashMap<>(map);
}
// Method to insert a new entry after a specified key
public void insertAfter(K existingKey, K newKey, V value) {
LinkedHashMap<K, V> newMap = new LinkedHashMap<>();
boolean found = false;
for (Map.Entry<K, V> entry : map.entrySet()) {
K key = entry.getKey();
V val = entry.getValue();
newMap.put(key, val);
if (key.equals(existingKey)) {
newMap.put(newKey, value);
found = true;
}
}
if (!found) {
throw new IllegalArgumentException("Key " + existingKey + " not found.");
}
map.clear();
map.putAll(newMap);
}
// Method to insert a new entry before a specified key
public void insertBefore(K existingKey, K newKey, V value) {
int index = indexOfKey(existingKey);
if (index != -1) {
int i = 0;
LinkedHashMap<K, V> newMap = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : map.entrySet()) {
if (i == index) {
newMap.put(newKey, value);
}
newMap.put(entry.getKey(), entry.getValue());
i++;
}
map.clear();
map.putAll(newMap);
} else {
throw new IllegalArgumentException("Key " + existingKey + " not found.");
}
}
// Method to add an entry to the beginning of the map
public void addFirst(K key, V value) {
LinkedHashMap<K, V> newMap = new LinkedHashMap<>();
newMap.put(key, value);
map.remove(key); // ensure .putAll() will not replace existing at non-last position
newMap.putAll(map);
map.clear();
map.putAll(newMap);
}
/**
* Add element to last position in map
* @param key key
* @param value value
*/
public void addLast(K key, V value) {
map.remove(key); // ensure .put() will not replace existing at non-last position
map.put(key, value);
}
// Method to get the index of a key in the map
private int indexOfKey(K key) {
int index = 0;
for (K k : map.keySet()) {
if (k.equals(key)) {
return index;
}
index++;
}
return -1;
}
/**
* @return backed up map
*/
public LinkedHashMap<K, V> getMap() {
return map;
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}
@Override
public V get(Object key) {
return map.get(key);
}
@Nullable
@Override
public V put(K key, V value) {
return map.put(key, value);
}
@Override
public V remove(Object key) {
return map.remove(key);
}
@Override
public void putAll(@NotNull Map<? extends K, ? extends V> m) {
map.putAll(m);
}
@Override
public void clear() {
map.clear();
}
@NotNull
@Override
public Set<K> keySet() {
return map.keySet();
}
@NotNull
@Override
public Collection<V> values() {
return map.values();
}
@NotNull
@Override
public Set<Entry<K, V>> entrySet() {
return map.entrySet();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment