Java实现Map遍历key-value的四种方法
Map遍历key-value的四种方法
(1)先用keySet()取出所有key值,再取出对应value——增强for循环遍历
(2)先用keySet()取出所有key值,再取出对应value——使用迭代器遍历
(3)通过entrySet来获取key-value——增强for循环遍历
(4)通过entrySet来获取key-value——使用迭代器遍历
Map是java中的接口,Map.Entry是Map的一个内部接口。Map提供了一些常用方法,如keySet()、entrySet()等方法,keySet()方法返回值是Map中key值的Set集合;entrySet()的返回值也是返回一个Set集合,此集合的类型为Map.Entry<K, V>。Map.Entry是Map声明的一个内部接口,此接口为泛型,定义为Entry<K,V>。它表示Map中的一个实体(一个key-value对)。接口中有geTKEy(),getValue方法。
1、构造HashMap,给hashMap添加元素
HashMap hashMap = new HashMap();
hashMap.put("三国演义","罗贯中");
hashMap.put("水浒传","施耐庵");
hashMap.put("西游记","吴承恩");
hashMap.put("红楼梦","曹雪芹");
2、方法一:先用keySet()取出所有key值,再取出对应value——使用迭代器遍历
2.1 代码
System.out.println("====1、先用keySet()取出所有key值,再取出对应value——增强for循环遍历====");
Set keyset = hashMap.keySet();
for(Object key:keyset){
System.out.println(key+"-"+hashMap.get(key));
}
2.2 运行结果
3、方法二:先取出所有的key,再通过key取出对应的value——使用迭代器遍历
3.1 代码
System.out.println("====2、先用keySet()取出所有key值,再取出对应value——使用迭代器遍历====");
Iterator iterator = keyset.iterator();
while(iterator.hasNext()){
Object key = iterator.next();
System.out.println(key+"-"+hashMap.get(key));
}
3.2 运行结果
4、方法三:通过entrySet()获得key-value值——增强for循环遍历
4.1 代码
System.out.println("====3、通过entrySet()获得key-value值——增强for循环遍历====");
Set set = hashMap.entrySet();
for(Object key:set){
Map.Entry entry = (Map.Entry) key;
System.out.println(entry.getKey()+"-"+entry.getValue());
}
4.2 运行结果
5、方法四:通过entrySet()获得key-value值——使用迭代器遍历
5.1 代码
System.out.println("====4、通过entrySet()获得key-value值——使用迭代器遍历====");
Set set1 = hashMap.entrySet();
Iterator iterator1 = set1.iterator();
while(iterator1.hasNext()){
Object itset = iterator1.next();
Map.Entry entry = (Map.Entry) itset;
System.out.println(entry.getKey()+"-"+entry.getValue());
}
5.2 运行结果
6、完整代码
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapTraverse {
public static void main(String[] args){
HashMap hashMap = new HashMap();
hashMap.put("三国演义","罗贯中");
hashMap.put("水浒传","施耐庵");
hashMap.put("西游记","吴承恩");
hashMap.put("红楼梦","曹雪芹");
System.out.println("====1、先用keySet()取出所有key值,再取出对应value——增强for循环遍历====");
Set keyset = hashMap.keySet();
for(Object key:keyset){
System.out.println(key+"-"+hashMap.get(key));
}
System.out.println("====2、先用keySet()取出所有key值,再取出对应value——使用迭代器遍历====");
Iterator iterator = keyset.iterator();
while(iterator.hasNext()){
Object key = iterator.next();
System.out.println(key+"-"+hashMap.get(key));
}
System.out.println("====3、通过entrySet()获得key-value值——增强for循环遍历====");
Set set = hashMap.entrySet();
for(Object key:set){
Map.Entry entry = (Map.Entry) key;
System.out.println(entry.getKey()+"-"+entry.getValue());
}
System.out.println("====4、通过entrySet()获得key-value值——使用迭代器遍历====");
Set set1 = hashMap.entrySet();
Iterator iterator1 = set1.iterator();
while(iterator1.hasNext()){
Object itset = iterator1.next();
Map.Entry entry = (Map.Entry) itset;
System.out.println(entry.getKey()+"-"+entry.getValue());
}
}
}
7、应用举例
7.1 问题描述
使用HashMap添加3个员工对象,要求
键:员工id
值:员工对象
并遍历显示工资>18000的员工(遍历方式最少两种)员工类:姓名、工资、员工id
7.2 代码
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapExercise {
public static void main(String[] args) {
HashMap hashMap = new HashMap();
hashMap.put(1, new employee("Tom", 20000.0, 1));
hashMap.put(2, new employee("Jack", 10000.0, 2));
hashMap.put(3, new employee("Bob", 30000.0, 3));
hashMap.put(4, new employee("Marry", 17000.0, 4));
System.out.println("====1、通过keySet()获取所有key值,再得到对应的value值——增强for循环遍历");
Set keyset = hashMap.keySet();
for (Object key : keyset) {
employee m = (employee) hashMap.get(key);
if (m.getSalary() > 18000) {
System.out.println(m);
}
}
System.out.println("====2、通过entrySet()获取key-value值——使用迭代器遍历====");
Set set = hashMap.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object itset = iterator.next();
Map.Entry entry = (Map.Entry) itset;
employee m = (employee) entry.getValue();
if (m.getSalary() > 18000) {
System.out.println(m);
}
}
}
}
class employee{
private String name;
private Double salary;
private int id;
public employee(String name, Double salary, int id) {
this.name = name;
this.salary = salary;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "employee{" +
"name='" + name + '\'' +
", salary=" + salary +
", id=" + id +
'}';
}
}
7.3 运行结果
相关文章