Domanda

Se .NET ha un SortedDictionary oggetto ... ciò che è questo in Java, per favore? Ho anche bisogno di essere in grado di recuperare un Enumeration (di elementi), nel codice Java .. quindi posso solo iterare su tutti i tasti.

Sto pensando che sia un TreeMap ? Ma non credo che ha un Enumeration che viene esposta?

Tutte le idee?

È stato utile?

Soluzione

TreeMap sarebbe la scelta giusta. Per quanto riguarda il Collection di tutti i tasti (o valori), eventuali espone Map keySet() e values().

EDIT (per rispondere alla tua domanda con tag del codice). Supponendo di aver un Map<String, Object>:

for (String key : map.keySet()) {
     System.out.println(key); // prints the key
     System.out.println( map.get(key) ); // prints the value
}

È inoltre possibile utilizzare al posto di entrySet() keySet() o values() per scorrere l'KEY-> coppie di valori.

Altri suggerimenti

TreeMap è probabilmente la cosa più vicina che si sta andando a trovare.

È possibile scandire le chiavi chiamando TreeMap.keySet(); e l'iterazione di Set che viene restituito:

// assume a TreeMap<String, String> called treeMap
for(String key : treeMap.keySet())
{
    string value = treeMap[key];
}

Sarebbe l'equivalente di:

// assume a SortedDictionary called sortedDictionary
foreach(var key in sortedDictionary.Keys)
{
    var value = sortedDictionary[key];
}



Si potrebbe anche provare quanto segue:

// assume TreeMap<String, String> called treeMap
for (Map.Entry<String, String> entry : treeMap.entrySet())
{
    String key = entry.getKey();
    String value = entry.getValue();
}

che è l'equivalente al seguente codice di .NET:

// assume SortedDictionary<string, string> called sortedDictionary
foreach(KeyValuePair<string, string> kvp in sortedDictionary)
{
    var key = kvp.Key;
    var value = kvp.Value;
}

Quello che vi serve è entrySet () metodo di SortedMap (TreeMap).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top