Java-Collections工具类测试
来源:互联网 发布:java ee ide是什么 编辑:程序博客网 时间:2024/05/16 14:15
package collection;import java.util.*;public class CollectionsTest { //通过Collections工具类中的sort方法对Integer泛型的List排序 public void testSort1() { List<Integer> integerList = new ArrayList<Integer>(); Random random = new Random(); Integer k; for(int i = 0;i < 10;i++) { //通过nestInt()添加十个100内的不重复整数 k = random.nextInt(100); if(integerList.contains(k) == false) { integerList.add(k); } System.out.println("成功添加整数:"+k); } 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); } } //通过Collections的sort方法对String泛型的List排序 public void testSort2() { List<String> stringList = new ArrayList<String>(); Random random = new Random(); //生成十条长度为10以内的随机字符串 String numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; for(int i = 0;i < 10;i++) { int num = random.nextInt(10); char[] ch = new char[num]; int k; for(int j = 0;j < num;j++) { k = random.nextInt(62); ch[j] = numbersAndLetters.charAt(k); } //将字符数组转化为字符串 StringBuffer sb = new StringBuffer(); for(int m = 0;m < ch.length;m++) { sb.append(ch[m]); } String newStr = sb.toString(); if(stringList.contains(newStr) == false && newStr.length() > 0) { stringList.add(newStr); } else { i--; } } 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); } } //对Student类型泛型的List排序 //Comparable接口:默认比较规则 Comparator接口:临时比较规则 对于一个对象不存在默认比较规则 public void testSort3() { List<Student> studentList = new ArrayList<Student>(); Random random = new Random(); //生成三个1000内的随机不重复ID int k = 0; int[] arr = new int[3]; while(k < 3) { boolean flag = true; int rd = random.nextInt(1000); for(int i = 0;i < k; i++) { if(arr[i]==rd) { flag = false; } } if(flag) { arr[k] = rd; k++; } } studentList.add(new Student(arr[0]+"","Mike")); studentList.add(new Student(arr[1]+"","Lucy")); studentList.add(new Student(arr[2]+"","Jack")); studentList.add(new Student(10000+"","Tom")); System.out.println("--------------排序前-------------"); for(Student student:studentList) { System.out.println("学生:"+student.id+":"+student.name); } Collections.sort(studentList);//要在Student类中实现Comparable接口 System.out.println("--------------按ID排序后-------------"); for(Student student:studentList) { System.out.println("学生:"+student.id+":"+student.name); } Collections.sort(studentList,new StudentComparator());//重写comparator中的compare方法 System.out.println("--------------按姓名排序后-------------"); for(Student student:studentList) { System.out.println("学生:"+student.id+":"+student.name); } } public static void main(String[] args) { CollectionsTest ct = new CollectionsTest(); //ct.testSort1(); //ct.testSort2(); ct.testSort3(); }}
package collection;import java.util.Comparator;public class StudentComparator implements Comparator<Student> { @Override //o1大于o2 返回正数 public int compare(Student o1, Student o2) { return o1.name.compareTo(o2.name); }}
package collection;import java.util.*;//学生类public class Student implements Comparable<Student> { public String id; public String name; public Set<Course> courses; @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; } public Student(String id,String name) { this.id = id; this.name = name; this.courses = new HashSet<Course>(); } //按学生ID排序 @Override public int compareTo(Student o) { // 相等返回0 this对象较大返回正值 反之返回负值 return this.id.compareTo(o.id); }}
0 0
- Java-Collections工具类测试
- Java Collections工具类
- Java Collections工具类
- java Collections工具类
- Java Collections工具类
- java Collections集合工具的测试
- 关于java Collections工具类
- Java 集合工具类 Collections
- Java 集合工具类Collections
- java集合(工具类Collections)
- java Collections集合工具类
- java的collections工具类
- Java.util.Collections工具类
- Java 集合工具类 Collections
- Java--Collections集合工具类
- JAVA-Properties类和Collections工具类
- Java类集 _集合工具类:Collections
- Java类集 _集合工具类:Collections
- Mac中显示和隐藏隐藏文件命令行
- JAVA自动拆装箱
- JavaBean
- Android 自定义漂亮的圆形进度条
- 设计模式:简单工厂、工厂方法、抽象工厂之小结与区别
- Java-Collections工具类测试
- git fork: upstream update
- js传真实地址 C:\fakepath
- UGUI系统学习之三种渲染模式Render Mode
- Android 开发小提示集合
- Spring MVC 定时器(http://声明要加上)
- vs 2008 显式实现接口 和 隐式实现接口的 区别
- idea 构建多个项目
- 整理iOS常用的第三方框架