Java SortedSet TreeSet按指定方式排序 不同方式排序
2011-11-05 23:12
567 查看
Java SortedSet TreeSet按指定方式排序 不同方式排序的列子
//实现Things类 class Things{ public String name; public String id; String location; public String arrivetime; Things(String id, String name,String location,String arrivetime){ this.id=id; this.name=name; this.location=location; this.arrivetime=arrivetime; } public String toString(){ return"id:"+id+"\tname:"+name+"\tlocation:"+location+"\tarrivetime:"+arrivetime+"\n"; } }
//实现按ID进行比较 import java.util.Comparator; public class SortById implements Comparator<Object>{ @Override public int compare(Object o1, Object o2) { Things th1=(Things)o1; Things th2=(Things)o2; return th1.id.compareTo(th2.id); } }
//实现按Time进行比较 import java.util.Comparator; public class SortByTime implements Comparator<Object>{ @Override public int compare(Object o1, Object o2) { Things th1=(Things)o1; Things th2=(Things)o2; return th1.arrivetime.compareTo(th2.arrivetime); } }
//测试 import java.util.*; class Demo5{ public static void main(String args[]) { SortedSet<Things> ssa=new TreeSet<Things>(new SortById()); //创建一个按照Id排序的TreeSet实例a SortedSet<Things> ssb=new TreeSet<Things>(new SortByTime()); //创建一个按照Time排序的TreeSet实例b ssa.add(new Things("001","A","库1","2001-01-10")); //添加的时候时间必须标准比如2011-11-1必须写成2011-11-01 ssa.add(new Things("002","A","库1","2011-10-31")); ssa.add(new Things("003","B","库1","2011-10-03")); ssa.add(new Things("006","A","库2","2011-10-01")); ssa.add(new Things("007","B","库2","2011-10-30")); ssa.add(new Things("008","B","库2","2011-09-12")); ssa.add(new Things("004","B","库1","2011-09-02")); ssa.add(new Things("005","A","库2","2001-01-09")); int count1=0,count2=0,count3=0,count4=0; System.out.println("按ID排序:"); Iterator<Things> it = ssa.iterator(); while(it.hasNext()){ Things c = it.next(); ssb.add(c); //从a中不断读取的时候就在b中添加 System.out.print(c); if(c.name=="A" && c.location=="库1") count1++; else if(c.name=="A" && c.location=="库2") count2++; else if(c.name=="B" && c.location=="库1") count3++; else if(c.name=="B" && c.location=="库2") count4++; } System.out.println("按时间排序:"); it = ssb.iterator(); while(it.hasNext()){ Things c = it.next(); System.out.print(c); } System.out.println("A物料在库1的库存量为:"+count1); System.out.println("A物料在库2的库存量为:"+count2); System.out.println("B物料在库1的库存量为:"+count3); System.out.println("B物料在库2的库存量为:"+count4); } }
相关文章推荐
- 浅谈java中的TreeSet中的排序方式
- java基础—自定义一个比较器,对TreeSet 集合中的元素按指定方法来排序(java集合六)
- Java TreeSet实现学生按年龄大小和姓名排序的方法示例
- java中自定义对象排序(TreeSet)
- Java基础15:treeset;排序方法-比较器;泛型;
- 浅谈TreeSet中的两种排序方式
- HashSet /LinkedHashSet/TreeSet排序的不同
- Java中使用TreeSet对章节序号排序
- My_Java之笔记(8)- 实现TreeSet集合的排序方法
- 浅谈对java中的hashset和Treeset 中重写equals()方法的不同及用法
- java treeset实现不重复存储的方式(在类对象中实例化CompareTo)
- 黑马程序员训练营---------TreeSet集合中对象进行排序的两种方式
- 【java编程】Collection类之TreeSet按字符串长度排序
- 黑马程序员对TreeSet集合中的元素排序的两种方式
- java list对元素进行指定多个字段属性按多种排序方式进行排序
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】
- java集合(TreeSet自然排序)
- 黑马程序员 TreeSet排序的两种实现方式
- Java容器-引用数据类型排序+TreeSet、TreeMap底层实现
- Java-集合排序(TreeSet)