HashMap应用案例三:存储国家编号及国家信息
案例目的:熟练应用 HashMap 存储及获取信息
案例需求:以国家编号为 Key,国家信息为 Value,将信息存储到 HashMap<Integer,Country>
数据结构中。要求如下:A. 至少存储三条 国家编号和国家信息 B. 可以通过国家编号查
询国家信息并显示 C. 修改指定国家编号的国家信息。
案例解析:
1. 该题目主要考察的是 HashMap 的基本使用。
2. 其中问题 A 主要考察的是 HashMap 的数据存储,主要利用了 put(键,值)方法。如
上图中的 map.put(1001 , new Country("中国" , "东亚") ); ;问题 B 主要是考察根据 Key
得到对应 Value 的方法,该方法为 value = get( key ),其示例代码如上图中的 Country
country = map.get(countryId); ;问题 C 主要考察了,如何遍历 HashMap 集合,一般的
思路为先得到键的集合(代码如:Set<Integer> keys = map.keySet();),再遍历键的集合,
再通过问题 B 中讲过的根据 Key 得到值的 get 方法,将值遍历得出。代码如上图中所
示。
案例实现:
Country.java(国家信息类)
package www.yiniuedu.pojo;
/**
* Country.java(国家类)
*
* @author bsh
*
*/
public class Country {
private Integer cNo;// 国家编号
private String cName;// 国家名称
private String cAddress;// 国家地理位置
public Country() {
super();
}
public Country(Integer cNo, String cName, String cAddress) {
super();
this.cNo = cNo;
this.cName = cName;
this.cAddress = cAddress;
}
@Override
public String toString() {
return "国家信息 [国家编号=" + cNo + ", 国家名称=" + cName + ", 国家位置=" + cAddress + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cAddress == null) ? 0 : cAddress.hashCode());
result = prime * result + ((cName == null) ? 0 : cName.hashCode());
result = prime * result + ((cNo == null) ? 0 : cNo.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;
Country other = (Country) obj;
if (cAddress == null) {
if (other.cAddress != null)
return false;
} else if (!cAddress.equals(other.cAddress))
return false;
if (cName == null) {
if (other.cName != null)
return false;
} else if (!cName.equals(other.cName))
return false;
if (cNo == null) {
if (other.cNo != null)
return false;
} else if (!cNo.equals(other.cNo))
return false;
return true;
}
public Integer getcNo() {
return cNo;
}
public void setcNo(Integer cNo) {
this.cNo = cNo;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
public String getcAddress() {
return cAddress;
}
public void setcAddress(String cAddress) {
this.cAddress = cAddress;
}
}
TestCountry.java(国家信息测试类)
package www.yiniuedu.test;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import www.yiniuedu.pojo.Country;
public class TestCountry {
public static void main(String[] args) {
Country c1 = new Country(86,"中国","东亚");
Country c2 = new Country(186,"英国","欧州西北部");
Country c3 = new Country(861,"美国","北美");
Map<Integer,Country> map = new HashMap<Integer,Country>();
map.put(c1.getcNo(), c1);
map.put(c2.getcNo(), c2);
map.put(c3.getcNo(), c3);
System.out.println("请输入要查询的国家编号:");
Scanner sc = new Scanner(System.in);
Integer id = sc.nextInt();
Set<Integer> set = map.keySet();
for(Integer i:set) {
if(i.equals(id)) {
Country c = map.get(i);
System.out.println(c);
System.out.println("请输入国家名称:");
String name = sc.next();
System.out.println("请输入国家地理位置:");
String address = sc.next();
c.setcName(name);
c.setcAddress(address);
map.put(i, c);
}
}
for(Integer i:set) {
System.out.println(map.get(i));
}
}
}
测试结果:
创作不易,耗时用力,小手轻点,顺手打赏
微信打赏
QQ打赏