Java静态工具类

JDK常用静态工具类实用总结----2019-3-15

JDK中有很多内置工具类以s结尾,集合工具类Collections,数组工具类Arrays,对象工具类Objects,文件工具类Files,路径工具类Paths,数学工具类Maths,还有TimeUtil和System

 

1、Objects中有很多有用的Api,如空指针判断、对象比相等较等。

判断两个对象是否相等,两个参数都为null

Public static boolean equals(Objhect a,Object b)

判断对象是否为空

Public static boolean isNull(Object o)

判断对象是不为空

Public static boolean nonNull(Object o)

对变量进行非空校验,如果为空则抛出空指针异常,不为空则返回此变量,还可以抛出指定异常信息

Public static<T> T requireNonNull(T obj)  //如果为null,返回异常,需要用try catch 捕获

Public static<T> T requireNonNull(T obj,String message)

 

2、Arrays数组工具类

//数组排序

Public static void sort(int[] a)//数组由小到大排序

 

//二分法查找数组中的元素,之前一定要进行排序

Public static int binarySearch(long[] a,long key)

 

//判断两个数组是否相等(数组元素对应相等,数组长度相等)

Public static boolean equals(long[] a,long[] b) //Arrays.equals()

 

//使用定值填充数组

Public static void fill(long[] a,long val)

 

//复制数组

Public statib<T> T[] copyOf(T[] original,int newLength)

 

//获取数组的hashcode

Public static int hashcode(long a[])  //Arrays.hashcode();

 

//将整个数组转化为形如:[a,b,c,d]的格式的字符串

Publi static String toString(int[] a)   //Arrays.toString(a);

 

3、Collection 中定义了许多集合常用API,

//对List进行排序、

Public static <T extends Comparable<? Super T>> void sort(List<T> list)

//二分法查找List集合元素索引值。List必须排序

Public static<T> int binarySearch(List<? Extends Comparable<? Super T> > list,T key)

//翻转List

Public static void reverse(List<?>list)  //Collections.reverse(List);

//public static <T extends Object & Comparable<? Super T>>

T min(Collection<? Extends T> coll)

 

//寻找List中最大元素

public static <T extends Object & Comparable<? super T>>

T max(Collection<? extends T> coll)

Eg:Collections.max(nums)   //nums是List集合

//寻找List中最小元素

public static <T extends Object & Comparable<? super T>>

T min(Collection<? extends T> coll)

Eg:Collections.min(nums)

 

4、 Files常用的与文件相关的API:

  //获取指定文件的字节输入流

Public static InputStream new InputStream(Path path,OpeOption …options)

//获取指定文件的字节输出流

Public static OutputStream new OutputStream(Path path,OpenOption… options)

//以指定编码获取指定文件的字符输入流

Public static BufferedReader newBufferedReader(Path path,Charset cs)

//以指定编码获取指定文件的字符输出流

Public static BuffereWriter  BufferedWriter(Path path,Charset cs,OpenOption… options)

//以指定编码获取文件中的所有内容,并以字符串返回

Public static Stream<String> lines(Path path,Charset cs)

 

//创建文件

Public  static Path createFile(Path path,FileAttribute<?>… attrs)

//创建路径

Public static Path createDirectory(Path dir,FileAttribute<?>.. attrs)

//删除文件

Public static void delete(Path path)

//如果文件存在,则删除文件

Public static void boolean deleteIfExists(Path path);

//文件复制

Public static Path copy(Path source,Path target,copyOption… options)

//文件移动

Public static Path move(Path path,Path target,CopyOption… options)

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值