Mapオブジェクトを定義し、各要素のkeyとvalueを巡回して印刷します。
1332 ワード
import java.util.hashMap;import java.util.Map;import java.util.Map.Entry;
import comp.itheime.bean.Person;
public class Test 2{
import comp.itheime.bean.Person;
public class Test 2{
/**
* 2、 , main Map ( ),
* , key value。
*/
public static void main(String[] args) {
// HashMap Person
HashMap hm = new HashMap<>();
// key Person value
hm.put(new Person(" ",25), " ");
hm.put(new Person(" ",23), " ");
hm.put(new Person(" ",20), " ");
hm.put(new Person(" ",18), " ");
hm.put(new Person("IT ",1), " ");
/*
key value
hm.keySet() key */
System.out.println("........... hm.keySet()...........");
for (Person personkey : hm.keySet()) {
System.out.println(" : "+personkey.getName()+", : "+personkey.getAge()+", : "+hm.get(personkey));
}
System.out.println("........... hm.entrySet()...........");
/* */
for (Entry en : hm.entrySet()){
System.out.println(" : "+en.getKey().getName()+", : "+en.getKey().getAge()+", Value: "+en.getValue());
}
}
)