Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能;如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f,g这样,当然数字也是这样的。
compare(a,b)方法:根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。
equals(obj)方法:仅当指定的对象也是一个 Comparator,并且强行实施与此 Comparator 相同的排序时才返回 true。
Collections.sort(list, new PriceComparator());的第二个参数返回一个int型的值,就相当于一个标志,告诉sort方法按什么顺序来对list进行排序。
具体实现代码方法如下:
Book实体类:
- package com.tjcyjd.comparator;
-
- import java.text.DecimalFormat;
- import java.text.SimpleDateFormat;
- import java.util.GregorianCalendar;
- import java.util.Iterator;
- import java.util.TreeMap;
-
-
-
-
-
-
-
- public class Book implements Comparable {
- public int id;
- public String name;
- public double price;
- private String author;
- public GregorianCalendar calendar;
-
- public Book() {
- this(0, "X", 0.0, new GregorianCalendar(), "");
- }
-
- public Book(int id, String name, double price, GregorianCalendar calender,
- String author) {
- this.id = id;
- this.name = name;
- this.price = price;
- this.calendar = calender;
- this.author = author;
- }
-
-
- public String toString() {
- String showStr = id + "\t" + name;
- DecimalFormat formatPrice = new DecimalFormat("0.00");
- showStr += "\t" + formatPrice.format(price);
- showStr += "\t" + author;
- SimpleDateFormat formatDate = new SimpleDateFormat("yyyy年MM月dd日");
- showStr += "\t" + formatDate.format(calendar.getTime());
- return showStr;
- }
-
- public int compareTo(Object obj) {
- Book b = (Book) obj;
- return this.id - b.id;
- }
-
- public static void main(String[] args) {
- Book b1 = new Book(10000, "红楼梦", 150.86, new GregorianCalendar(2009,
- 01, 25), "曹雪芹、高鄂");
- Book b2 = new Book(10001, "三国演义", 99.68, new GregorianCalendar(2008, 7,
- 8), "罗贯中 ");
- Book b3 = new Book(10002, "水浒传", 100.8, new GregorianCalendar(2009, 6,
- 28), "施耐庵 ");
- Book b4 = new Book(10003, "西游记", 120.8, new GregorianCalendar(2011, 6,
- 8), "吴承恩");
- Book b5 = new Book(10004, "天龙八部", 10.4, new GregorianCalendar(2011, 9,
- 23), "搜狐");
- TreeMap tm = new TreeMap();
- tm.put(b1, new Integer(255));
- tm.put(b2, new Integer(122));
- tm.put(b3, new Integer(688));
- tm.put(b4, new Integer(453));
- tm.put(b5, new Integer(40));
- Iterator it = tm.keySet().iterator();
- Object key = null, value = null;
- Book bb = null;
- while (it.hasNext()) {
- key = it.next();
- bb = (Book) key;
- value = tm.get(key);
- System.out.println(bb.toString() + "\t库存:" + tm.get(key));
- }
- }
- }
自定义比较器和测试类:
- package com.tjcyjd.comparator;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.GregorianCalendar;
- import java.util.Iterator;
- import java.util.List;
-
- public class UseComparator {
- public static void main(String args[]) {
- List<Book> list = new ArrayList<Book>();
- Book b1 = new Book(10000, "红楼梦", 150.86, new GregorianCalendar(2009,
- 01, 25), "曹雪芹、高鄂");
- Book b2 = new Book(10001, "三国演义", 99.68, new GregorianCalendar(2008, 7,
- 8), "罗贯中 ");
- Book b3 = new Book(10002, "水浒传", 100.8, new GregorianCalendar(2009, 6,
- 28), "施耐庵 ");
- Book b4 = new Book(10003, "西游记", 120.8, new GregorianCalendar(2011, 6,
- 8), "吴承恩");
- Book b5 = new Book(10004, "天龙八部", 10.4, new GregorianCalendar(2011, 9,
- 23), "搜狐");
- list.add(b1);
- list.add(b2);
- list.add(b3);
- list.add(b4);
- list.add(b5);
-
- System.out.println("数组序列中的元素:");
- myprint(list);
- Collections.sort(list, new PriceComparator());
- System.out.println("按书的价格排序:");
- myprint(list);
- Collections.sort(list, new CalendarComparator());
- System.out.println("按书的出版时间排序:");
- myprint(list);
- }
-
-
- public static void myprint(List<Book> list) {
- Iterator it = list.iterator();
- while (it.hasNext()) {
- System.out.println("\t" + it.next());
- }
- }
-
-
- static class PriceComparator implements Comparator {
- public int compare(Object object1, Object object2) {
- Book p1 = (Book) object1;
- Book p2 = (Book) object2;
- return new Double(p1.price).compareTo(new Double(p2.price));
- }
- }
-
-
- static class CalendarComparator implements Comparator {
- public int compare(Object object1, Object object2) {
- Book p1 = (Book) object1;
- Book p2 = (Book) object2;
- return p2.calendar.compareTo(p1.calendar);
- }
- }
- }
Java.util.Collections类下有一个静态的shuffle()方法,如下:
1)static void shuffle(List<?> list) 使用默认随机源对列表进行置换,所有置换发生的可能性都是大致相等的。
2)static void shuffle(List<?> list, Random rand) 使用指定的随机源对指定列表进行置换,所有置换发生的可能性都是大致相等的,假定随机源是公平的。
通俗一点的说,就像洗牌一样,随机打乱原来的顺序。
注意:如果给定一个整型数组,用Arrays.asList()方法将其转化为一个集合类,有两种途径:
1)用List<Integer> list=ArrayList(Arrays.asList(ia)),用shuffle()打乱不会改变底层数组的顺序。
2)用List<Integer> list=Arrays.aslist(ia),然后用shuffle()打乱会改变底层数组的顺序。代码例子如下:
- package ahu;
- import java.util.*;
-
- public class Modify {
- public static void main(String[] args){
- Random rand=new Random(47);
- Integer[] ia={0,1,2,3,4,5,6,7,8,9};
- List<Integer> list=new ArrayList<Integer>(Arrays.asList(ia));
- System.out.println("Before shufflig: "+list);
- Collections.shuffle(list,rand);
- System.out.println("After shuffling: "+list);
- System.out.println("array: "+Arrays.toString(ia));
- List<Integer> list1=Arrays.asList(ia);
- System.out.println("Before shuffling: "+list1);
- Collections.shuffle(list1,rand);
- System.out.println("After shuffling: "+list1);
- System.out.println("array: "+Arrays.toString(ia));
-
- }
- }
运行结果如下:
- Before shufflig: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- After shuffling: [3, 5, 2, 0, 7, 6, 1, 4, 9, 8]
- array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- Before shuffling: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- After shuffling: [8, 0, 5, 2, 6, 1, 4, 9, 3, 7]
- array: [8, 0, 5, 2, 6, 1, 4, 9, 3, 7]
在第一种情况中,Arrays.asList()的输出被传递给了ArrayList()的构造器,这将创建一个引用ia的元素的ArrayList,因此打乱这些引用不会修改该数组。 但是,如果直接使用Arrays.asList(ia)的结果, 这种打乱就会修改ia的顺序。意识到Arrays.asList()产生的List对象会使用底层数组作为其物理实现是很重要的。 只要你执行的操作 会修改这个List,并且你不想原来的数组被修改,那么你就应该在另一个容器中创建一个副本。