Map集合的遍历(方式1)
转换为Map集合中的操作: 1.获取所有键的集合。用keySet()方法实现 2.遍历键的集合,获取到每一个键。用增强for实现 3.根据键去找值。用get(Object key)方法实现
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class map1 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String, String>();
map.put("cc1", "cc2");
map.put("zz1", "zz2");
map.put("ww1", "ww2");
Set<String> ks = map.keySet();
for(String key : ks) {
String value = map.get(key);
System.out.println(key + "," + value);
}
}
}
输出
Map集合的遍历(方式2)
把Map看成是一个夫妻对的集合遍历思路 1、获取所有结婚证的集合 2、遍历结婚证的集合,得到每一个结婚证 3、根据结婚证获取丈夫和妻子
转换为Map集合中的操作: 1、获取所有键值对对象的集合
Set<Map.Entry<K,V>>entrySet():获取所有键值对对象的集合
2、遍历键值对对象的集合,得到每一个键值对对象 用增强for实现,得到每一个Map.Entry 3、根据键值对对象获取键和值 用getKey()得到键 用getValue()得到值
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class map2 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("cc1", "cc2");
map.put("zz1", "zz2");
map.put("ww1", "ww2");
Set<Map.Entry<String, String>> es = map.entrySet();
for(Map.Entry<String, String> me : es) {
String key = me.getKey();
String value = me.getValue();
System.out.println(key + "," + value);
}
}
}
输出
HashMap集合存储学生对象并遍历
需求:创建一个HashMap集合,键是学号(String),值是学生对象(Student)。存储三个键值对元素,并遍历
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Student1> hm = new HashMap<String, Student1>();
Student1 s1 = new Student1("cc1", 20);
Student1 s2 = new Student1("cc2", 18);
Student1 s3 = new Student1("cc3", 22);
hm.put("001", s1);
hm.put("002", s2);
hm.put("003", s3);
Set<String> ks = hm.keySet();
for(String key : ks) {
Student1 value = hm.get(key);
System.out.println(key + "," + value.getName() + "," + value.getAge());
}
Set<Map.Entry<String, Student1>> es = hm.entrySet();
for(Map.Entry<String, Student1> me : es) {
String key = me.getKey();
Student1 value = me.getValue();
System.out.println(key + "," + value.getName() + "," + value.getAge());
}
}
}
public class Student1 {
private String name;
private int age;
public Student1() {
}
public Student1(String name, int age) {
this.name = name;
this.age = 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;
}
}
输出
HashMap集合存储学生对象并遍历
需求:创建一个HashMap集合,键是学生对象(Student),值是居住地(String)。存储多个键值对元素,并遍历。 要保证键的唯一性:如果学生对象的成员变量值相同,我们就认为是同一个对象
import java.util.HashMap;
import java.util.Set;
public class HashMap2 {
public static void main(String[] args) {
HashMap<Student1, String> hm = new HashMap<Student1, String>();
Student1 s1 = new Student1("cc1", 20);
Student1 s2 = new Student1("cc2", 18);
Student1 s3 = new Student1("cc3", 22);
Student1 s4 = new Student1("cc3", 22);
hm.put(s1, "西安");
hm.put(s2, "武汉");
hm.put(s3, "郑州");
hm.put(s4, "北京");
Set<Student1> ks = hm.keySet();
for(Student1 key : ks) {
String value = hm.get(key);
System.out.println(key.getName() + "," + key.getAge() + "," + value);
}
}
}
public class Student1 {
private String name;
private int age;
public Student1() {
}
public Student1(String name, int age) {
this.name = name;
this.age = 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 int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student1 other = (Student1) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
如果不调用equals(),hashCode() 调用后输出 新加的键和值将覆盖之前的
ArrayList集合存储HashMap元素并遍历
需求:创建一个ArrayList集合,存储三个元素,每一个元素都是HashMap,每一个HashMap的键和值都是String,并遍历。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class ArrayListHashMap {
public static void main(String[] args) {
ArrayList<HashMap<String, String>> a = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hm1 = new HashMap<String, String>();
hm1.put("cc1", "cc2");
a.add(hm1);
HashMap<String, String> hm2 = new HashMap<String, String>();
hm1.put("zz1", "zz2");
a.add(hm2);
HashMap<String, String> hm3 = new HashMap<String, String>();
hm1.put("ww1", "ww2");
a.add(hm3);
for(HashMap<String, String> hm : a) {
Set<String> ks = hm.keySet();
for(String key : ks) {
String value = hm.get(key);
System.out.println(key + "," + value);
}
}
}
}
输出
HashMap集合存储ArrayList元素并遍历
需求:创建一个HashMap集合,存储三个元素,每一个键值对元素的键是String,值是ArrayList,每一个ArrayList的元素是String,并遍历。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class HashMapArrayList {
public static void main(String[] args) {
HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
ArrayList<String> s1 = new ArrayList<String>();
s1.add("cc1");
hm.put("zz", s1);
ArrayList<String> s2 = new ArrayList<String>();
s2.add("cc2");
hm.put("bb", s2);
ArrayList<String> s3 = new ArrayList<String>();
s3.add("cc3");
hm.put("ww", s3);
Set<String> ks = hm.keySet();
for(String key : ks) {
System.out.println(key);
ArrayList<String> value = hm.get(key);
for(String s : value) {
System.out.println("\t" + s);
}
}
}
}
输出
|