String忽略大小写方法compareToIgnoreCase源码及Comparator自定义比较器
代码语言:javascript复制//源码
public int compareToIgnoreCase(String str) {
return CASE_INSENSITIVE_ORDER.compare(this, str);
}
/**
* A Comparator that orders {@code String} objects as by
* {@code compareToIgnoreCase}. This comparator is serializable.
* <p>
* Note that this Comparator does <em>not</em> take locale into account,
* and will result in an unsatisfactory ordering for certain locales.
* The java.text package provides <em>Collators</em> to allow
* locale-sensitive ordering.
*
* @see java.text.Collator#compare(String, String)
* @since 1.2
*/
public static final Comparator<String> CASE_INSENSITIVE_ORDER
= new CaseInsensitiveComparator();
private static class CaseInsensitiveComparator
implements Comparator<String>, java.io.Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 8575799808933029326L;
public int compare(String s1, String s2) {
int n1 = s1.length();
int n2 = s2.length();
int min = Math.min(n1, n2);
for (int i = 0; i < min; i ) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(i);
if (c1 != c2) {
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {
c1 = Character.toLowerCase(c1);
c2 = Character.toLowerCase(c2);
if (c1 != c2) {
// No overflow because of numeric promotion
return c1 - c2;
}
}
}
}
return n1 - n2;
}
/** Replaces the de-serialized object. */
private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
}
对于设置的比较器的升序和降序问题的解释 public interface Comparator<T>{ int compare(T first, T second); } 通过计算两个字符串,如果第一个参数大于第二个参数,则返回一个正数,反之则返回一个负数
当返回结果为负数时,表示不需要交换两个参数的位置 当返回结果为正数时,表示需要交换两个参数的位置 而当数组进行排序时,从前往后传入两个参数,传入无非两种情况:
第一个参数大于第二个参数–>返回值为正数–>交换两个参数 第一个参数小于第二个参数–>返回值为负数–>不交换两个参数 我们会发现,这两种结果其实都指向一个相同的结论,就是执行完都会使得第一个参数小于第二个参数,故符合开始的结论:return first.a - second.a表示按升序排序
那么自然如果return second.a -first.a则表示按降序排序。
代码语言:javascript复制//DEMO
public class Student implements Comparable<Student>{
/**
* 姓名
*/
private String username;
/**
* 年龄
*/
private int age;
public Student() {
super();
}
public Student(String username, int age) {
this.username = username;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Student o) {
return this.age-o.age;//升序
}
}
package com.example.core.mydemo.json;
import com.alibaba.fastjson.JSON;
import com.example.core.mydemo.json.vo.Student;
import java.util.*;
import java.util.stream.Collectors;
public class ComparatorTest {
public static void main(String[] args) {
Student s1 = new Student("Aliming",8);
Student s2 = new Student("ajiaqi",9);
Student s3 = new Student("Bruixing",7);
Student s4 = new Student("bzeyi",7);
List<Student> list = new ArrayList();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
int size = list.size();
List<String> listTmp = list.stream().map(Student::getUsername).collect(Collectors.toList());
String [] arrayToSort = listTmp.toArray(new String[size]);
// Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER); //忽略大小写
Arrays.sort(arrayToSort); //默认是区分大小写
/**
* 区分大小写
* 排序1:Aliming
* 排序1:Bruixing
* 排序1:ajiaqi
* 排序1:bzeyi
*
*
* 忽略大小写
* 排序1:ajiaqi
* 排序1:Aliming
* 排序1:Bruixing
* 排序1:bzeyi
*
*/
for(int i = 0; i < size; i ) {
System.out.println(" 排序1:" arrayToSort[i]);
}
List<String> featureSort = listTmp.stream().sorted((a,b) -> (a.compareToIgnoreCase(b))).collect(Collectors.toList());
for(int i = 0; i < size; i ) {
System.out.println(" 排序2:" arrayToSort[i]);
}
Collections.sort(list, new Comparator<Student>() {//比较器
@Override
public int compare(Student o1, Student o2) {
// return o2.getAge()-o1.getAge();//以学生的年龄降序
return o1.getAge()-o2.getAge();//以学生的年龄升序
}
});
/**
* 排序3:{"age":7,"username":"Bruixing"}
* 排序3:{"age":7,"username":"bzeyi"}
* 排序3:{"age":8,"username":"Aliming"}
* 排序3:{"age":9,"username":"ajiaqi"}
*/
for (int i = 0; i < list.size(); i ) {
System.out.println("排序3:" JSON.toJSONString(list.get(i)));
}
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// 年龄降序
int result = o2.getAge()-o1.getAge();//年龄降序
if(result==0){//第一个规则判断完了 下一个规则 姓名的首字母 升序
result = o1.getUsername().charAt(0)-o2.getUsername().charAt(0);
}
return result;
}
});
/**
* 排序4:{"age":9,"username":"ajiaqi"}
* 排序4:{"age":8,"username":"Aliming"}
* 排序4:{"age":7,"username":"Bruixing"}
* 排序4:{"age":7,"username":"bzeyi"}
*/
for (int i = 0; i < list.size(); i ) {
System.out.println("排序4:" JSON.toJSONString(list.get(i)));
}
}
}