当前位置:网站首页>On CompareTo method in string
On CompareTo method in string
2022-07-29 10:58:00 【51CTO】
I read an article today gitchat The article , The title is Chat Java String Source sorting algorithm , From which I have some insights and thoughts , So I'm going to sum up what I gained in the process of watching
One ,java.lang.Comparable Interface
Comparable The interface forces the sorting of the list of class objects . The order is called natural order , Its compareTo Method , It's called natural comparison
If you use this Represents the current call to this compareTo Object of method ,obj Is the method passing in parameters
be :
Comparable Interface compareTo It is an internal comparison , That is, it supports comparison with the current object
Two ,java.util.Comparator Interface
Comparator It can be considered as an external comparator , An object does not support comparing itself with itself ( It didn't come true Comparable Interface ), But I want to compare the two objects
Comparative logic
3、 ... and , Chat string Medium compareTo Method
String What is realized in is Comparable Interface for String Object to make comparison logic
Let's start with an example
result :
Combined with the above example String Chinese vs compareTo Method implementation
So you can see from the source code above ,string Medium compareTo Logic can be roughly sorted into
- Each character in the front of the string is exactly the same , return : The length difference between the last two strings ;
- Each character in the front of the string is different , return : Different characters appear ASCII Code difference .
- Every character in a string is exactly the same , return 0;
stay String There is also a static inner class CaseInsensitiveComparator This interface is also implemented
The rewritten interface method is String Object case insensitive comparison method
边栏推荐
- Kunlunbase instruction manual (II) best practices for peer-to-peer deployment
- Software testing dry goods
- Applied practical skills of deep reinforcement learning
- AI model risk assessment Part 2: core content
- Use R-Pack skimr to collect the beautiful display of President measurement
- 开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
- 聊聊性能测试环境搭建
- IPV6基础
- 『面试知识集锦100篇』1.面试技巧篇丨HR的小心思,你真的懂吗?
- golang 实现文件上传下载
猜你喜欢

8. Interleave - understand ThreadPoolExecutor thread pool from architecture design to practice

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码

Basic construction of QT project

IPv6 Foundation

StarRocks 技术内幕:实时更新与极速查询如何兼得

PaddleLite 编译以及代码跑通复盘

Alibaba P8 broke out this interview guide for big factories. After reading it, the salary soared by 30K!

VMware: use commands to update or upgrade VMware esxi hosts

KRYSTAL:审计数据中基于知识图的战术攻击发现框架

如何使用 grep 跨多行查找模式匹配
随机推荐
美团、饿了么被杭州市监约谈要求落实食品安全管理责任 严禁恶意竞争
Zhou Hongyi: 360 is the largest secure big data company in the world
自采集在线电脑壁纸php源码v2.0自适应端
SkiaSharp 之 WPF 自绘 弹动小球(案例版)
重磅 | 开放原子校源行活动正式启动
基本.分块
golang 实现文件上传下载
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
基于flask实现的mall商城---用户模块
Error: Protobuf syntax version should be first thing in file
Determine whether the values of two objects are equal
LeetCode二叉树系列——144.二叉树的前序遍历
Site data collection -scrapy usage notes
一文搞懂什么是二叉树(二叉树的种类、遍历方式、定义)
Regular expression matching URL
北京大学公开课重磅来袭!欢迎走进「AI for Science」课堂
Learning R language these ebooks are enough!
开源峰会抢先看 | 7 月 29 日分论坛 & 活动议程速览
Detailed arrangement of JVM knowledge points (long text warning)
TCP和UDP