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. SpringBoot整合Light Security框架

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

  2. JAVA比较指定的两个日期

    判断指定日期是否在某个日期内 public static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); p ...

  3. 【LeetCode】1470. 重新排列数组 Shuffle the Array (Python)

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

  4. 【LeetCode】967. Numbers With Same Consecutive Differences 解题报告(Python & C++)

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

  5. CS5266 Type-C转HDMI+PD3.0+USB3.0 三合一拓展坞电路设计

    CS5266 Type-C转HDMI+PD3.0+USB3.0 三合一拓展坞电路设计 CS5266是一款带PD3.0快充 Type-C转HDMI 4K30HZ音视频转换芯片.CS5266支持PD3.0 ...

  6. Pytest_fixture(9)

    什么是fixture fixture是pytest特有的功能,使用装饰器 @pytest.fixture 标记的函数在其他函数中能被当作参数传入并被调用. fixture有明确的名字,在其他函数,模块 ...

  7. nuxt写路由接口

    //在server/interface/city.js import Router from 'koa-router'; const router = new Router({ prefix:'/ci ...

  8. C/C++避免头文件重复包含的方法

    C/C++避免头文件重复包含的方法 1. #ifndef 2. #pragma once 3. 混合使用 在实际的编程过程中,因为会使用多个文件,所以在文件中不可避免的要引入一些头文件,这样就可能会出 ...

  9. Python常用功能函数系列总结(七)

    本节目录 常用函数一:批量文件重命名 常用函数一:批量文件重命名 # -*- coding: utf-8 -*- """ DateTime : 2021/02/08 10 ...

  10. react 网址导航

    项目搭建 使用webpack.babel.react.antdesign配置单页面应用开发环境