集合框架-工具类-Collections-逆序替换

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 import java.util.TreeSet;
8
9 import cn.itcast.p2.comparator.ComparatorByLength;
10
11 public class CollectionsDemo {
12
13 public static void main(String[] args) {
14 // TODO Auto-generated method stub
15 /*
16 * Collections:是集合框架的工具类。
17 * 里面的方法都是静态的。
18 */
19 // demo_1();//排序
20 // demo_2();//折半 最值
21 // demo_3();//逆序
22 demo_4();//替换
23
24 }
25
26
27
28 private static void demo_4() {
29 // TODO Auto-generated method stub
30 List<String> list = new ArrayList<String>();
31
32 list.add("abcde");
33 list.add("cba");
34 list.add("zhangsan");
35 list.add("zhaoliu");
36 list.add("xiaoqiang");
37
38 System.out.println(list);
39 // Collections.replaceAll(list, "cba", "nba");//replaceAll相当于set(indexOf"cba","nba");
40 // Collections.fill(list, "cc");//fill一次性将集合的所有值替换或重新初始化一次
41 Collections.shuffle(list);//随机将元素安在任意位置
42 System.out.println(list);
43 }
44
45 private static void demo_3() {
46 // TODO Auto-generated method stub
47
48 // TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder());//原理就是下面这个方法
49 /* TreeSet<String> ts = new TreeSet<String>(new Comparator<String>() {
50 public int compare(String o1, String o2) {
51 int temp = o2.compareTo(o1);
52 return temp;
53 }
54 });
55 */ //自己实现
56
57 //reverseOrder(Comparator<T> cmp),将一个已有比较器逆转
58 TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder(new ComparatorByLength()));
59 ts.add("abc");
60 ts.add("hahaha");
61 ts.add("zzz");
62 ts.add("aa");
63 ts.add("cba");
64
65 System.out.println(ts);
66 }
67
68 private static void demo_2() {
69 // TODO Auto-generated method stub
70 List<String> list = new ArrayList<String>();
71
72 list.add("abcde");
73 list.add("cba");
74 list.add("aa");
75 list.add("zzz");
76 list.add("cba");
77 list.add("nbaa");
78 //折半要先排序
79 Collections.sort(list);
80 System.out.println(list);
81
82 int index = Collections.binarySearch(list, "aaa");
83
84 System.out.println("index="+index);
85
86 //获取最大值。
87 // String max = Collections.max(list);//max=zzz
88 String max = Collections.max(list,new ComparatorByLength());
89 System.out.println("max="+max);
90 }
91
92 public static void demo_1() {
93
94 List<String> list = new ArrayList<String>();
95
96 list.add("abcde");
97 list.add("cba");
98 list.add("aa");
99 list.add("zzz");
100 list.add("nbaa");
101 System.out.println(list);
102
103
104
105 //对list集合进行指定顺序的排序。
106 // Collections.sort(list);
107 // mySort(list);
108 // mySort(list, new ComparatorByLength());
109 System.out.println(list);
110
111
112 }
113 //下面方法相当于Collections.sort(list,new ComparatorByLength);
114 /*
115 public static <T> void mySort(List<T> list,Comparator<? super T> comp) {
116
117 for (int i = 0; i < list.size()-1; i++) {
118 for (int j = i+1; j < list.size(); j++) {
119 if (comp.compare(list.get(i),list.get(j)) >0) {
120 // T temp = list.get(i);
121 // list.set(i, list.get(j));
122 // list.set(j, temp);
123 Collections.swap(list, i, j);
124 }
125 }
126 }
127 }
128 //介绍Collections.swap交换方法
129 /*
130 public static <T extends Comparable<? super T>> void mySort(List<T> list) {
131 for (int i = 0; i < list.size()-1; i++) {
132 for (int j = i+1; j < list.size(); j++) {
133 if (list.get(i).compareTo(list.get(j))> 0 ) {
134 // T temp = list.get(i);
135 // list.set(i, list.get(j));
136 // list.set(j, temp);
137 Collections.swap(list, i, j);
138 }
139 }
140 }
141 }*/
142
143 //相当于按自然顺序方法升序排列Collections.sort
144 //public static <T extends Comparable<? super T>> void sort(List<T> list)
145 /*
146 public static <T extends Comparable<? super T>> void mySort(List<T> list) {
147 for (int i = 0; i < list.size()-1; i++) {
148 for (int j = i+1; j < list.size(); j++) {
149 if (list.get(i).compareTo(list.get(j))> 0 ) {
150 T temp = list.get(i);
151 list.set(i, list.get(j));
152 list.set(j, temp);
153 }
154 }
155 }
156 }*/
157
158 //传入String类型的集合
159 /* public static void mySort(List<String> list) {
160
161 for (int i = 0; i < list.size()-1; i++) {
162 for (int j = i+1; j < list.size(); j++) {
163 if (list.get(i).compareTo(list.get(j))> 0 ) {
164 String temp = list.get(i);
165 list.set(i, list.get(j));
166 list.set(j, temp);
167 }
168 }
169 }
170 }*/
171
172 }
CollectionsDemo
集合框架-工具类-Collections-逆序替换的更多相关文章
- java基础37 集合框架工具类Collections和数组操作工具类Arrays
一.集合框架工具类:Collections 1.1.Collections类的特点 该工具类中所有的方法都是静态的 1.2.Collections类的常用方法 binarySearch(List< ...
- JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)
package com.itcast.test20140113; import java.util.ArrayList; import java.util.Arrays; import java.ut ...
- java集合框架工具类Collections,集合的操作
1 import java.util.*; public class asList { public static void main(String args[]) { // int arr[] = ...
- 集合框架工具类--Collections排序
package ToolCollectionsDemo; import java.util.ArrayList; import java.util.Collections; import java.u ...
- 操作集合的工具类Collections
1 操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操 ...
- java之操作集合的工具类--Collections
Collections是一个操作Set.List和Map等集合的工具类. Collections中提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了对集合对象设置不可变.对集合对象实现同步控 ...
- Java-集合第六篇操作集合的工具类Collections
1.Java提供了一个操作Set.List.Map等集合的工具类:Collections. 工具类中提供的方法主要针对Set.List.Map的排序.查询.修改等操作,以及将集合对象设置为不可变.对集 ...
- Java基础---泛型、集合框架工具类:collections和Arrays
第一讲 泛型(Generic) 一.概述 1.JDK1.5版本以后出现的新特性.用于解决安全问题,是一个类型安全机制. 2.JDK1.5的集合类希望在定义集合时,明确表明你要向集合中装入那种类 ...
- 集合框架-工具类-Collections-排序
1 package cn.itcast.p2.toolclass.collections.demo; 2 3 import java.util.ArrayList; 4 import java.uti ...
随机推荐
- Swagger请求报错:TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body. 如下图 ...
- vc++ 调用winapi调节屏幕亮度(增加win7代码demo)
1.关于 代码是通过测试的,测试环境: win7 + MFC 为什么要发在这里? 区别于上一篇随笔. MD排版更顺眼 demo 会放到 这里 更正了上一篇随笔中的代码错误 2.头文件 #include ...
- 【LeetCode】930. Binary Subarrays With Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 字典 相似题目 参考资料 日期 题目地址: ...
- 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- Hive SQL优化思路
Hive的优化主要分为:配置优化.SQL语句优化.任务优化等方案.其中在开发过程中主要涉及到的可能是SQL优化这块. 优化的核心思想是: 减少数据量(例如分区.列剪裁) 避免数据倾斜(例如加参数.Ke ...
- 【Java例题】3.4求a+aa+aaa+aaaa+... ...+aa...a(n个
4. package chapter3; import java.util.*; public class demo4 { public static void main(String[] args) ...
- Optimization Landscape and Expressivity of DeepCNNs
目录 引 主要内容 基本的一些定义 卷积层 全连接层 池化层 改写卷积层 假设2.4 引理2.5 假设3.1 假设3.2 引理3.3 定理3.4 定理3.5 推论3.6 假设4.1 引理4.2 引理4 ...
- <数据结构>XDOJ.322关键路径长度
问题与解答 问题描述 计算AOE-网中关键路径的长度. 输入格式 输入数据第一行是一个正整数,表示图中的顶点个数n(顶点将分别按0,1,-,n-1进行编号),顶点数不超过100,其中0为源点,n-1为 ...
- 制作登录页面,点击键盘的 Enter 键或者单击“登录”按钮,验证用户输入的邮箱和密码是否正确
查看本章节 查看作业目录 需求说明: 制作登录页面 点击键盘的 Enter 键或者单击"登录"按钮,验证用户输入的邮箱和密码是否正确 实现思路: 准备登录的静态页面 在页面中嵌入脚 ...