public class TreeMapTest {
/**
* 每一个学生都有对应的归属地 学生Student,
* 地址:String 学生属性:姓名和年龄
* 注意:姓名和年龄相同的视为同一个学生 保证学生的唯一性
*
* 1,描述学生 2,定义map容器,将学生作为键,地址做为仁政,存入 3,获取map集合中的元素
*
* 对集合中的年龄进行升序排序
* 第一种让加入到集合中的元素身上具备比较功能
* 第二种给集合传递一个比较器。让集合具备特定的比较功能。
当元素也具备比较功能,集合也有比较器时。会以比较器为主
*/
public static void main(String[] args) {
TreeMap<Student, String> map = new TreeMap<Student, String>(new MyComp());
map.put(new Student("scg", 25), "吉安");
map.put(new Student("scg", 28), "梅县");
map.put(new Student("ldm", 21), "吉林长春");
map.put(new Student("zdf", 16), "合肥");
map.put(new Student("lmp", 24), "长化");
map.put(new Student("zdf", 24), "长化");
map.put(new Student("zdf", 16), "长化");
map.put(new Student("scf", 22), "太原");
// 第一种取出方式
// 得到所有的key的集合
Set<Student> keys = map.keySet();
Iterator<Student> ite = keys.iterator();
while (ite.hasNext()) {
// 返回迭代到的元素
Student s = ite.next();
// 得到key所对应的value
String values = map.get(s);
System.out.println("第一种取出方式:" + "姓名:" + s.getName() + "..."
+ "..年龄:" + s.getAge() + "..地址:" + values);
}
// // 第二种取出方式
// // 取出集合中的元素
// // 得到所有key和vlaue映射的集合
// Set<Map.Entry<Student, String>> sets = map.entrySet();
// // 获取迭代器
// Iterator<Map.Entry<Student, String>> itea = sets.iterator();
// while (itea.hasNext()) {
// Map.Entry<Student, String> mapInfo = itea.next();
// Student s = mapInfo.getKey();
// String sv = mapInfo.getValue();
// System.out.println("第二种取出方式:学生:姓名:" + s.getName() + "...年龄:"
// + s.getAge() + "...地址:" + sv);
// }
}
}
//让元素自身具备比较功能
//以年龄进行排序
//年龄相同时以姓名进行排序
class Student implements Comparable{
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Object obj){
if(!(obj instanceof Student))
throw new RuntimeException("参数类型错误");
Student s=(Student)obj;
if(this.age>s.age)
return 1;
if(this.age==s.age)
return this.name.compareTo(s.name);
return -1;
}
}
// 我们要自己创建比较器对象让集合对Student进行升序排序
//以年龄进行排序
//年龄相同时以姓名进行排序
/*class MyComp implements Comparator {
@Override
public int compare(Object obj1, Object obj2) {
if (!(obj1 instanceof Student && obj2 instanceof Student))
throw new RuntimeException("参数类型错误");
Student s1 = (Student) obj1;
Student s2 = (Student) obj2;
if (s1.getAge() > s2.getAge())
return 1;
if (s1.getAge() == s2.getAge()) { // 年龄若相同判断姓名是否相等
// 年龄和姓名相同才认为是同一个人
return s1.getName().compareTo(s2.getName());
}
return -1;
}
}*/
//代码优化版
class MyComp implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
int num=new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
if(num==0)
return s1.getName().compareTo(s2.getName());
return num;
}
}
/*//这里是按照姓名进行升序排序
class MyComp implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
int num=s1.getName().compareTo(s2.getName());
if(num==0)
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
return num;
}
}*/
《黑马程序员》 TreeMap练习分析
最新推荐文章于 2022-12-11 02:36:54 发布