Java常用工具类

Java常用工具类

前言


多查API-----👆👇👈👉

 

①String类(不可变的字符序列)


String s1 = "abc"; //在常量池

String s2 = new String("abc"); //在堆中有个变量,变量的值存的是,常量池中的值的地址

常用方法

 public static void main(String[] args){
     String s1 = "abc";
     String s2 = "efg";
     
     int s1.length();//字符串长度
     char s1.charAt(int index);//字符串index处的字符
     boolean s1.isEmpty();//判断字符串是否为空
     String s1.toLowerCase();//将字符串中所有字符转为小写
     String s1.toUpperCase();//将字符串中所有字符转为大写
     boolean s1.equals(s2);//比较两个字符串是否相等,别用s1 == s2,这是比较地址值
     boolean s1.equalsIgnoreCase(s2);//比较两个字符串是否相等(不区分大小写)
     String s1.concat(s2);//将s2连接到s1尾部,等价于 s1 + s2
     String s1.trim();//去除首尾的空格,中间的不去 "   a b c   " ---> "a b c"
     boolean s1.compareTo(s2);//比较两个字符串大小
     String s1.substring(int index);//获取字符串中,从index到最后的字符串
     String s1.substring(int begin,int end);//获取字符串中,从begin到end的字符串(左闭右开)
     boolean s1.endsWith(String a);//判断字符串是否以字符串a结束
     boolean s1.startsWith(String a);//判断字符串是否以字符串a开始
     boolean s1.startsWith(String a,int index);//判断字符串从index开始,是否以字符串a开始
     int s1.indexOf(String a);//返回字符串中第一次出现字符串a索引,无则返回-1
     int s1.lastIndexOf(String a);//返回字符串中最后出现字符串a索引,无则返回-1
     boolean s1.contains(String a);//判断字符串中是否含有字符串a
     String s1.replace(String oldS,String newS);//用newS替换字符串中所有的oldS
     String[] s1.split(String regex);//根据正则表达式来分割字符串,返回字符串数组
 }

 

String的转换

  • 将基本数据类型、包装类转换为String

 int num = 123;
 String s1 = String.valueOf(num);
  • String与char[ ]的转换

 //S -> c
 String s1 = "abcde";
 char[] arr = s1.toCharArray();
 //c -> S
 char[] arr = new char[]{'a','b','c','d','e'};
 String s1 = new String(arr);

 

②StringBuffer类/与/StringBuilder类(可变的字符序列)


常用方法

 public static void main(String[] args){
     StringBuffer s1 = new StringBuffer("abc"); //线程安全,效率低
     StringBuilder s1 = new StringBuilder("abc"); //线程不安全,效率高
     void s1.append(a);//进行字符串拼接,将a拼接到s1后
     void s1.delete(int start,int end);//删去[start,end)位置的内容
     void s1.replace(int start,int end,String str);//把[start,end)的内容替换为str
     void s1.insert(int index, a);//在index前插入a
     void s1.reverse();//把当前字符序列逆转
     int s1.indexOf(String str);//有则返回第一个字符下标,无则返回-1
     int s1.length();
     char s1.charAt();
     String s1.substring();
     void s1.setCharAt(int index, char a);//将index处的字符改成a
 }

 

③Date


1、java.util.Date类

  • 两个构造器

 Date()//无参构造器,获取本地当前时间
 Date(long date)//参数为时间戳的构造器,获取时间戳对应的时间
  • 方法

 Date date = new Date();
 date.toString();//重载了toString,输出年、月、日、时、分、秒
 date.getTime();//获取当前时间戳
 //可以获取月、日、时等

 

2、java.sql.Date类

  • 一个构造器

 Date date = new Date(256476581151L);//放入时间戳
 System.out.println(date);//输出2037-07-18
  • 方法

 date.toString();//输出年、月、日
 date.getTime();//获取当前时间戳

 

3、java.util.Date 转换成 java.sql.Date

 Date date = new Date();
 java.sql.Date date2 = new java.sql.Date(date.getTime());

 

4、java.lang.System类

 System.currentTimeMillis();//获取当前时间戳

 

④SimpleDateFormat类


  • 格式化:日期 --> 字符串

 Date date = new Date();
 SimpleDateFormat sdf = new SimpleDateFormat();//默认构造器
 String format = sdf.format(date);
 System.out.println(format);
 //2022/4/12 下午3:57

 

⑤Math类


 int Math.max(int a, int b);//求两数最大值
 int Math.min(int a, int b);//求两数最小值
 double Math.random();//返回[0.0, 1.0)的随机数

 

⑥Arrays类


 Arrays.toString(数组名);//打印数组;
 Arrays.sort(数组名);//数组排序(升序);
 Arrays.sort(数组名,int a, int b);//数组中的[a,b)排序;
 Arrays.fill(数组名,填充的值)//将数组中所有值用传来的值覆盖填充;
 Arrays.equals(int[] a,int[] b);//判断两个数组是否相等

 

⑦Collections类


 reverse(List);//反转List中元素的顺序
 shuffle(List);//对List集合元素进行随机排序
 sort(List);//根据元素的自然顺序对指定的List集合按升序排序
 sort(List,Comparator);//根据指定的Comparator产生的顺序对List集合元素进行排序
 swap(List,int i,int j);//将List中i和j位置的元素交换
 max(Collection);//返回集合中最大的元素
 min(Collection);//返回集合中最小的元素
 frequency(Collection,Object);//返回集合中指定元素的出现次数
 copy(List dest,List src);//将src中的内容复制到dest中
 replaceAll(List list,Object oldVal,Object newVal);//在list中使用newVal替换全部的oldVal

 

⑧Java比较器


自然排序

  • 自定义类中需要排序,可以让自定义类实现Comparable接口,并重写CompareTo(Object o)方法

  • 规则:当前对象this > o,返回正整数

  • 当前对象this < o,返回负整数

  • 当前对象this = o,返回零

 class Node implements Comparable<Node>{
     int num;
     
     @Override
     public void compareTo(Node o){
         return this.num - o.num;   //升序
    }
 }

 

定制排序

  • Comparator

  • 举个例子:一般来说数组实现了接口,重写了排序方法,实现从小到大排序,但是我现在想从大到小排序

  • 可以把其放在Collections.sort 或 Arrays.sort

 Integer[] arr={1,2,3,4};
  //注意必须要用Integer来定义数组,不能用int,因为int没法看成对象,
  //所以要用int的封装类Integer。不然sort方法报错
 Arrays.sort(arr,new Comparator<Integer>(){
        public int compare(Integer a,Integer b){
        return b-a;
        }
        });

 

⑨Biginteger类(大整数类)


  • Biginteger类

 BigInteger a = new BigInteger(String);
 BigInteger a.add(BigInteger);       // +
 BigInteger a.subtract(BigInteger);  // -
 BigInteger a.multiply(BigInteger);  // *
 BigInteger a.divide(BigInteger);    // /
 BigInteger a.modPow(BigInteger, BigInteger); // 快速幂
 //还有许多...

 

posted @   hhux  阅读(179)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示