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. 【九度OJ】题目1195:最长&最短文本 解题报告

    [九度OJ]题目1195:最长&最短文本 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1195 题目描述: 输入多行字符串, ...

  2. 【LeetCode】115. Distinct Subsequences 解题报告(Python)

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

  3. C#反射调用 异常信息:Ambiguous match found.

    异常信息(异常类型:System.Reflection.AmbiguousMatchException)异常提示:Ambiguous match found.异常信息:Ambiguous match ...

  4. java 堆、栈

    堆: 1)Java的堆是一个运行时数据区,类的对象从堆中分配空间.这些对象通过new等指令建立,通过垃圾回收器来销毁. 2)堆的优势是可以动态地分配内存空间,需要多少内存空间不必事先告诉编译器,因为它 ...

  5. 【LeetCode】剑指 Offer 04. 二维数组中的查找

    二维数组查找:线性查找法 有二维数组: [  [1,   4,  7, 11, 15],  [2,   5,  8, 12, 19],  [3,   6,  9, 16, 22],  [10, 13, ...

  6. 第十个知识点:RSA和强RSA问题有什么区别?

    第十个知识点:RSA和强RSA问题有什么区别 这个密码学52件事数学知识的第一篇,也是整个系列的第10篇.这篇介绍了RSA问题和Strong-RSA问题,指出了这两种问题的不同之处. 密码学严重依赖于 ...

  7. Pydantic使用

    Pydantic可以在代码运行时提供类型提示, 数据校验失败时提供友好的错误提示, 使用Python的类型注解来进行数据校验和settings管理 一般使用 from datetime import ...

  8. The Limitations of Deep Learning in Adversarial Settings

    目录 概 主要内容 alg2, alg3 一些有趣的实验指标 Hardness measure Adversarial distance Nicolas Papernot, Patrick McDan ...

  9. CS5216PIN TO PIN替换PS8402A方案|PS8402A电路设计原理图|CS5216芯片

    PS8402A是HDMI 电平移位器/中继器专为2型双模Display Port(DP++)电缆适配器应用而设计.它设计用于Display Port到DVI或Display Port到HDMI的2型适 ...

  10. 编写Java程序,创建一个 Person 类,该类中有一个类成员变量 country、一个实例变量 name 和另一个实例变量 age。

    返回本章节 返回作业目录 需求说明: 创建一个 Person 类,该类中有一个类成员变量 country.一个实例变量 name 和另一个实例变量 age. country 表示地区,name 表示姓 ...