常用API

1,常用API(二)

(1)Math

Math:代表数学,是一个工具类,里面提供的都是对数据进行操作的一些静态方法

public static int abs(int a)//获取参数绝对值
System.out.println(Math.abs(-12));//12
System.out.println(Math.abs(-15.2));//15.2

public static double ceil(double a)//向上取整
Math.ceil(4.000001);//5.0
Math.ceil(4.0);//4.0

public static double floor(double a)//向下取整
Math.fioor(4.9999);//4.0
Math.fioor(5.0000);//5.0

public static int round(float a)//四舍五入
Math.round(3.49999);//3
Math.round(3.50001);//4

public static int max(int a,int b)//获取两个int值中的较大值
Math.max(10,20);//20

public static int min(int a,int b)//获取两个int值中的较小值
Math.min(10,20);//10

public static double pow(double a,double b)//返回a的b次幂的值
Math.pow(2,3);//8.0
Math.pow(3,2);//9.0

public static double random()//返回值为double的随机值,范围[0.0,1.0)

(2)System

System:代表程序所在的系统,也是一个工具类

public static void exit(int status)//终止当前运行的java虚拟机
System.exit(0);//人为的终止虚拟机(不要使用)

public static long currentTimeMills()//返回当前系统的时间毫秒值形式

时间毫秒值:指的是从1970年1月1日 00:00:00开始走到此时此刻的总的毫秒数,应该是很大的,1s=1000ms

(3)Runtime

Runtime:代表程序所在的运行环境

Runtime:是一个单例类

public static Runtime getRuntime()//返回与当前java应用程序关联的运行时对象

public void exit(int status)//终止当前运行的虚拟机

public int availableProcessors()//返回java虚拟机可用的处理器数

public long totalMemony()//返回Java虚拟机中的内存总量

public long freeMemory()//返回Java虚拟机中的可用内存

public Process exec(String command)//启动某个程序,并返回代表该程序的对象

(4)BigDecimal

BigDecimal:用于解决浮点型运算时,出现结果失真的问题

public BigDecimal(double val)//将double转换成BigDecimal
BigDecimal a1=new BigDecimal(Double.toString(a));
BigDecimal a2=new BigDecimal(Double.toString(b));

public BigDecimal(String val)//把String转换成BigDecimal
public static BigDecimal valueOf(double val)//转换一个double成BigDecimal(不推荐)
BigDecimal a1=BigDecimal.valueOf(a);
BigDecimal b1=BigDecimal.valueOf(b);

public BigDecimal add(BigDecimal b)//加法
BigDecimal c1=a1.add(b1);//0.3

public BigDecimal subtract(BigDecimal b)//减法
BigDecimal c2=a1.subtract(b1);//-0.1

public BigDecimal multiply(BigDecimal b)//乘法
BigDecimal c3=a1.multiply(b1);//0.02

public BigDecimal divide(BigDecimal b)//除法
BigDecimal c4=a1.divide(b1);//0.5

public BigDecimal divide(另一个BigDecimal对象,精确几位,舍入模式)//除法,可以控制精确到小数几位
BigDecimal i=BigDecimal.valueOf(0.1);
BigDecimal j=BigDecimal.valueOf(0.2);
BigDecimal k=i.divide(j,2, RoundingMode.HALF_UP);
System.out.println(k);

public double doubleValue()//将BigDecimal转换成double
double rs=k.doubleValue();
System.out.println(rs);
  • (5)Date

构造器说明
public Date()创建一个Data对象,代表的是系统当前此刻的日期时间
public Date(long time)把时间毫秒值转换成Data日期对象
常见方法说明
public long getTime()返回从1970年1月1日 00:00:00走到此刻的总的毫秒数
public void setTime(long time)设置日期对象的时间为当前时间毫秒值对应的时间

(6)SimpleDateFormat

简单日期格式化:可以用来把日期对象,时间毫秒值格式化成我们想要的形式

常见构造器说明
public SimpleDateFormat(String pattern)创建简单日期格式化对象,并封装时间的格式
常见构造器说明
public final String format(Date date)将日期格式化成日期/时间字符串
public final String format(0bject time)将时间毫秒值式化成日期/时间字符乌
解析方法说明
public Date parse(String source)把字符串时间解析成日期对象

(7)Calender

Calender:代表的是系统此刻时间对应的日历,通过它可以单独获取、修改时间中的年、月、日、时、分、秒等

注意:calendar是可变对象,一旦修改后其对象本身表示的时间将产生变化

Calender日历类的常见方法:

方法名说明
public static calendar getInstance()获取当前日历对象
public int get(int field)获取日历中的某个信息
public final Date getTime()获取日期对象
public long getTimeInMi1lis()获取时间毫秒值
public void set(int field,int value)修改日历的某个信息
public void add(int field,int amount)为某个信息增加/减少指定的值
int year=now .get(Calendar.YEAR);
System.out.println(year);//2023

int days=now.get(Calendar.DAY_OF_YEAR);
System.out.println(days);//319

//修改
now.set(Calendar.MONTH,9);//修改月份为10月份
now.set(Calendar.DAY_OF_YEAR,125);//修改为一年中的第125天
System.out.println(now);

//增减
now.add(Calendar.DAY_OF_YEAR,100);//一年中的天数加100天
now.add(Calendar.DAY_OF_YEAR,-10);//一年中的天数减10天

JDK8新增时间

JDK8之前传统的时间APIJDK8开始之后新增的时间API
1   设计不合理,使用不方便,很多都被淘汰了设计更合理,功能丰富,使用更方便
2都是可变对象,修改后会丢失最开始的时间信息都是不可变对象,修改后会返回新的时间对象,不会丢失最开始的时间
3线程不安全线程安全
4只能精确到毫秒能精确到毫秒、纳秒

(8)LocalDate  LocalTime  LocalDateTime

LocalDate:代表本地日期(年,月,日,星期)

LocalTime:代表本地时间(时,分,秒,纳秒)

LocalDateTime:代表本地日期,时间(年,月,日,星期,时,分,秒,纳秒)

他们获取对象的方案

方法名示例
public static Xxxx now() 获取系统当前时间对应的该对象

LocaDate ld = LocalDate.now();

LocalTime lt = LocalTime.now();

LocalTime lt = LocalTime.now();

int year=ld.getYear();//年
int month=ld.getMonthValue();//月(1-12)
int day=ld.getDayOfMonth();//日
int dayOfYear=ld.getDayOfYear();//一年中的第几天
int dayOfWeek=ld.getDayOfWeek().getValue();//星期几
可以把LocalDateTime转换成LocolDate和LocolTime
public LocalDate toLocalDate()
public LocalTime toLocalTime()
piblic static LocalDateTime of(LocalDate date,LocalTime time)
方法名说明
public int getHour()获取小时
public int getMinute()获取分
public int getSecond()获取秒
public int getNano()获取纳秒
方法名说明

getYear.getMonthValue. getDayOfMonth、getDayOfYear

getDayOfweek. getHour. getMinute. getSecond、getNano

获取年月日、时分秒、纳秒等

withYear、withMonth、withDayOfMonth、withDayOfYear

withHour、withMinute、 withSecond、withNano

修改某个信息,返回新日期时间对象

plusYears、plusMonths、plusDays、plusweeks

plusHours、plusMinutes、 plusSeconds、plusNanos

把某个信息加多少,返回新日期时间对象

minusYears、minusMonths、 minusDay,minusweeks

minusHours、minusMinutes、minusSeconds,minusNanos

把某个信息减多少,返回新日期时间对象
equals isBefore isAfter判断2个时间对象,是否相等,在前还是在后

(9)ZoneId ZoneDataTime

ZoneId:代表时区Id

