Java的常用类之三----其他类(接口)

1、Comparable 或 Comparator

使用背景
Java中的对象,正常情况下,只能进行比较: == 或!= 。不能使用 > 或 < 的
但是在实际开发场景中,我们需要对多个对象进行排序,言外之意,就需要比较对象大小
如何实现?使用两个接口中的任何一个:Comparable 或 Comparator

1.1、Comparable接口的使用举例:自然排序

1.像String、包装类等实现了comparable接口,重写了compareTo()方法,给出了比较两个对象的大小

2.重写compareTo()方法的规则:
如果当前对象this大于形参对象obj,则返回正整数
如果当前对象this小于形参对象obj,则返回负整数
如果当前对象this等于形参对象obj,则返回0

3.像String、包装类重写了compareTo()以后,就会进行从小到大的排序

4.对于自定义类来说,如果需要排序,可以让自定义类实现Comparable接口,重写compareTo()方法 ,在compareTo()方法中指明如何排序

public class Goods implements Comparable {
    private String name;
    private double price;
    //省略了 getter setter 构造器  toString方法	
    //重写了compareTo()方法
     @Override
    public int compareTo(Object o) {
        if (o instanceof Goods) {
            Goods goods = (Goods) o;
            if (this.price > goods.price) {
                return 1;
            } else if (this.price < goods.price) {
                return -1;
            } else {
//                return 0;
                return -this.name.compareTo(goods.name);
            }
            //方法二
//           return Double.compare(this.price,goods.price)
        }
//        return 0;
        throw  new RuntimeException("传入的数据类型不一致");
    }
    }
   Goods[] arr = new Goods[5];
        arr[0] = new Goods("lenovoMouse", 46);
        arr[1] = new Goods("xiaomiMouse", 12);
        arr[2] = new Goods("dellMouse", 35);
        arr[3] = new Goods("huaweiMouse", 65);
        arr[4] = new Goods("MicrosoftMouse", 35);

        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
1.2、 Comparator接口的使用,定制排序

1.背景
当元素的类型没有实现java.lang.Comparable接口而又不方便修改代码
或者实现了java.lang.Comparable接口的排序规则不适合当前操作
那么可以考虑实现Comparator的对象来排序
2.重写compara(Object o1,Object o2)方法,比较o1和o2的大小
如果返回正整数,则表示o1大于o2
如果返回0,则表示相等
返回负整数,表示o1小于o

        String[] arr = new String[]{"AA", "CC", "JJ", "MM", "DD", "KK"};
       Arrays.sort(arr, new Comparator<String>() {
           //按照字符串从大到小的排序
           @Override
           public int compare(String o1, String o2) {
               if ( o1 instanceof String && o2 instanceof String){
                   String s1 = (String) o1;
                   String s2 = (String) o2;
                   return -s1.compareTo(s2);
               }
//                return 0;
               throw new RuntimeException("输入的数据类型不一致");
           }
       });
       Arrays.sort(arr);
       System.out.println(Arrays.toString(arr));

2、其他常用类的使用

2.1、System

① System中代表程序所在系统,提供了对应的一些系统属性信息,和系统操作。System类不能手动创建对象,因为构造方法被private修饰,阻止外界创建对象。System类中的都是static方法,类名访问即可。
② 常用方法
1.获取系统当前毫秒值(public static long currentTimeMillis())

获取当前系统时间与1970年01月01日00:00点之前的毫秒差值,我们可以用它来测试程序的执行时间

public static void main(String[] args) {
     long start = System.currentTimeMillis();
    for (int i=0; i<10000; i++) {
         System.out.println(i);
}
long end = System.currentTimeMillis();
System.out.println("共耗时毫秒:" + (end-start) );
}

2.结束正在运行的Java程序(public staitc void exit(int status))

参数传入一个数字即可。通常传入0记为正常状态,其它为异常状态。

3.垃圾回收器(public static void gc())

用来运行JVM中的垃圾回收器,完成内存中垃圾的清除。

4.确定当前的系统属性(public static getProperties getProperties() )
System.out.println(System.getProperties());

2、Math

① 在 Java 中 Math 类封装了常用的数学运算,提供了基本的数学操作,如指数、对数、平方根和三角函数等。Math 类位于 java.lang 包,它的构造方法是 private 的,因此无法创建 Math 类的对象,并且 Math 类中的所有方法都是类方法,可以直接通过类名来调用它们。

② 方法
Math.abs 求绝对值
Math.sin 正弦函数 Math.asin 反正弦函数
Math.cos 余弦函数 Math.acos 反余弦函数
Math.tan 正切函数 Math.atan 反正切函数 Math.atan2 商的反正切函数
Math.toDegrees 弧度转化为角度 Math.toRadians 角度转化为弧度
Math.ceil 得到不小于某数的最大整数
Math.floor 得到不大于某数的最大整数
Math.IEEEremainder 求余
Math.max 求两数中最大
Math.min 求两数中最小
Math.sqrt 求开方
Math.pow 求某数的任意次方, 抛出ArithmeticException处理溢出异常
Math.exp 求e的任意次方
Math.log10 以10为底的对数
Math.log 自然对数
Math.PI 记录的圆周率
Math.E 记录e的常量
Math中还有一些类似的常量,都是一些工程数学常用量。
Math.rint 求距离某数最近的整数(可能比某数大,也可能比它小)
Math.round 同上,返回int型或者long型(上一个函数返回double型)
Math.random 返回0,1之间的一个随机数

用法实例:
double s=Math.sqrt(5);
double x=Math.pow(2,4) //计算2的4次方

3、BigInteger 和 BigDecimal

该类的内容可以查看:https://blog.csdn.net/qq997404392/article/details/78085783
在Java的整数类型里面,byte为8位,short为16位,int为32位,long为64位。正因为这些数值的二进制位数已经固定,所以它们能表示的数值大小就有一定的范围限制。因此,Java中提供BigInteger类和 BigDecimal类来处理更大的数字。

Java的常用类之一 ---- String、StringBuffer、StringBuilder类

https://blog.csdn.net/weixin_43244120/article/details/105187804

Java的常用类之二 ----日期时间类

https://blog.csdn.net/weixin_43244120/article/details/105188644

发布了19 篇原创文章 · 获赞 0 · 访问量 487

猜你喜欢

转载自blog.csdn.net/weixin_43244120/article/details/105189069