import java.util.*;
public class TodaysHomework{
public static void main(String[] args){
//keySet() values() entrySet() Map.Entry
//getKey() getValue() setValue()
//size() remove() Iterator
//完整的练习 Map集合的操作和字符串的处理 以及流程控制的内容
TreeMap<Student,String> map = new TreeMap<Student,String>(new QQB());
Student s1 = new Student("昊昊",27);
Student s2 = new Student("磊磊",24);
Student s3 = new Student("龙龙",24);
Student s4 = new Student("晖晖",27);
Student s5 = new Student("超超",25);
Student s6 = new Student("超超",22);
Student s7 = new Student("嵩嵩",27);
map.put(s1,"0531-66532154");
map.put(s2,"0531-66666661");
map.put(s3,"0532-88888888");
map.put(s4,"0538-6678111");
map.put(s5,"0531-55555511");
map.put(s6,"0531-55555522");
map.put(s7,"0538-3434596");
//1st.打印当前通讯录总共收录了多少个联系人 0分
System.out.println(map.size());
//2nd.晖晖有个人生重要的考试 请假三天 删除联系人 两种不同的方法实现
/*
{
Set<Student> ks = map.keySet();
for(Iterator<Student> car = ks.iterator(); car.hasNext(); ){
Student x = car.next();
if("晖晖".equals(x.name)){
car.remove();//TestTreeSet5 我们应该尽量保证compareTo能够返回0 否则删不掉 删不掉就要学会用迭代器
}
}
}
*/
/*
{
Set<Map.Entry<Student,String>> es = map.entrySet();
for(Iterator<Map.Entry<Student,String>> car = es.iterator(); car.hasNext(); ){
Map.Entry<Student,String> e = car.next();
Student k = e.getKey();
if("晖晖".equals(k.name)){
car.remove();
}
}
}
*/
//3rd.嵩嵩过生日了 已经28岁了 修改收录的年龄信息 一种做法
/*
{
Map<Student,String> temp = new TreeMap<>(new QQB());
Set<Map.Entry<Student,String>> es = map.entrySet();
for(Iterator<Map.Entry<Student,String>> car = es.iterator(); car.hasNext(); ){
Map.Entry<Student,String> e = car.next();
Student k = e.getKey();
String v = e.getValue();
if("嵩嵩".equals(k.name)){
car.remove();
k.age = 28;
temp.put(k,v);
}
}
map.putAll(temp);
}
*/
//4th.泰安市发展迅速 电话号码升位 开头+8 0538
/*
{
Set<Map.Entry<Student,String>> es = map.entrySet();
for(Map.Entry<Student,String> e : es){
String v = e.getValue();
if(v.startsWith("0538-")){
v = v.replace("0538-","0538-8");
e.setValue(v);
}
}
}
*/
//5th.请显示所有居住在济南的学生姓名
/*
{
for(Map.Entry<Student,String> e : map.entrySet()){
Student k = e.getKey();
String v = e.getValue();
if(v.startsWith("0531-")){
System.out.println(k.name);
}
}
}
*/
//6th.打印所有同学所在的城市
//这是最重要的一个题目 它有极强的教育意义
//它在教育每一个程序员 什么情况下 主动使用Map集合
{
Map<String,String> cac = new HashMap<>();
cac.put("0531","济南");
cac.put("0532","青岛");
cac.put("0538","泰安");
cac.put("010","北京");
for(Map.Entry<Student,String> e : map.entrySet()){
Student k = e.getKey();
String v = e.getValue();
String code = v.substring(0,v.indexOf("-"));
String city = cac.get(code);
System.out.println(k.name + ":" + city);
}
}
//7th.所有年龄低于25岁的同学 改用昵称 小X
/*
{
Map<Student,String> temp = new TreeMap<>(new QQB());
Set<Map.Entry<Student,String>> es = map.entrySet();
for(Iterator<Map.Entry<Student,String>> car = es.iterator(); car.hasNext(); ){
Map.Entry<Student,String> e = car.next();
Student k = e.getKey();
String v = e.getValue();
if(k.age < 25){
car.remove();
k.name = "小" + k.name.substring(0,1);
temp.put(k,v);
}
}
map.putAll(temp);
}
*/
}
}
//请脱离开Student类 制定Student类的比较规则
//这个规则要求 先按名字排序 名字相同按照年龄
//名字年龄都相同 则按照先来后到的顺序
//考察的是TestTreeSet4 - 如何借助多个属性综合形成排序规则
class QQB implements Comparator<Student>{
@Override
public int compare(Student s1,Student s2){
if(!(s1.name.equals(s2.name)))
return s1.name.compareTo(s2.name);
if(s1.age != s2.age)
return s1.age - s2.age;
return 1;
}
}
class Student{
String name;
int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
@Override
public String toString(){
return name+":"+age;
}
}
集合题
猜你喜欢
转载自blog.csdn.net/qq_43011881/article/details/87913275
今日推荐
周排行