import java.util.Set;このコードを実行すると、次のようになります:
import java.util.TreeMap;
public class Parcours_treemap {
public static void main(String a[]){
TreeMap<文字列、文字列>tm = new TreeMap<文字列、文字列>();
//キーと値のペアを追加します
tm.put("first", "element1");
tm.put("second", "element2");
tm.put("third","element3");
セット<文字列>キー= tm.keySet();
for(String key: keys){
System.out.println(""+key+" の値は "+tm.get(key));
}
}
}
2 番目の値は次のようになります。 element2
最初の値は次のようになります。 element1
3番目の値はelement3
import java.util.Iterator;このコードは、実行後に次の結果を返します:
import java.util.Set;
import java.util.TreeMap;
import java.util.Map;
public class TreeMap_iterator {
public static void main(String a[]){
// TreeMap を作成します
TreeMap<文字列、文字列>tmap = new TreeMap<文字列、文字列>();
// TreeMap にキーと値のペアを追加します
tmap.put("Key1","element1");
tmap.put("キー2","要素2");
tmap.put("キー3","element3");
tmap.put("キー4","要素4");
// すべての入力を取得
Set set = tmap.entrySet();
// リストを参照するためのイテレータを取得します
Iterator it = set.iterator();
// キーと値のピアを表示します
while(it.hasNext()) {
Map.Entry mentry = (Map.Entry)it.next();
System.out.print("キー: "+mentry.getKey() + " - ");
System.out.println("値: "+mentry.getValue());
}
}
}
key: キー1 - 値: element1
key: キー2 - 値: element2
key: キー3 - 値: element3
key: Key4 - 値: element4
Please disable your ad blocker and refresh the window to use this website.