方法名说明
public static Set<String> getAvailablezonelds()获取Java中支持的所有时区
public static Zoneld systemDefault()获取系统默认时区
public static Zoneld of(String zoneld)获取一个指定时区
方法名说明
public static ZonedDateTime naw()获取当前时区的ZonedDateTime对象
public static ZonedDateTime now(Zoneld zone)获取指定时区的ZonedDateTime对象
getYear. getMonthvalue. getDayOfMonth. getDayofYeargetDayofweek,getHour、getMinute、getSecond、 getNano获取年月日、时分秒、纳秒等
public ZonedDateTime withXxx(时间)修改时间系列的方法
public ZonedDateTime minusXxoq(时间)减少时间系列的方法
public ZonedDateTime plusXxoq(时间)增加时间系列的方法

(10)Instant

Instant:通过获取Instant的对象可以拿到此刻的时间,该时间由两部分组成:从1970-01-0100:00:00开始走到此刻的总秒数+不够1秒的纳秒数

方法名说明
public static Instant now()获取当前时间的Instant对象(标准时间)
public long getEpochSecond()获取从1970-01-01T00: 00: 00开始记录的秒数
public int getNano()从时间线开始,获取从第二个开始的纳秒数
plusMillis plusSeconds plusNanos增加时间系列的方法
minusMillis minusSeconds minusNanos减少时间系列的方法
equals. isBefore. isAfter判断系列的方法

作用:可以直接用来记录代码的执行时间,或用于记录用户操作某个事件的时间点

传统的Date类,只能精确到毫秒,并且是可变对象

新增的instant类,可以精确到纳秒,并且是不可变对象,推荐使用Instant代替Date

(11)DateTimeFormatter

DateTimeFormatter:线路更安全

方法名说明
public static DateTimeFormatter ofPattern(时间格式)获取格式化器对象
public string format(时间对象)格式化时间
String rs=formatter.format(now);//正向格式化
System.out.println(rs);

String rs2=now.format(formatter);//反向格式化
System.out.println(rs2);
方法名说明
public String format(DateTimeFormatter formatter)格式化时间
public static LocalDateTime parse(CharSequence text,DateTimeFormatter formatter)解析时间

(12)Period

Period:可以用于计算两个LocalDate对象相差的年数,月数,天数

方法名说明
public static Period between(LocalDate start,LocalDate end)传入2个日期对象,得到Period对象
public int getYears()计算隔几年,并返回
public int getMonths()计算隔几个月,并返回
public int getDays()计算隔多少天,并返回

(13)Duration(持续时间)

Duration:可用于计算两个时间对象相差的天数,小时数,分数,纳秒数;支持LocalTime,LocalDateTime,Instant

方法名说明
public static Duration between(开始时间对象1,截止时间对象2)传入2个时间对象,得到Duration对象
public long toDays()计算隔多少天,并返回
public long toHours()计算隔多少小时,并返回
public long toMinutes()计算隔多少分,并返回
public long getSeconds()计算隔多少秒,并返回
public long toMillis()计算隔多少毫秒,并返回
public long toNanos()计算隔多少纳秒,并返回

(14)Arrays

Arrays:用来操作数组的一个工具类

常见方法

方法名说明
1     public static String tostring(类型]arr)返回数组的内容
2public static int[] copyOfRange(类型[]arr,起始索引,结束索引)拷贝数组(指定范围)
3public static copyOf(类型arr, int newLength)拷贝数组
4public static setAll(double[]array, IntToDoubleFunction generator)把数组中的原数据改为新数据
5public static void sort(类型arr)对数组进行排序(默认是升序排序)
4
double[] prices={99.8,128,100};
        Arrays.setAll(prices, new IntToDoubleFunction() {
            @Override
            public double applyAsDouble(int value) {
                return prices[value]*0.8;//[79.84, 102.4, 80.0]
            }
        });

5Arrays.sort(prices);
System.out.println(Arrays.toString(prices));//[79.84, 80.0, 102.4]

如果数组中存储的是对象,排序方法:

方式一:让该对象的类实现Comparable(比较规则)接口,然后重写compareTo方法,自己来制定比较规则

public static <T> void sort(T]arr, Comparator<? super T> c)对数组进行排序(支持自定义排序规则)

comparaTo方法的几个约定
约定一:认为左边对象 大于 右边对象 请您返回正整数
约定二:认为左边对象 小于 右边对象 请您返回负整数
约定三:认为左边对象 等于 右边对象 请您一定返回0
public int compareTo(Student o) {
if (this.age>o.age){
            return 1;
        }else if (this.age<o.age){
            return -1;
        }
        return 0;
    }

//简化代码
return this.age-o.age;//升序
return o.age-this.age;//降序

方式二∶使用下面这个sort方法,创建Comparator比较器接口的匿名内部类对象,然后自己制定比较规则

约定同上

if (o1.getHeight()>o2.getHeight()){
                    return 1;
                }else if (o1.getHeight()<o2.getHeight()){
                    return 2;
                }
                return 0;//升序

//简化代码
return Double.compare(o1.getHeight(),o2.getHeight());//升序
return Double.compare(o2.getHeight(),o1.getHeight());//降序

double不可以使用方法一的简化代码

(15)Lambda表达式

Lsmbda表达式时JDK8开始新增的一种语法形式;

作用:用于简化匿名内部类的代码写法

注意:Lambda表达式并不是说能简化全部匿名内部类的写法,只能简化函数式接口的匿名内部类
//原始代码
Swimming s=new Swimming() {
        @Override
        public void swim() {
        System.out.println("狗跑的贼快~~~");
    }
};
s.swim();


//简化代码
Swimming s=()->{
    System.out.println("狗跑的贼快~~~");
};
s.swim();

interface Swimming{
    void swim();
}

 函数式接口:有且只能有一个抽象方法的接口

注意:将来我们见到的大部分函数式接口,上面都可能会有一个@Functionallnterface的注解,有该注解的接口就必定是函数式接口

(16)Lambda表达式的省略规则

Lambda表达式的省略规则
1   参数类型可以省略不写
2如果只有一个参数,参数类型可以省略,同时()也可以省略
3如果Lambda表达式中的方法体代码只有一行代码,可以省略大括号不写,同时要省略分号!此时,如果这行代码是return语句,也必须去掉return不写。
//原始代码
Arrays.setAll(prices, (int value) ->{
    return prices[value]*0.8;
});

//简化代码
Arrays.setAll(prices, value ->{
    return prices[value]*0.8;
});

//超级简化版
Arrays.setAll(prices, value -> prices[value]*0.8);

(17)方法引用

1)静态方法引用

类名:静态方法

使用场景:如果某个Lambda表达式里只是调用一个静态方法,并且前后参数的形式一致,就可以使用静态方法引用

//源代码
Arrays.sort(students,(o1,o2)->CompareByData.compareByAge(o1,o2));

//简化后的
Arrays.sort(students,CompareByData::compareByAge);

2)实例方法引用

对象名::实例方法

使用场景:如果某个Lambda表达式里只是调用一个实例方法,并且前后参数的形式一致,就可以使用实例方法引用

//源代码
Arrays.sort(students,(o1,o2)->CompareByData.compareByAgeDesc(o1,o2));

//简化后的
Arrays.sort(students,CompareByData::compareByAgeDesc);

3)特定类型的方法引用

类型::方法

使用场景:如果某个Lambda表达式里只是调用一个实例方法,并且前面参数列表中的第一个参数是作为方法的主调,后面的所有参数都是作为该实例方法的入参的,则此时就可以使用特定类型的方法引用

//源代码
Arrays.sort(names,((o1, o2) -> o1.compareToIgnoreCase(o2)));

//简化代码
Arrays.sort(names,String::compareToIgnoreCase);

4)构造器引用

类名:new

使用场景:如果某个Lambda表达式里只是在创建对象,并且前后参数情况一致,就可以使用构造器引用

//源代码
CreateCar cc=(name,price)->new Car(name,price);

//简化代码
CreateCar cc=Car::new;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值