Collections类解析 红太狼 2022-04-11 07:19 166阅读 0赞 # 最常用的排序: # ## 需要实现Comparable接口 ## # 1.什么是Comparable接口 # 此接口强行对实现它的每个类的对象进行整体排序。此排序被称为该类的自然排序 ,类的 compareTo 方法被称为它的自然比较方法 。实现此接口的对象列表(和数组)可以通过 Collections.sort (和 Arrays.sort )进行自动排序。实现此接口的对象可以用作有序映射表中的键或有序集合中的元素,无需指定比较器。 强烈推荐(虽然不是必需的)使自然排序与 equals 一致。所谓与equals一致是指对于类 C 的每一个 e1 和 e2 来说,当且仅当 (e1.compareTo((Object)e2) == 0) 与e1.equals((Object)e2) 具有相同的布尔值时,类 C 的自然排序才叫做与 equals 一致 。 # 代码: # ## 负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。 ## class Employee implements Comparable<Employee> { public Employee(String n, double s) { name = n; salary = s; Random ID = new Random(); id = ID.nextInt( 10000000 ); } public int getId() { return id; } public String getName() { return name; } public double getSalary() { return salary; } public void raiseSalary( double byPercent) { double raise = salary *byPercent/ 100 ; salary+=raise; } public int compareTo(Employee other) { if (id<other.id) //这里比较的是什么 sort方法实现的就是按照此比较的东西从小到大排列 return - 1 ; if (id>other.id) return 1 ; return 0 ; } private int id; private String name; private double salary; } ## 排序(Sort)使用sort方法可以根据元素的自然顺序 对指定列表按升序进行排序。列表中的所有元素都必须实现 Comparable接口。此列表内的所有元素都必须是使用指定比较器可相互比较的 ## # 2)混排(Shuffling) # 混排算法所做的正好与 sort 相反: 它打乱在一个 List 中可能有的任何排列的踪迹。也就是说,基于随机源的输入重排该 List,这样的排列具有相同的可能性(假设随机源是公正的)。 Collections.Shuffling(list) double array[] = {112, 111, 23, 456, 231 }; for (int i = 0; i < array.length; i++) { list.add(new Double(array[i])); } Collections.shuffle(list); for (int i = 0; i < array.length; i++) { System.out.println(li.get(i)); } //结果:112,111,23,456,231 # 3) 反转(Reverse) # 使用Reverse方法可以根据元素的自然顺序 对指定列表按降序进行排序。 Collections.reverse(list) double array[] = {112, 111, 23, 456, 231 }; for (int i = 0; i < array.length; i++) { list.add(new Double(array[i])); } Collections. reverse (list); for (int i = 0; i < array.length; i++) { System.out.println(li.get(i)); } //结果:231,456,23,111,112 # 4) 替换所以的元素(Fill) # 使用指定元素替换指定列表中的所有元素。 String str[] = {"dd","aa","bb","cc","ee"}; for(int j=0;j<str.length;j++){ li.add(new String(str[j])); } Collections.fill(li,"aaa"); for (int i = 0; i < li.size(); i++) { System.out.println("list[" + i + "]=" + li.get(i)); } //结果:aaa,aaa,aaa,aaa,aaa # 5) 拷贝(Copy) # 用两个参数,一个目标 List 和一个源 List, 将源的元素拷贝到目标,并覆盖它的内容。目标 List至少与源一样长。如果它更长,则在目标 List 中的剩余元素不受影响。 Collections.copy(list,li): 后面一个参数是目标列表 ,前一个是源列表 double array[] = {112, 111, 23, 456, 231 }; List list = new ArrayList(); List li = new ArrayList(); for (int i = 0; i < array.length; i++) { list.add(new Double(array[i])); } double arr[] = {1131,333}; String str[] = {"dd","aa","bb","cc","ee"}; for(int j=0;j<arr.length;j++){ li.add(new Double(arr[j])); } Collections.copy(list,li); for (int i = 0; i <list.size(); i++) { System.out.println("list[" + i + "]=" + list.get(i)); } //结果:1131,333,23,456,231 # 6) 返回Collections中最小元素(min) # 根据指定比较器产生的顺序,返回给定 collection 的最小元素。collection中的所有元素都必须是通过指定比较器可相互比较的 Collections.min(list) double array[] = {112, 111, 23, 456, 231 }; List list = new ArrayList(); for (int i = 0; i < array.length; i++) { list.add(new Double(array[i])); } Collections.min(list); for (int i = 0; i <list.size(); i++) { System.out.println("list[" + i + "]=" + list.get(i)); } //结果:23 # 7) 返回Collections中最大元素(max) # 根据指定比较器产生的顺序,返回给定 collection 的最大元素。collection中的所有元素都必须是通过指定比较器可相互比较的 Collections.max(list) double array[] = {112, 111, 23, 456, 231 }; List list = new ArrayList(); for (int i = 0; i < array.length; i++) { list.add(new Double(array[i])); } Collections.max(list); for (int i = 0; i <list.size(); i++) { System.out.println("list[" + i + "]=" + list.get(i)); } //结果:456 # 8) lastIndexOfSubList # 返回指定源列表中最后一次出现指定目标列表的起始位置 int count = Collections.lastIndexOfSubList(list,li); double array[] = {112, 111, 23, 456, 231 }; List list = new ArrayList(); List li = new ArrayList(); for (int i = 0; i < array.length; i++) { list.add(new Double(array[i])); } double arr[] = {111}; String str[] = {"dd","aa","bb","cc","ee"}; for(int j=0;j<arr.length;j++){ li.add(new Double(arr[j])); } Int locations = Collections. lastIndexOfSubList (list,li); System.out.println(“===”+ locations); //结果 3 # 9) IndexOfSubList # 返回指定源列表中第一次出现指定目标列表的起始位置 int count = Collections.indexOfSubList(list,li); double array[] = {112, 111, 23, 456, 231 }; List list = new ArrayList(); List li = new ArrayList(); for (int i = 0; i < array.length; i++) { list.add(new Double(array[i])); } double arr[] = {111}; String str[] = {"dd","aa","bb","cc","ee"}; for(int j=0;j<arr.length;j++){ li.add(new Double(arr[j])); } Int locations = Collections.indexOfSubList(list,li); System.out.println(“===”+ locations); //结果 1 # 10) Rotate # 根据指定的距离循环移动指定列表中的元素 Collections.rotate(list,-1); 如果是负数,则正向移动,正数则方向移动 double array[] = {112, 111, 23, 456, 231 }; List list = new ArrayList(); for (int i = 0; i < array.length; i++) { list.add(new Double(array[i])); } Collections.rotate(list,-1); for (int i = 0; i <list.size(); i++) { System.out.println("list[" + i + "]=" + list.get(i)); } //结果:111,23,456,231,112 参考: [http://blog.csdn.net/lskyne/article/details/8961014][http_blog.csdn.net_lskyne_article_details_8961014] [http://www.cnblogs.com/gnuhpc/archive/2012/12/17/2822251.html][http_www.cnblogs.com_gnuhpc_archive_2012_12_17_2822251.html] # # [http_blog.csdn.net_lskyne_article_details_8961014]: http://blog.csdn.net/lskyne/article/details/8961014 [http_www.cnblogs.com_gnuhpc_archive_2012_12_17_2822251.html]: http://www.cnblogs.com/gnuhpc/archive/2012/12/17/2822251.html
还没有评论,来说两句吧...