import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class Main {
private static HashMap<Integer, Person> map ;
public static void main(String [] args){
map = new HashMap<>();
System.out.println("hello word");
System.out.println("-----------1----------");
for (int i = 0; i < 10; i++) {
Person p = new Person("john", i, "android");
map.put(i, p);
}
for(Map.Entry<Integer, Person> e : map.entrySet()){
System.out.println(e.getValue());
}
System.out.println("-----------2----------");
Iterator<Entry<Integer, Person>> i = map.entrySet().iterator();
while (i.hasNext()) {
Entry<Integer, Person> entry = i.next();
System.out.println(entry.getValue());
}
System.out.println("-----------3----------");
for(Person p : map.values()){
System.out.println(p);
}
System.out.println("-----------4----------");
for(int in : map.keySet()){
System.out.println(map.get(in));
}
}
}
class Person {
private String name;
private int age;
private String job;
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", job=" + job + "]";
}
public Person(String name, int age, String job) {
super();
this.name = name;
this.age = age;
this.job = job;
}
}
Java中Map遍历的四种方法
最新推荐文章于 2024-03-25 20:33:26 发布