Riporto qui un esempio di utilizzo della struttura dati IndexedTreeMap (potete vedere la versione originale a questa pagina)
Rispetto alla versione originale io ho preferito modificare l’ ArrayList list in ArrayList list per poterlo poi ordinare tramite l’istruzione java.util.Collections.sort( list );
in tal modo mi assicuro un esatta correlazione quando, tramite il metodo get() andrò a recuperare le coppie Chiave-Valore dalla TreeMap vera e propria.
Ecco l’esempio:
public class PROVAmyIndexedTreeMap
{
public static void main (String[] args)
{
myIndexedTreeMap tmi = new myIndexedTreeMap ();
tmi.put (”ciao”, “hello”);
tmi.put (”casa”, “home”);
System.out.println (tmi.get (0)); // stampa home
System.out.println (tmi.get (1)); // stampa hello
System.out.println();
System.out.println (tmi.get (”ciao”)); // stampa hello
System.out.println (tmi.getKI (”home”)); // stampa -1
System.out.println (tmi.getKI (”casa”)); // 0
System.out.println (tmi.get( tmi.getKI (”casa”) )); // home
System.out.println (tmi.getList()); // [casa, ciao]
}
}
Ed ecco il codice della classe myIndexedTreeMap
//package com.tmi;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.Collections;
public class myIndexedTreeMap
{
private ArrayList list;
private TreeMap map;
// COSTRUTTORE
public myIndexedTreeMap ()
{
list = new ArrayList ();
map = new TreeMap ();
}
public V put (K key, V value)
{
V old = map.get (key);
if (!map.containsKey (key))
{
list.add ((String) key);
Collections.sort (list);
map.put (key, value);
}
return old;
}
public ArrayList getList()
{
return list;
}
public V get (Object key)
{
return map.get (key);
}
public V get (int index)
{
return map.get (list.get (index));
}
public Object getKI (int indice)
{
return list.get (indice);
}
public int getKI (String data_univoca)
{
return list.indexOf (data_univoca);
}
public int size()
{
return map.size();
}
public boolean isEmpty()
{
return ( map.size() == 0 ) ? true : false;
}
public int getIndex_by_key (String data_univoca) // FUNZIONA SE LO CHIAMO getIndexByKey, MA LO CAMBIO
{ // PER MANTENERE OMOGENEITA’ CON IL NOME DELL’ALTRO METODO
return list.indexOf (data_univoca);
}
public Object getKey_by_index (Integer index) // NON FUNZIONA SE LO CHIAMO “getKeyByIndex”
{
return list.get (index);
}
/* AGGIUNTA DA ME IL 7 Marzo 2008 ALLE 15:46
public java.util.Collection values()
{
return map.values();
}*/
/* AGGIUNTA DA ME IL 21 Aprile 2008 ALLE 10:55 *//* TOLTA ALLE 18:13 DI lunedi 12 maggio 2008 */
//public int check()
//{
// return keyDoppioni.size();
//}
/* AGGIUNTA DA ME IL 21 Aprile 2008 ALLE 11:529 *//* TOLTA ALLE 16:30 DI lunedi 12 maggio 2008 */
//public V vs (String $avversaria)
//{
// int locationIndex = avversaria.indexOf($avversaria);
// return map.get ( arraylistORD.get (locationIndex) );
//}
//public ArrayList avv ()
//{
// return avversaria;
//}
}
Compilare digitando sul prompt dei comandi la seguente dicitura
javac PROMAmyIndexedTreeMap.java
ed eseguire il codide
digitando
java PROMAmyIndexedTreeMap
Aspetto di leggere i vostri pareri sull’utilità di questa struttura dati..
.. e se avete consigli su una struttura dati migliore… non esitate a farmelo sapere 
Ciao