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

集合框架-工具类-Collections-排序的更多相关文章

  1. 集合框架工具类--Collections排序

    package ToolCollectionsDemo; import java.util.ArrayList; import java.util.Collections; import java.u ...

  2. JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)

    package com.itcast.test20140113; import java.util.ArrayList; import java.util.Arrays; import java.ut ...

  3. java基础37 集合框架工具类Collections和数组操作工具类Arrays

    一.集合框架工具类:Collections 1.1.Collections类的特点 该工具类中所有的方法都是静态的 1.2.Collections类的常用方法 binarySearch(List< ...

  4. java集合框架工具类Collections,集合的操作

    1 import java.util.*; public class asList { public static void main(String args[]) { // int arr[] = ...

  5. 操作集合的工具类Collections

    1       操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操 ...

  6. java之操作集合的工具类--Collections

    Collections是一个操作Set.List和Map等集合的工具类. Collections中提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了对集合对象设置不可变.对集合对象实现同步控 ...

  7. Java-集合第六篇操作集合的工具类Collections

    1.Java提供了一个操作Set.List.Map等集合的工具类:Collections. 工具类中提供的方法主要针对Set.List.Map的排序.查询.修改等操作,以及将集合对象设置为不可变.对集 ...

  8. Java基础---泛型、集合框架工具类:collections和Arrays

    第一讲     泛型(Generic) 一.概述 1.JDK1.5版本以后出现的新特性.用于解决安全问题,是一个类型安全机制. 2.JDK1.5的集合类希望在定义集合时,明确表明你要向集合中装入那种类 ...

  9. Java 集合框架工具类

    Collections Arrays Collections 集合框架的工具类, 里面的方法都是静态的, 可以直接使用类名调用 常用方法 1. sort(List<T> list); // ...

  10. java集合框架——工具类

    一.概述 JAVA集合框架中有两个很重要的工具类,一个是Collections,另一个是Arrays.分别封装了对集合的操作方法和对数组的操作方法,这些操作方法使得程序员的开发更加高效. public ...

随机推荐

  1. JAVA火星坐标系、百度坐标系、84坐标系相互转换工具类

    /** * 坐标系转换工具类 */ public class PositionUtil { public static double pi = 3.1415926535897932384626; pu ...

  2. SpringBoot整合Light Security框架

    官方git地址:https://gitee.com/itmuch/light-security/tree/master 引入maven <dependency> <groupId&g ...

  3. App调试的几个命令实践

    1.logcat命令这个命令最简单常用,可查看帮助 第一种事例:adb logcat > /sdcard/mylogcat.txt 第二种事例:adb logcat > D:/Temp/1 ...

  4. 【LeetCode】186. Reverse Words in a String II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每个单词单独翻转+总的翻转 日期 题目地址:https ...

  5. 【LeetCode】694. Number of Distinct Islands 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  6. 【LeetCode】647. Palindromic Substrings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...

  7. LightOJ - 1003 Drunk

    One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...

  8. 第四十四个知识点:在ECC密码学方案中,描述一些基本的防御方法

    第四十四个知识点:在ECC密码学方案中,描述一些基本的防御方法 原文地址:http://bristolcrypto.blogspot.com/2015/08/52-things-number-44-d ...

  9. 深入理解Java虚拟机二:垃圾收集与内存分配

    垃圾收集:垃圾收集要完成三件事,包括哪些内存需要回收,什么时候回收及如何回收. 1.需要回收的内存判定:没有引用指向原先分配给某个对象的内存时,则该内存是需要回收的垃圾 Java垃圾收集器在对内存进行 ...

  10. [算法笔记-题解]问题 D: 习题4-4 三个整数求最大值

    问题 D: 习题4-4 三个整数求最大值 [命题人 : 外部导入] 时间限制 : 1.000 sec 内存限制 : 12 MB 题目描述 有3个整数a, b, c,由键盘输入,输出其中最大的数. 输入 ...