Created
February 7, 2015 02:45
-
-
Save anonymous/7a93535a3cfb63623a54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright 2015 ANON GISTER. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
******************************************************************************/ | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
public class IterableLinkedMap<K, V> extends LinkedHashMap<K, V> { | |
LinkedHashMap<Integer, K> keyMap = new LinkedHashMap<Integer, K>(); | |
@Override | |
public V put(K key, V value) { | |
V put = super.put(key, value); | |
if (null == put){ | |
keyMap.put(keyMap.size(), key); | |
} | |
return put; | |
} | |
public V getPosition(int position) { | |
K key = keyMap.get(position); | |
if (key != null){ | |
return get(key); | |
} | |
return null; | |
} | |
@Override | |
public void clear() { | |
super.clear(); | |
keyMap.clear(); | |
} | |
@Override | |
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { | |
return false; | |
} | |
@Override | |
public V remove(Object key) { | |
V remove = super.remove(key); | |
if (remove != null){ | |
int i = 0; | |
LinkedHashMap<Integer, K> newKeyMap = new LinkedHashMap<Integer, K>(); | |
for (K k : keyMap.values()) { | |
if (!k.equals(key)) { | |
newKeyMap.put(i, k); | |
i++; | |
} | |
} | |
assert(newKeyMap.size() == size()); | |
keyMap = newKeyMap; | |
} | |
return remove; | |
} | |
@Override | |
public void putAll(Map<? extends K, ? extends V> map) { | |
for (Map.Entry<? extends K, ? extends V> entry: map.entrySet()){ | |
put(entry.getKey(), entry.getValue()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment