℡。sc 9:58:21 1.HashMap<Person,Person> 2.HashMap<String,String> ℡。sc 9:58:48 分别用keySet+增强for,keySet+迭代器遍历
package zuoyeDemo02; public class Persons { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Persons(String name, int age) { super(); this.name = name; this.age = age; } public Persons() { super(); } @Override public String toString() { return "Persons [name=" + name + ", age=" + age + "]"; } }
package zuoyeDemo02; public class Personss { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Personss [name=" + name + ", age=" + age + "]"; } public Personss(String name, int age) { super(); this.name = name; this.age = age; } public Personss() { super(); } }
package zuoyeDemo02; import java.util.HashMap; import java.util.Iterator; import java.util.Set; public class Demo033 { public static void main(String[] args) { HashMap<Persons, Personss> ha=new HashMap<Persons, Personss>(); ha.put(new Persons("a",10),new Personss("b",20)); ha.put(new Persons("b",15), new Personss("a",19)); ha.put(new Persons("c",15), new Personss("d",19)); // 遍历keySet+增强for // 1.获取所有key所在的Set集合 Set<Persons> ste= ha.keySet(); // 遍历set得到key每一个值 for(Persons s:ste){ System.out.println(s+".."+ha.get(s)); } // 遍历 keyset+迭代器 // 1.获取所有key所在的Set集合 Set<Persons> se=ha.keySet(); Iterator<Persons> it= se.iterator(); // 循环 while (it.hasNext()) { Persons p=it.next(); System.out.println(p+"..."+ha.get(p)); } } }
package comdame;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Demo22 {
public static void main(String[] args) {
HashMap<String, String> ha=new HashMap<String, String>();
// 存值
ha.put("a", "ca");
ha.put("b", "ca");
// 遍历 keyset+迭代器
// 1.获取所有key所在的Set集合
Set<String> se= ha.keySet();
for(String s:se){
System.out.println(s+"..."+ha.get(s));
}
// 遍历keySet+增强for
// 1.获取所有key所在的Set集合
Set<String> set=ha.keySet();
// 创建 iterator对象
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String ss=it.next();
System.out.println(ss+"..."+ha.get(ss));
}
}
}