Created
November 10, 2015 11:04
-
-
Save appersiano/4aae89e308ee85d465fd to your computer and use it in GitHub Desktop.
Get Key and Value of a Map by Index
This file contains hidden or 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
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.Set; | |
public class MyLinkedMap<K, V> extends LinkedHashMap<K, V> | |
{ | |
/** | |
* Ottieni la chiave in base all'indice | |
* @param i indice | |
* @return | |
*/ | |
public K getKey(int i) | |
{ | |
Map.Entry<K, V>entry = this.getEntry(i); | |
if(entry == null) return null; | |
return entry.getKey(); | |
} | |
/** | |
* Ottieni il valore in base all'indice | |
* @param i indice | |
* @return | |
*/ | |
public Map.Entry<K, V> getEntry(int i) | |
{ | |
// check if negetive index provided | |
Set<Entry<K,V>> entries = entrySet(); | |
int j = 0; | |
for(Map.Entry<K, V>entry : entries) | |
if(j++ == i)return entry; | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment