java.math包
BigDecimal
BigInteger
MathContext
RoundingMode这是枚举
BigDecimal
不可变的,任意精度的有符号十进制数,适合商业计算中,数字很长的数字运算
方法add,subtract,multiply,divide
package java_20180210_api_datetime; import java.math.BigDecimal; import java.math.MathContext; public class BigDemo { public static void main(String[] args) { // long l1=1213133242353245345345; BigDecimal d1=new BigDecimal("20"); BigDecimal d3=new BigDecimal("5"); System.out.println(d1.add(d3)); System.out.println(d1.subtract(d3)); System.out.println(d1.multiply(d3)); System.out.println(d1.divide(d3)); System.out.println(d1.doubleValue()); System.out.println(d1.toString()); System.out.println(d1.toPlainString()); BigDecimal d2=new BigDecimal("134.56", new MathContext(2)); System.out.println(d2.toPlainString()); } }
数学运算类
Math
Random
Math是java.lang中的类
Random是java.util中的类
package java_20180211_api_math; import java.util.Random; public class MathDemo { public static void main(String[] args) { System.out.println(Math.abs(-12.34)); System.out.println(Math.ceil(5.324)); System.out.println(Math.floor(-234.876)); for (int i = 0; i < 10; i++) { System.out.println(Math.random()); } System.out.println("=============="); int i = 0; do { i++; int max=100; int min=60; Random random = new Random(); int s = random.nextInt(max)%(max-min+1)+min; System.out.print(s); System.out.print(" "); } while (i<10); System.out.println("=============="); //下面这个5是种子值 Random r1=new Random(5); for (int j = 0; j < 10; j++) { //加10就代表10-20之间的一个数字 System.out.println(r1.nextInt(11)+10); } } }
比较
Comparable
Comparator
Comparable是一个函数式接口(只有一个抽象方法的接口)。java.lang.Comparable<T>
为现有类提供一种比较的能力
Comparator是一个函数式接口。java.util.Comparator<T>
将两个类进行比较,在集合排序中会大量使用
二者比较
Comparable接口为子类提供比较的能力
Comparator接口封装了比较的逻辑,可用于策略设计模式,其子类常作为排序方法中的参数
package java_20180211_api_math; public class User implements Comparable<User> { int age; @Override public int compareTo(User o) { if (age > o.age) { return 1; } else if (age < o.age) { return -1; } return 0; } } ================== package java_20180211_api_math; public class CompDemo { public static void main(String[] args) { User u1=new User(); u1.age=40; User u2=new User(); u2.age=20; if (u1.compareTo(u2)>0) { System.out.println("u1>u2"); } else if(u1.compareTo(u2)<0){ System.out.println("u1<u2"); }else { System.out.println("u1==u2"); } } } ===================== package java_20180211_api_math; import java.util.Comparator; public class CompDemo2 implements Comparator<User> { @Override public int compare(User o1, User o2) { if (o1.age > o2.age) { return 1; } else if (o1.age < o2.age) { return -1; } return 0; } }
迭代
Iterable
Iterator
java.lang.Iterable<T>
如果想要一个类放在foreach中遍历,就必须实现Iterable<T>这个接口,一般都是数组
package java_20180211_api_math; import java.util.Iterator; class Student { String name; public Student(String name) { this.name = name; } } class Clazz implements Iterable<Student> { Student[] students; public Clazz(int num) { students = new Student[num]; } int index = 0; public void add(Student s) { students[index++] = s; } @Override public Iterator<Student> iterator() { return new Iterator<Student>() { int index = 0; @Override public Student next() { return students[index++]; } @Override public boolean hasNext() { return index != students.length; } }; } } public class IterDemo { public static void main(String[] args) { Clazz c1 = new Clazz(3); Student s1 = new Student("s1"); Student s2 = new Student("s2"); Student s3 = new Student("s3"); c1.add(s1); c1.add(s2); c1.add(s3); // for (Student stu : c1.students) { // System.out.println(stu.name); // } for (Student stu : c1) { System.out.println(stu.name); } } }