「Java入門」java.util.Setとjava.util.Map.entrySetの使い方
Javaコード
package com.arkgame.itstudy;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Test;
import java.util.Set;
public class KeySetDemo {
@Test
public static void testAA() {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, “First");
map.put(2, “Second");
map.put(3, “Three");
Set<Integer> set = map.keySet();
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
Integer key = it.next();
String value = map.get(key);
System.out.println(key + “<->" + value);
}
}
@Test
public static void testBB() {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(4, “TEST11");
map.put(5, “TEST22");
map.put(6, “TEST33");
Set<Entry<Integer, String>> set = map.entrySet();
for (Entry<Integer, String> entry : set) {
System.out.print(entry.getKey() + “<->");
System.out.println(entry.getValue());
}
}
public static void main(String[] args) {
System.out.println(“java.util.Setの使い方");
testAA();
System.out.println(“java.util.Map.entrySetの使い方");
testBB();
}
}
結果
java.util.Setの使い方
1<->First
2<->Second
3<->Three
java.util.Map.entrySetの使い方
4<->TEST11
5<->TEST22
6<->TEST33