List<String> 调用Collection.sort()整个流程的知识点
来源:互联网 发布:java 包装类 编辑:程序博客网 时间:2024/06/08 08:32
1.sort()本身:
Collection.sort()方法有两种形式,
格式一: public static
2.Comparator接口:
这个接口中最重要的方法实现就是compareTo(),这也是排序的关键,举个例子(String 中实现的compareTo()方法):
/* @param anotherString the <code>String</code> to be compared. * @return the value <code>0</code> if the argument string is equal to * this string; a value less than <code>0</code> if this string * is lexicographically less than the string argument; and a * value greater than <code>0</code> if this string is * lexicographically greater than the string argument. */ public int compareTo(String anotherString) { int len1 = value.length; int len2 = anotherString.value.length; int lim = Math.min(len1, len2); char v1[] = value; char v2[] = anotherString.value; int k = 0; while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; }
由源码可以看出:
首先取出两个字符串的长度,比较较小的长度内,两者是否相等。
若不相等,则直接返回该位置字符的ASCII码相减后的值。
若各位置都相等,则将两个字符串长度的差值返回。
ps:之前我一直把compareTo和Comparator搞混,其实很好区分Comparator是一个接口,compareTo()是它的实现方法;
3.List<String>排序有什么用:
主要是用来判断每个字符出现的次数,因为排序之后整个字符串会变得很规整,使用indexOf()和lastIndexOf()就能很快的算出一个字符在String中出现的次数。
阅读全文
0 0
- List<String> 调用Collection.sort()整个流程的知识点
- 关于List操作排序工具,collection.sort的原理
- list 的自定义排序 Collection.sort()方法个人学习总结
- C++调用python的函数的说明(整个调用流程)
- List<String>Sort
- 从NDK下载到JNI调用的整个流程
- Mybatis--查询collection--List<String>
- String中正则表达式的各种功能,集合Collection,list
- java collection.sort()根据时间排序list
- android collection.sort()根据时间排序list
- 用Collection.sort对List做排序
- Collection<String>n=new ArrayList<String>()与List<String>n=new ArrayList()的区别
- Java Collection类的知识点
- 使用JDBC的整个流程
- WinCE启动的整个流程
- 文件上传的整个流程
- SearchView搜索的整个流程
- 使用thinkphp的整个流程
- Linux监控系统磁盘使用比例,当使用率达到指定比例,删除指定目录下的所有文件的shell脚本
- Rhyme/算法 冒泡排序优化版
- 自顶向下,逐步求精
- 秒杀系统架构分析与实战
- caffe 创建网络模型
- List<String> 调用Collection.sort()整个流程的知识点
- 使用Phalcon+PHP-FPM构建PHP基于多进程的数据库连接池
- 半天学会 Swift
- 多线程
- python高级
- Only one JmsListenerAnnotationBeanPostProcessor may exist within the context错误解决
- cocoscreator获取和加载资源
- 请编写一个递归函数,用来输出n个元素的所有子集。例如,三个元素{a,b,c}的所有子集是:{},{a},{b},{c},{a,c},{ac},{b,c},{a,b,c}.
- Exception occurred during processing request: null 的错误原因以及解决办法