文章目录
- 1.java比较器
-
- Comparable接口
- Comparator接口
- 2.BigInteger与BigDecimal
-
- BigInteger(对应整形)
- BigDecimal(对应浮点型)
1.java比较器
说明: java中的对象,正常情况下,只能比较 == 或 != 不能使用 > 或 <,但是在开发场景中,我们需要对多个对象进行排序,就需要比较对象的大小
java实现对象排序的方式有两种:
- 自然排序: java.lang.Comparable
- 定制排序: java.util.Compara
Comparable接口
应用举例: String、包装类实现了Comparable接口,重写了compareTo()方法,给出了比较两个对象大小的方式
重写compareTo()的规则:
- 如果当前对象this大于形参对象obj,则返回正整数;
- 如果当前对象this小于形参对象obj,则返回负整数;
- 如果当前对象this等于形参对象obj,则返回 0;
对于自定义类,可实现Comparable接口,重写compareTo(obj)方法:
(使用Arrays.sort排序时,默认使用compareto()方法进行排序。String中有重写compareto()方法,所以可以用Arrays.sort对String数组进行排序,若一个类没有重写实现Comparable接口的compareto方法,则不能使用Arrays.sort对其进行排序)
class Goods implements Comparable{private String name;private int price;public Goods(){}public Goods(String name,int price){this.name = name;this.price = price;}// 按照商品价格排序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);}}throw new Runt imeException("传入的数据类型不一致");}
}
public class Main{public static void main(String[] args){Goods[] arr = new Goods[3];arr[0] = new Goods("a",12);arr[1] = new Goods("b",9);arr[2] = new Goods("c",10);Arrays.sort(arr);System.out.println(Arrays.toString(arr));}
}
Comparator接口
当元素的类型没有实现Comparable接口时而又不方便修改代码,或者实现了java.lang.Comparable接口的排序规则不适合当前的操作,那么可以考虑使用Comparator的对象来排序
重写compare(Object o1,Object o2)方法,比较o1和o2的大小:如果方法返回正整数,则表示o1大于o2;如果返回0,表示相等;如果返回负整数,表示o1小于o2
public class Main{public static void main(String[] args){String[] arr = new String[]{"ab","cb","df","aa"};Arrays.sort(arr,new Comparator(){// 重写方法public int compare(Object o1,Object o2){if(o1 instanceof String && o2 instanceof String){String s1 = (String)o1;String s2 = (String)o2;return -s1.comparTo(s2);}throw new RuntimeException("输入的类型不一致");}});System.out.println(Arrays.toString(arr));}
}
2.BigInteger与BigDecimal
BigInteger(对应整形)
Integer作为int的包装类,能存储的最大整数值为231-1,long也是有限的,最大为263-1。如果要表示更大的整数,不管是 基本数据类型还是他们的包装类都无能为力,更不用说进行运算了
java.math包的 BigInteger可以表示不可变的任意精度的整数。BigInteger提供所有 java 的基本数据整数操作符的对应物,并提供 java.lang.Math 的所有相关方法,另外,BigInteger还提供以下运算:模运算、GCD运算、质数测试、素数生成、位操作以及一些其他操作。
构造器:
- BigInteger(String val): 根据字符串构建BigInteger对象
常用方法:
- public BigInteger abs(BigInteger val): 返回此 BigInteger 的绝对值 BigInteger.
- public BigInteger add(BigInteger val): 返回其值为 (this + val) 的BigInteger.
- public BigInteger subtract(BigInteger val): 返回其值为 (this - val) 的BigInteger.
- public BigInteger multiply(BigInteger val): 返回其值为 (this * val) 的BigInteger.
- public BigInteger divide(BigInteger val): 返回其值为 (this / val) 的BigInteger。整数相除只保留整数部分.
- public BigInteger remainder(BigInteger val): 返回其值为 (this % val) 的BigInteger.
- public BigInteger[] divideAndRemainder(BigInteger val): 返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组.
- public BigInteger pow(int exponent): 返回其值为 (thisexponent) 的 BigInteger.
BigDecimal(对应浮点型)
一般的 Float类和 Double类可以用来做科学或工程计算,但在 商业计算中。要求数字精确度比较高,故用到 java.Math.BigDcimal类。
BigDecimal类支持不可变的、任意精度的有符号十进制定点数
构造器:
- public BigDecimal(double val)
- public BigDecimal(String val)
常用方法:
- poublic BigDecimal add(BigDecimal augend)
- poublic BigDecimal subtract(BigDecimal subtrahend)
- poublic BigDecimal multiply(BigDecimal multiplicand)
- poublic BigDecimal divide(BigDecimal divisor,int scale,int roundinaMode)
public class Main{public static void main(String[] args){BigDecimal bd = new BigDecimal("123435.351");BigDecimal bd2 = new BigDecimal("11");System.out.println(bd.divide(bd2)); // 若除不尽,会报错,没有指明以四舍五入的方式去处理保留System.out.println(bd.divide(bd2,BigDecimal.ROUND_HALF_UP)); // 11221.396,默认保留 3位System.out.println(bd.divide(bd2, 25, BigDecimal.ROUND_HALF_UP)); // 11221.3955454545454545454545455,保留25位}
}