Collections工具类
来源:互联网 发布:单片机电子琴实验报告 编辑:程序博客网 时间:2024/06/14 14:20
import java.util.*;class CollectionsDemo {public static void main(String[] args) {List<String> lt=new ArrayList<String>();lt.add("a");lt.add("bb");lt.add("zzzzz");lt.add("qqq");lt.add("ccc");Collections.sort(lt,new LtSort());System.out.println(lt);System.out.println(Collections.max(lt,new LtSort()));}}class LtSort implements Comparator<String>{public int compare(String s1,String s2){if(s1.length()>s2.length())return 1;if(s1.length()<s2.length())return -1;return s1.compareTo(s2);}}
zzzz
z
import java.util.*;class Demo3 {public static void main(String[] args) {ArrayList<Integer> al=new ArrayList<Integer>();al.add(0);al.add(-8);al.add(-8);al.add(9);al.add(5);al.add(4);sop(al);Collections.reverse(al);//反转sop(al);Collections.sort(al);//升序排序,也可以在sort中引用比较器 按指定规则排序sop(al);}public static void sop(Object o){System.out.println(o);}}
其他操作
import java.util.*;class Demo {public static void main(String[] args) {List<String> lt=new ArrayList<String>();lt.add("a");lt.add("bb");lt.add("bb");lt.add("zzzzz");lt.add("qqq");lt.add("ccc");Collections.sort(lt,new LtSort());sop(lt);sop(Collections.reverse(lt));//反转lt中的元素sop("a所在的位置: "+Collections.binarySearch(lt,"a"));//用二分法查找"a"所在的位置,当出现负号时,表示的是插入点的位置(-x-1)sop("bb出现的次数:"+Collections.frequency(lt,"bb"));//查找次数Collections.replaceAll(lt,"a","z");//将所有"a"替换sop(lt);fillDemo(lt,"yi",0,4);//替换一部分元素}public static void fillDemo(List<String> list,String s,int star,int end){List<String> slist=list.subList(star,end);Collections.fill(slist,s);sop(list);}public static void sop(Object o){System.out.println(o);}}class LtSort implements Comparator<String>{public int compare(String s1,String s2){if(s1.length()>s2.length())return 1;if(s1.length()<s2.length())return -1;return s1.compareTo(s2);}}reverseOrder
import java.util.*;class Demo2 {public static void main(String[] args) {Set<String> st = new TreeSet<String>(Collections.reverseOrder(new lenComparator()));//reverseOrder()强行逆转st.add("a");st.add("jj");st.add("zzz");st.add("yyyy");Iterator it = st.iterator();while(it.hasNext()){System.out.println(it.next());}}}class lenComparator implements Comparator<String>{public int compare(String s1,String s2){if(s1.length()>s2.length())return 1;if(s1.length()<s2.length())return -1;return s1.compareTo(s2);}}
阅读全文
0 0
- 工具类:Collections 、Arrays
- Java Collections工具类
- 集合工具类Collections
- Collections工具类示例
- 集合工具类Collections
- Java Collections工具类
- Collections集合工具类
- 集合工具类 Collections
- Collections工具类
- Collections工具类
- 20170519@Collections工具类
- Collections工具类
- Collections工具类
- Collections工具类
- Collections工具类
- java Collections工具类
- Java Collections工具类
- Collections工具类
- awk 里面的字符串要用双引号,awk的参数用单引号
- 深度学习: softmax loss 计算
- 常用混淆Rxjava等
- LUA 排序算法和性能分析[4]:快速排序算法
- Stanford机器学习---第十讲. 数据降维
- Collections工具类
- RxJava学习1---创建
- 随机森森示例1:iris
- Relational Learning Task
- JS学习笔记
- 深入分析 ThreadLocal 内存泄漏问题
- 关于nuc970 lcd的分析
- Linux进程间通信 —— 信号(上)
- Linux 目录中的etc是什么缩写