实用的好东西比较器
来源:互联网 发布:网络对人们的好处 编辑:程序博客网 时间:2024/05/21 06:38
把学生存入集合中,根据学生年龄从小到大排序
package cn.huaxin.test;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Iterator;import java.util.List;import java.util.TreeSet;public class Test2 implements Comparator<Student> { public int compare(Student o1, Student o2) { if (o1.age > o2.age) { return 1; } else if (o1.age < o2.age) { return -1; } else { return 0; } } public static void main(String[] args) { tes1(); TreeSet<Student> tree = new TreeSet(new Test2()); Student s1 = new Student(10,"小明"); tree.add(s1); Student s2 = new Student(5,"小红"); tree.add(s2); Student s3 = new Student(8,"小狗"); tree.add(s3); Student s4 = new Student(20,"小小"); tree.add(s4); Student s5 = new Student(200,"bu"); tree.add(s5); Student s6 = new Student(100,"aa"); tree.add(s6); Iterator<Student>it = tree.iterator(); while(it.hasNext()){ Student s = it.next(); System.out.println(s.name+"==年龄"+s.age); } } private static void tes1() { List<Student> list = new ArrayList<Student>(); Student s1 = new Student(10,"小"); Student s2 = new Student(5,"小明"); Student s3 = new Student(8,"小猫"); Student s4 = new Student(2,"小马"); list.add(s1); list.add(s2); list.add(s3); list.add(s4); Test2 com = new Test2(); Collections.sort(list, com); for(Student s : list){ System.out.println(); } }}
0 0
- 实用的好东西比较器
- Eclipse快捷键大全【转】 全+实用,绝对的好东西
- 想想还是用自己的博客写东西比较好
- vim 实用的东西
- 实用的东西
- 兼容性比较好且实用强大的弹出窗口效果
- 实用兼容性比较好的jQuery图片裁剪插件CropZoom
- 收集好的东西
- 大学生的好东西
- 转载的好东西
- 好的东西分享
- 转载的好东西
- 绝对的好东西
- 好用的东西
- 收藏的好东西
- 收藏的好东西
- 敝人分享一款好东西,对于学网站制作的同志们绝对实用
- 写些什么东西比较好呢?
- cocos2d-x cpp-test学习
- 在Ubuntu上用anaconda3安装opencv3
- 关于锚点和label的纯css内容切换
- HTML基本语法和语义
- 熄灯之后的学习——再读《MySQL必知必会》(3)
- 实用的好东西比较器
- 相邻区域染色最小染色数问题 POJ 1129
- JSP基本知识
- node.js项目改进之分离logic层确立数据中枢
- 产品一二天思考
- 委托和接口有什么区别?各用在什么场合?
- 网络协议栈设计(六)---链路层以太网代码实现(接收)
- Java的内部类
- 使用Navicat导入《MySQL必知必会》书上用例数据库的方法