JAVA Collections工具类
一、Collections工具类两种sort()方法
二、示例
一、Collections工具类两种sort()方法
格式一: public static
说明:该方法中的泛型
覆写equals()和compareTo()方法。其中如String类、Integer类都是Comparable接口子类,可以进行排序,而基本类型不能进行sort排序。比较项目在类内指定
格式二:public static
说明:该方法中指定比较方式Comparator super T> c,即c必须实现Comparator super T>接口,覆写compareTo()方法指定比较项目。比较项目在类外指定,比较灵活
二、示例
示例中获取字符串和数字的公用方法:
/** * 生成随机 不重复的字符串 : number 生成字符串个数 */ public static ListgenerateString(int number) { List listString = new ArrayList(); // 用于存放返回值 List length = null; // 字符串长度 StringBuffer sb = new StringBuffer(); // 中间变量 int control = 0; // 控制个数 String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; while (true) { // 控制结束 if ( control==number ) { break; } // 生成随机数,生成36位的2aaab761-4341-4968-aceb-3861ee3824b2 UUID类型数据 String uuid = UUID.randomUUID().toString().replace("-", ""); sb.setLength(0); // 获得随机字符串长度,长度不为0 do { length = getDiffNo(1, 11); } while ( length.get(0)==0 ); // 拼凑字符串 for (int i=0; i getDiffNo(int n, int max) { // 生成 [0-n] 个不重复的随机数 // list 用来保存这些随机数 List list = new ArrayList(); Random random = new Random(); Integer k; for (int i=0; i 1、对Integer泛型的List进行排序
/** * 1.通过Collections.sort()方法,对Integer泛型的List进行排序; * 创建一个Integer泛型的List,插入十个100以内的不重复随机整数, 调用Collections.sort()方法对其进行排序 * 2.排序规则:先数字后字母,数字0-9,字母A-Z a-z的顺序 */ public void listIntegerSort() { // 插入十个100以内的不重复随机整数 ListintegerList = getDiffNo(10, 100); System.out.println("-------------排序前--------------"); for (Integer integer : integerList) { System.out.println("元素:" + integer); } Collections.sort(integerList); System.out.println("----------------排序后-------------------"); for (Integer integer : integerList) { System.out.println("元素:" + integer); } } 2、对String泛型的List进行排序
/** * 1.对String泛型的List进行排序; 创建String泛型的List,添加乱序的String元素, * 调用sort方法,再次输出排序后的顺序 */ public void listStringSort() { ListstringList = new ArrayList (); stringList.add("eipJlcx"); stringList.add("WvQRufC"); stringList.add("J"); stringList.add("HdaU2G"); stringList.add("M0WswHD3"); System.out.println("------------排序前-------------"); for (String string : stringList) { System.out.println("元素:" + string); } Collections.sort(stringList); System.out.println("--------------排序后---------------"); for (String string : stringList) { System.out.println("元素:" + string); } } /** * 对String泛型的List进行排序,要求随机生成10个的不重复字符串,字符串的长度在10以内 */ public void listStringRandomSort() { // 生成随机字符串 ListlistString = generateString(10); System.out.println("--------------排序前---------------"); for (String integer : listString) { System.out.println("元素:" + integer); } // 排序 Collections.sort(listString); System.out.println("----------------排序后------------------"); for (String integer : listString) { System.out.println("元素:" + integer); } } 3、对其他类型泛型的List进行排序
Course类实现
/** * 课程类 * @author Administrator * */ public class Course { public String id; public String name; public Course(String id, String name) { this.id = id ; this.name = name; } public Course() { } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Course)) return false; Course other = (Course) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }Student类实现Comparable接口,类内设置比较项
import java.util.HashSet; import java.util.Set; /** * 学生类 * @author Administrator * */ public class Student implements Comparable{ public String id; public String name; public Set courses; public Student(String id, String name) { this.id = id; this.name = name; this.courses = new HashSet (); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Student)) return false; Student other = (Student) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } @Override public int compareTo(Student o) { // 设置ID为比较项 // TODO Auto-generated method stub return this.id.compareTo(o.id); } } 实现Comparator接口,类外设置比较项
import java.util.Comparator; public class StudentComparator implements Comparator{ @Override public int compare(Student o1, Student o2) { // TODO Auto-generated method stub return o1.name.compareTo(o2.name); } } 比较Student类
/** * 对其他类型泛型的List进行排序,以Student为例。 */ public void listComparatorSort() { ListstudentList = new ArrayList (); List list = getDiffNo(4, 1000); studentList.add(new Student(list.get(0) + "", "Mike")); studentList.add(new Student(list.get(1) + "", "Angela")); studentList.add(new Student(list.get(2) + "", "Lucy")); studentList.add(new Student(1000 + "", "Beyonce")); System.out.println("--------------排序前---------------"); for (Student student : studentList) { System.out.println("学生:" + student.id + ":" + student.name); } // 实现Comparator 接口,设置ID比较方式 Collections.sort(studentList); System.out.println("----------------按照ID排序后------------------"); for (Student student : studentList) { System.out.println("学生:" + student.id + ":" + student.name); } // 实现Comparator 接口,设置特定比较方式,以name比较排序 Collections.sort(studentList, new StudentComparator()); System.out.println("----------------按照姓名排序后-----------------"); for (Student student : studentList) { System.out.println("学生:" + student.id + ":" + student.name); } } 参考学习连接:
Comparable接口的实现和使用:http://www.cnblogs.com/gnuhpc/archive/2012/12/17/2822251.html
Collections.sort对List排序的两种方法:http://blog.csdn.net/wxx614817/article/details/50628197
下一篇: 初识android中的动画