集合框架-工具类-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 ...
随机推荐
- 鱼书_第一章_Python入门
Python版本 Python有Python 2.x和Python 3.x两个版本.两个版本不兼容,可能出现用Python 3.x编的代码不能被Python 2.x执行的情况. Python安装 An ...
- ubuntu ffmpeg 转码错误
[aac @ 0xde2400] The encoder 'aac' is experimental but experimental codecs are not enabled, add '-st ...
- 【LeetCode】490. The Maze 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】171. Excel Sheet Column Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 Java解法 Python解法 日期 [LeetCode] 题 ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【剑指Offer】二进制中1的个数 解题报告(Python)
题目地址:https://www.nowcoder.com/ta/coding-interviews 题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 解题方法 这个题如果使 ...
- Mod Tree(hdu2815)
Mod Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 洛谷 P1434 [SHOI2002]滑雪(DP,记忆化搜索)
题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一个区域中最长 ...
- Kronecker Products and Stack Operator
目录 定义 Stack Operator Kronecker Product 性质 Stack Operator Kronecker Product 半线性 Whitcomb L. Notes on ...
- Java高级程序设计笔记 • 【第5章 XML解析】
全部章节 >>>> 本章目录 5.1 XML 文档概述 5.1.1 XML文档结构 5.1.1 XML结构说明 5.1.1 XML文档元素 5.1.2 XML文档语法规范 ...