TreeMap练习:对学生对象的姓名进行升序排序
2015-04-20 21:06
731 查看
package map;
import java.util.*;
public class MapDemo4 {
/**
* @TreeMap练习
*
* 对学生对象的姓名进行升序排序
*
* 因为数据是以键值对形式存在的。
* 所以要使用可以排序的Map集合。TreeMap。
*
*
*/
public static void main(String[] args) {
TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());
tm.put(new Student("blisi3",23),"nanjing");
tm.put(new Student("lisi1",21),"beijing");
tm.put(new Student("lisi1",21),"tianjing");
tm.put(new Student("alisi4",24),"wuhan");
tm.put(new Student("lisi2",22),"shanghai");
Set<Map.Entry<Student, String>> entrySet = tm.entrySet();
Iterator<Map.Entry<Student, String>> it = entrySet.iterator();
while(it.hasNext()){
Map.Entry<Student, String> me = it.next();
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu+"..."+addr);
}
}
}
class StuNameComparator implements Comparator<Student>{
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;
}
}
import java.util.*;
public class MapDemo4 {
/**
* @TreeMap练习
*
* 对学生对象的姓名进行升序排序
*
* 因为数据是以键值对形式存在的。
* 所以要使用可以排序的Map集合。TreeMap。
*
*
*/
public static void main(String[] args) {
TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());
tm.put(new Student("blisi3",23),"nanjing");
tm.put(new Student("lisi1",21),"beijing");
tm.put(new Student("lisi1",21),"tianjing");
tm.put(new Student("alisi4",24),"wuhan");
tm.put(new Student("lisi2",22),"shanghai");
Set<Map.Entry<Student, String>> entrySet = tm.entrySet();
Iterator<Map.Entry<Student, String>> it = entrySet.iterator();
while(it.hasNext()){
Map.Entry<Student, String> me = it.next();
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu+"..."+addr);
}
}
}
class StuNameComparator implements Comparator<Student>{
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;
}
}
相关文章推荐
- 【java编程】Map集合之TreeMap按学生姓名进行升序排序
- Java 使用TreeMap对学生对象的年龄进行升序排序。
- 要求从用户输入的多行文本中提取学生的姓名、学号及登录日期,并封装到 Student 类中作为类的私有属性。创建一个Student类型的对象数组,对学号进行 升序排序并输出。
- 要求从用户输入的多行文本中提取学生的姓名、学号及登录日期,并封装到 Student 类中作为类的私有属性。创建一个 Student 类型的对象数组,对学号进行 升序排序并输出
- 集合练习。学生信息包括学号、姓名、出生日期、性别。把N个学生的信息放入一个集合中。可以根据学号,对学生信息进行检索。并可以根据生日进行排序输出。
- 有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- 链表的带参数排序,可以通过姓名、成绩进行升序或降序操作
- 1.给出一个Person类里面包含姓名、年龄、成绩,声明5个Person对象数组,要求对数组中的内容进行排序,排序规则如下:按成绩由高到低排序,如果成绩一样,按年龄由高到低排序。
- 将对象数组按照每一组对象的key值大小进行字典顺序(ASCII值大小)升序排序
- 根据对象中某一属性进行升序降序排序的javascript实现
- 对数组中的对象进行升序以及降序的排序方法(其中对象类型是系统类型).
- java排序练习:输入三个学生的姓名和他们的三个评分,去掉最高分和最低分
- java中使用TreeMap进行中文姓名排序
- 用C语言编写一个双链表,每个结点存储学生的编号以及姓名,遍历这个双链表,并将学号按照从小到大进行排序
- TreeMap集合的应用根据学生的年龄或者姓名排序
- 有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- 有 4000 5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- 对数组中的对象进行升序以及降序的排序方法(其中对象类型是系统类型).
- 用面向对象解决:输入一个5X5的二维数组,将数组进行排序,其中一维数组按照平均值降序,一维数组内部升序排列
- Java 往TreeSet集合中存储自定义对象学生,按照学生的年龄进行排序。