String常用方法、toString()、equals()、compareTo()

1、String常用方法
   charAt(int index) 返回指定索引的 char值
   compareTo(String anotherString) 比较两个字符串的字典,返回int类型 
   compareToIgnoreCase(String str) 按字典顺序比较两个字符串,忽略大小写的差异
   concat(String str) 返回新的字符串,将指定的字符串拼接此字符串,此字符串结尾
   endsWith(String suffix) 判断这个字符串是否已以指定的后缀结束
   indexOf(String str) 返回此字符串的指定子字符串中第一个出现的索引位置
   indexOf(String str, int fromIndex) 返回此字符串的指定子字符串中第一个出现在索引中的位置,从指定索引处开始
   length() 返回此字符串的长度
   subSequence(int beginIndex, int endIndex) 返回一个字符序列的子序列。 
   toCharArray() 将此字符串转换为一个新的字符数组
   valueOf(boolean b) 返回的 boolean参数的字符串表示形式

2、toString()
   1、所有类间接或直接继承Object,所以每个类都有toString()
   2、Object的toString()方法:
   public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
   }
   Println()中的参数如果没有(显示调用)调用toString(),
   只要是引用类型(除了String类型、基本类型形式(int char)、基本类型的引用数组类型(char[] int[]等))都是在Println()方法中默认调用toString()
   3、什么时候重写toString()
   当打印输出一个引用,想输出自己想要的信息的时候,一般要重写toString()
   public class Student{
    public String  name;
    public int age,id;

    public Student (String  name,int age,int id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }
    //重写toString ()方法
    public String  toString () {
        return "name:"+name+"age:"+age+"id:"+id;
    }
   }

   public class Test{
    Student s = new Student ("张三",18,16101);
    System.out.println(s);
   }
   运行:
   name:张三age:18id:16101
3、equals()
   1、String类型有自己的equals(),比较的是两个字符串的内容是否相同
   2、Object的equals():
   public boolean equals(Object obj) {
        return (this == obj);
   }
   实际上只是用“==”比较,“==”比较是地址和内容
   3、什么时候重写equals()
   当我们要判断两个对象的内容是否相等时(忽略地址),一般需要重写equals()方法
   4、重写equals(),一定要重写HashCode()
   根据一个类的equals方法(改写后),两个截然不同的实例有可能在逻辑上是相等的,但是,根据Object.hashCode方法,它们仅仅是两个对象。因此,违反了“相等的对象必须具有相等的散列码”
   public class Student{
    public String  name;
    public int age,id;

    public Student (String  name,int age,int id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }
    //重写hashCode()
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    //重写equals()
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (age != other.age)
            return false;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
   }

   public class Test{
    Student s1 = new Student ("张三",18,16101);
    Student s2 = new Student ("张三",18,16101);
    System.out.println(s1.equals(s2));
   }
   运行:
   true
4、compareTo()
   定义:比较此对象与指定对象的顺序。
   返回:负整数、零或正整数。如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。
   1、重写compareTo()需要先实现Comparable接口
   Comparable接口:
   public interface Comparable<T>{
    int compareTo(T t);
   }
   2、什么时候重写compareTo()
   按字母排序,按数字排序,年代排序等等某种**定制排序**
   TreeSet集合的参数类型为基本数据类型TreeSet是默认按照元素从小到大排序
   public class treeSet {
    public static void main(String[] args) {
        Set<Integer> t = new TreeSet<Integer>();

        t.add(10);t.add(2);t.add(5);t.add(16);t.add(11);

        for (Integer in : t) {
            System.out.print(in);
        }
    }

   }
   运行:
   2    5    10    11    16

   TreeSet集合的参数类型为自定义类型时,需要实现Comparable接口,并重写compareTo()
   public class Student implements Comparable<Student>{
    String  name;
    int age,id;
    public Student (String  name,int age,int id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }
    @Override
    public String  toString () {
        return "name:"+name+"age:"+age+"id:"+id;
    }
    //重写compareTo方法,按Student的id从小到大排序
    @Override
    public int compareTo(Student s) {
        if(this.id>s.id)
            return 1;
        else if(this.id==s.id)
            return 0;
        else
            return -1;
    }
   }

   public class Test03 {

    public static void main(String[] args) {
        Student s1 = new Student ("zhang1",18,16101);
        Student s2 = new Student ("zhang2",28,16102);
        Student s3 = new Student ("zhang3",38,16103);
        
        Set<Student> st = new TreeSet<Student>();
        st.add(s2);st.add(s3);st.add(s1);
        for (Student student : st) {
            System.out.println(student);
        }
    }

   }
   运行:
   name:zhang1age:18id:16101
   name:zhang2age:28id:16102
   name:zhang3age:38id:16103

原文地址:https://www.cnblogs.com/snzd9958/p/9498375.html