1 package cn.itcast.p2.toolclass.collections.demo;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.List;
7
8 import cn.itcast.p2.comparator.ComparatorByLength;
9
10 public class CollectionsDemo {
11
12 public static void main(String[] args) {
13 // TODO Auto-generated method stub
14 /*
15 * Collections:是集合框架的工具类。
16 * 里面的方法都是静态的。
17 */
18 demo_1();
19
20 }
21
22 public static void demo_1() {
23
24 List<String> list = new ArrayList<String>();
25
26 list.add("abcde");
27 list.add("cba");
28 list.add("aa");
29 list.add("zzz");
30 list.add("nbaa");
31 System.out.println(list);
32
33
34
35 //对list集合进行指定顺序的排序。
36 // Collections.sort(list);
37 // mySort(list);
38 // mySort(list, new ComparatorByLength());
39 System.out.println(list);
40
41
42 }
43 //下面方法相当于Collections.sort(list,new ComparatorByLength);
44 /*
45 public static <T> void mySort(List<T> list,Comparator<? super T> comp) {
46
47 for (int i = 0; i < list.size()-1; i++) {
48 for (int j = i+1; j < list.size(); j++) {
49 if (comp.compare(list.get(i),list.get(j)) >0) {
50 // T temp = list.get(i);
51 // list.set(i, list.get(j));
52 // list.set(j, temp);
53 Collections.swap(list, i, j);
54 }
55 }
56 }
57 }
58 //介绍Collections.swap交换方法
59 /*
60 public static <T extends Comparable<? super T>> void mySort(List<T> list) {
61 for (int i = 0; i < list.size()-1; i++) {
62 for (int j = i+1; j < list.size(); j++) {
63 if (list.get(i).compareTo(list.get(j))> 0 ) {
64 // T temp = list.get(i);
65 // list.set(i, list.get(j));
66 // list.set(j, temp);
67 Collections.swap(list, i, j);
68 }
69 }
70 }
71 }*/
72
73 //相当于按自然顺序方法升序排列Collections.sort
74 //public static <T extends Comparable<? super T>> void sort(List<T> list)
75 /*
76 public static <T extends Comparable<? super T>> void mySort(List<T> list) {
77 for (int i = 0; i < list.size()-1; i++) {
78 for (int j = i+1; j < list.size(); j++) {
79 if (list.get(i).compareTo(list.get(j))> 0 ) {
80 T temp = list.get(i);
81 list.set(i, list.get(j));
82 list.set(j, temp);
83 }
84 }
85 }
86 }*/
87
88 //传入String类型的集合
89 /* public static void mySort(List<String> list) {
90
91 for (int i = 0; i < list.size()-1; i++) {
92 for (int j = i+1; j < list.size(); j++) {
93 if (list.get(i).compareTo(list.get(j))> 0 ) {
94 String temp = list.get(i);
95 list.set(i, list.get(j));
96 list.set(j, temp);
97 }
98 }
99 }
100 }*/
101
102 }

CollectionsDemo

 1 package cn.itcast.p2.comparator;
2
3 import java.util.Comparator;
4
5 public class ComparatorByLength implements Comparator<String> {
6
7 @Override
8 public int compare(String o1, String o2) {
9 // TODO Auto-generated method stub
10 int temp = o1.length()-o2.length();
11
12 return temp==0?o1.compareTo(o2):temp;
13 }
14
15 }

ComparatorByLength

最新文章

  1. JS验证图片格式和大小并预览
  2. Thunderbird扩展
  3. JavaScript的内置对象和浏览器对象
  4. MySQL(三) —— 约束以及修改数据表
  5. Java之泛型练习
  6. Postman 安装 &amp; 资料
  7. ajax返回json数组遍历添加到html
  8. docker学习笔记(1)
  9. linux 树型显示文件 tree ls tree 命令
  10. StyleCop(C#代码检测工具)
  11. [转]Tomcat中的Session小结
  12. Linux设备驱动中的IO模型---阻塞和非阻塞IO【转】
  13. Linux内核分析-使用gdb跟踪调试内核从start_kernel到init进程启动
  14. xml的相关知识
  15. SQL2008数据库导出到SQL2000全部步骤过程
  16. lnmp环境一键搭建及卸载
  17. google chrome浏览器离线小恐龙游戏刷分bug
  18. 【CF1157F】Maximum Balanced Circle 求一个相邻元素之间绝对值为小于1的最大环
  19. PHP项目学习1
  20. Odoo中创建模块语句

热门文章

  1. JS验证身份证是否符合规则
  2. c++之升序和降序排序
  3. 【LeetCode】面试题 17.16. 按摩师 解题报告(C++)
  4. 【LeetCode】346. Moving Average from Data Stream 解题报告(C++)
  5. 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)
  6. 1370 - Bi-shoe and Phi-shoe
  7. DEV GridControl小结。。
  8. Python基础入门(8)- Python模块和包
  9. &lt;数据结构&gt;XDOJ317.输出完全二叉树的某一层
  10. IntelliJ IDEA 2019.3 代码提示忽略大小写(IDEA 2019版本如何设置代码提示不分大小写?)