Skip to content

Instantly share code, notes, and snippets.

@appersiano
Created November 10, 2015 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appersiano/4aae89e308ee85d465fd to your computer and use it in GitHub Desktop.
Save appersiano/4aae89e308ee85d465fd to your computer and use it in GitHub Desktop.
Get Key and Value of a Map by Index
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