集合(二)
Map接口:既不继承Collection接口,也不继承Set和List接口按键值对(key/value)存储元素
实现类:
HashMap Hashtable TreeMap
HashMap:常用类.访问速度快,可以允许null key和null value
Hashtable:访问速度慢,不允许null key和null value
TreeMap: 会对添加的key进行排序,不允许null key,可以使用null value.
一个简单的Map集合例子
import java.util.HashMap;
import java.util.Map;
public class MapTest01 {
public static void main(String[] args) {
// 创建一个 Map集合
Map<Integer, String> m = new HashMap<Integer, String>();
// 在map集合里用put 添加数据
m.put(101, "张三");
m.put(104, null);
m.put(102, "张三");
m.put(103, "李四");
m.put(103, "王五");
// key 是唯一值 value是 不是唯一值
// key 是使用 set集合存储的 而 value使用哦过的collection存储的
System.out.println(m.toString());
}
}
运行结果
简单的TreeMap排序
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class Test09 {
public static void main(String[] args) {
Map<Integer, String> m = new TreeMap<Integer, String>();
m.put(1, "张三");
m.put(5, "王五");
m.put(3, "李四");
m.put(2, "麻子");
Set<Entry<Integer, String>> se = m.entrySet();
Iterator<Entry<Integer,String>> it = se.iterator();//迭代
while(it.hasNext()){
Entry<Integer,String> th = it.next();
System.out.println(th);
}
}
}
运行结果
迭代器遍历
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class ItertorTest01 {
public static void main(String[] args) {
// 各集合的迭代器 遍历
List<Stu> list = new ArrayList<Stu>();
list.add(new Stu("张三1", 1));
list.add(new Stu("张三2", 2));
list.add(new Stu("张三3", 3));
System.out.println(list);
for (Stu stu : list) {
System.out.println(stu);
}
System.out.println();
// 迭代器遍历
Iterator<Stu> it = list.iterator();
while (it.hasNext()) {
Stu stu = it.next();
System.out.println(stu);
}
// 测试map集合的迭代器遍历
Map<Integer, Stu> m = new HashMap<Integer, Stu>();
m.put(1, new Stu("张益达1", 101));
m.put(2, new Stu("Snake", 102));
m.put(3, new Stu("张大炮", 103));
System.out.println(m);
System.out.println();
Set<Entry<Integer, Stu>> set = m.entrySet();
for (Entry<Integer, Stu> e : set) {
System.out.println(e);
}
System.out.println();
// 迭代器
Set<Entry<Integer, Stu>> setEntry = m.entrySet();
Iterator<Entry<Integer, Stu>> that = setEntry.iterator();
while (that.hasNext()) {
Entry<Integer, Stu> th = that.next();
System.out.println(th);
}
}
}
class Stu {
private String name;
private int id;
public Stu() {
// TODO Auto-generated constructor stub
}
public Stu(String name, int id) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Stu [name=" + name + ", id=" + id + "]";
}
}
Map例题
利用Map,完成下面的功能:
从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。
如果该年没有举办世界杯,则输出:没有举办世界杯。
附:世界杯冠军以及对应的夺冠年份,请参考下图。
截止到2009 年为止,历届世界杯冠军
在原有世界杯Map 的基础上,增加如下功能:
读入一支球队的名字,输出该球队夺冠的年份列表。
例如,读入“巴西”,应当输出
1958 1962 1970 1994 2002
读入“荷兰”,应当输出没有获得过世界杯
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class Test01 {
public static void main(String[]args){
Map<Integer,Country> map = new HashMap<Integer,Country>();
map.put(1, new Country("1930","乌拉圭"));
map.put(2, new Country("1934","意大利"));
map.put(3, new Country("1938","意大利"));
map.put(4, new Country("1950","乌拉圭"));
map.put(5, new Country("1954","德国"));
map.put(6, new Country("1958","巴西"));
map.put(7, new Country("1962","巴西"));
map.put(8, new Country("1966","英格兰"));
map.put(9, new Country("1970","巴西"));
map.put(10, new Country("1974","德国"));
map.put(11, new Country("1978","阿根廷"));
map.put(12, new Country("1982","意大利"));
map.put(13, new Country("1986","阿根廷"));
map.put(14, new Country("1990","德国"));
map.put(15, new Country("1994","巴西"));
map.put(16, new Country("1998","法国"));
map.put(17, new Country("2002","巴西"));
map.put(18, new Country("2006","意大利"));
Scanner sc = new Scanner(System.in);
@SuppressWarnings("unused")
Map1 m = new Map1();
System.out.println("请输入一个年份:");
String time = sc.next();
Map1.judgeYear(map,time);
System.out.println("请输入国家名");
String nation = sc.next();
Map1.judgeCountry(map,nation);
sc.close();
}
}
class Map1{
//判断输入年份
public static void judgeYear(Map<Integer,Country> map,String time) {
boolean exit = false; //定义输入年份是否举办世界杯
Set<Entry<Integer, Country>> set = map.entrySet();
for (Entry<Integer, Country> entry : set) {
if(entry.getValue().getYear().equals(time)){
System.out.println(entry.getValue().getCountry());
exit = true;
}
}
//若不存在输入年份,则输出该年份没有举办世界杯
if(exit == false){
System.out.println(time+"年没有举办世界杯!");
}
}
public static void judgeCountry(Map<Integer,Country> map,String nation) {
boolean exit = false; //定义输入国家是否举办世界杯
Set<Entry<Integer, Country>> set = map.entrySet();
for (Entry<Integer, Country> entry : set) {
if(entry.getValue().getCountry().equals(nation)){
System.out.println(entry.getValue().getYear());
exit = true;
}
}
//若不存在输入国家,则输出该国家没有举办过世界杯
if(exit == false){
System.out.println(nation+"没有举办过世界杯!");
}
}
}
/**
* 国家类
* @author my
*
*/
class Country{
private String year;
private String country;
public Country(){}
public Country(String year, String country) {
super();
this.year = year;
this.country = country;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
运行结果