本问介绍了collections工具类两种sort()方法,具体如下:
一、collections工具类两种sort()方法
格式一: public static > void sort(list list)
说明:该方法中的泛型都是comparable接口的子类,即只有是comparable接口子类类型的数据,才能进行比较排序。如果其他类型的数据要进行比较排序,必须继承comparable接口并
覆写equals()和compareto()方法。其中如string类、integer类都是comparable接口子类,可以进行排序,而基本类型不能进行sort排序。比较项目在类内指定
格式二:public static void sort(list list, comparator super t> c)
说明:该方法中指定比较方式comparator super t> c,即c必须实现comparator super t>接口,覆写compareto()方法指定比较项目。比较项目在类外指定,比较灵活
二、示例
示例中获取字符串和数字的公用方法:
/**
* 生成随机 不重复的字符串 : number 生成字符串个数
*/
public static list generatestring(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
string str = uuid.substring(i*3, (i*3+3));
//将str字符串转换为16进制,获得其值
int x = integer.parseint(str, 16);
//取余:x % 0x3e--0x3e = 3*16 + 14 = 62, 其中chars有62个字符
sb.append(chars[x % 0x3e]);
}
liststring.add(sb.tostring());
control++;
}
return liststring;
}
/**
* 生成随机不重复的数字 :n生成个数 max生成范围
*/
public static list getdiffno(int n, int max) {
// 生成 [0-n] 个不重复的随机数
// list 用来保存这些随机数
list list = new arraylist<>();
random random = new random();
integer k;
for (int i=0; i
do {
k = random.nextint(max);
} while (list.contains(k));
list.add(k);
}
return list;
}
1、对integer泛型的list进行排序
/**
* 1.通过collections.sort()方法,对integer泛型的list进行排序;
* 创建一个integer泛型的list,插入十个100以内的不重复随机整数, 调用collections.sort()方法对其进行排序
* 2.排序规则:先数字后字母,数字0-9,字母a-z a-z的顺序
*/
public void listintegersort() {
// 插入十个100以内的不重复随机整数
list integerlist = 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() {
list stringlist = 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() {
// 生成随机字符串
list liststring = 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() {
list studentlist = 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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!