Mapから全てのキーと値のセットを取得するサンプルコードです。
Mapから全てのキーと値のセットを取得するためにはまず、entrySet()メソッドで、キーと値のセットを格納するSet型のコレクションを取得します。そして、取得したコレクションから反復子(Iterator)を使用して、キーと値のセットを持つMap.Entry型のオブジェクトを取得します。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class GetAllMap {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(new Integer(1), "A");
map.put(new Integer(2), "B");
map.put(new Integer(3), "C");
map.put(new Integer(4), "D");
map.put(new Integer(5), "E");
Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
Iterator<Map.Entry<Integer, String>> it = entrySet.iterator();
while(it.hasNext())
{
Map.Entry<Integer, String> entry = it.next();
Integer key = entry.getKey();
String value = entry.getValue();
System.out.format("%d : %s \r\n", key,value);
}
}
}
ちょっと一休み. Javaキーワード並び替えゲーム
画面に表示される文字列を並び替えるとJavaに関連するキーワードになります。ヒントをたよりに並び替えを行ってエンターを押してください。
ユーザ登録をしてログインするとランキングに参加できます。