Java(7)常用的工具類

1.Collections集合工具類

內置了大量對集合操作的靜態方法,可以通過類名直接調用方法。

方法的種類:最大值max、最小值min、sort排序...詳見API幫助文檔

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public class CollectionTest {public static void main(String[] args) {List l = new ArrayList();l.add("c");l.add("e");l.add("m");l.add("o");l.add("g");l.add("f");//最大值String max = (String) Collections.max(l);System.out.println(max);//最小值String min = (String) Collections.min(l);System.out.println(min);//排序Collections.sort(l);System.out.println(l);}
}

2. 泛型

是對集合類型的一種約束,保證集合中只存放一種類型的數據

約束的范圍:集合、方法、類

import java.util.ArrayList;
import java.util.List;public class 泛型測試 {public static void main(String[] args) {List<String> l = new ArrayList();
//      List l = new ArrayList<String>();l.add("a");
//      不符合類型,會報錯👇
//      l.add(1);
//      l.add(true);System.out.println(l);List<Student> studentList = new ArrayList();Student s1 = new Student();Student s2 = new Student();Student s3 = new Student();studentList.add(s1);studentList.add(s2);studentList.add(s3);
//      studentList.add(1);不是學生類型,會報錯}public static double getSum(ArrayList<? extends Number> value){return 0;}
}

3.Object類(超類)

是Java中所有類的父類,是唯一沒有父類的類

常用的方法:

  • toString() -- 轉換字符串
  • hashCode() --? 一個對象的唯一十進制數列
  • equals() -- 判斷兩個字符串是否相等

3.1 模擬hashCode()

public class Test {public static void main(String[] args) {Student student1 = new Student();student1.id = 1;student1.name = "za";Student student2 = new Student();student2.id = 2;student2.name = "ci";System.out.println(student1.hashCode()); //189568618System.out.println(student2.hashCode()); //793589513String s1 = "Hello";String s2 = "World";System.out.println(s1.equals(s2)); //false}
}

3.2 模擬equals

實現屬性匹配,根據classId屬性判斷兩名同學是否來自同一個班

public class EqualsDemo {private String classId;private String name;public EqualsDemo(String classId, String name) {this.classId = classId;this.name = name;}public String getClassId() {return classId;}public void setClassId(String classId) {this.classId = classId;}public String getName() {return name;}public void setName(String name) {this.name = name;}public boolean equals(Object o){if(o instanceof EqualsDemo){int i = 0;EqualsDemo e = (EqualsDemo)o;if (e.getClassId().equals(this.getClassId())){return true;}else{return false;}}return false;}
}

測試文件: 返回true

public class EqualsDemoTest {public static void main(String[] args) {EqualsDemo tom = new EqualsDemo("1234","Tom");EqualsDemo jack = new EqualsDemo("1234","Jack");System.out.println(tom.equals(jack));}
}

4. 包裝類

是基于基本類型封裝的工具類,提供了大量對數據進行操作的方法。如:類型轉換方法

包裝類的種類:

  • 所有基本類型都有對應的包裝類
  • 包裝類中父類中的提供了valueOf()方法,可用于類型轉換
  • 各包裝類中提供了自己的類型轉換方法parseInt()、parseFoalt()等
  • 包裝類和對應的基本類型提供了自動裝箱、自動拆箱功能,因此可以直接轉換
public class 包裝類測試 {public static void main(String[] args) {int a = 1;Integer b = 2;System.out.println(a + b);//3
//      該寫法JDK1.8支持👇
//      Integer c = new Integer(500);String c = "500";System.out.println(c+1);//5001//字符串轉數字,該字符串必須為可計數類型Integer d = Integer.valueOf(c);System.out.println(d+1);//501Integer e = Integer.parseInt(c);System.out.println(e+1);//501}
}

5. 字符串

種類:

  • Sting:將字符串存到常量池中,查詢快,修改、追加慢。
  • StringBuffer:將字符串存放在堆中,查詢慢,修改和追加快,線程安全的
  • StringBuilder:將字符串存放在堆中,查詢快,修改和追加快,線程非安全的

5.1 equals內容比較?

public class 字符串測試1 {public static void main(String[] args) {String str1 = "hello";String str2 = new String("hello");System.out.println(str1 == str2);//false//equals可以對字符串的內容進行比較System.out.println(str1.equals(str2));//trueSystem.out.println(1+2+str1);//3helloSystem.out.println(str1+1+2);//hello12System.out.println(str1+(1+2));//hello3StringBuffer sb = new StringBuffer("abc");sb.append("efg");System.out.println(sb);//abcefgSystem.out.println(sb.length());//6System.out.println(sb.reverse());//gfecba

5.2 三種字符串追加比較

計算并打印出三種字符串拼接操作所花費的時間。

public class 字符串測試2 {public static void main(String[] args) {//String測試String s = "doudou";//獲得系統當前的毫秒數long start = System.currentTimeMillis();for (int i = 0; i < 50000; i++) {s+="doudou";}long end = System.currentTimeMillis();System.out.println("String使用了:"+(end-start));//StringBuffer測試StringBuffer strb = new StringBuffer("doudou");long start1 = System.currentTimeMillis();for (int i = 0; i < 50000; i++) {strb.append("doudou");}long end1 = System.currentTimeMillis();System.out.println("StringBuffer使用了:"+(end1-start1));//StringBuilder測試StringBuilder strbu = new StringBuilder("doudou");long start2 = System.currentTimeMillis();for (int i = 0; i < 50000; i++) {strbu.append("doudou");}long end2 = System.currentTimeMillis();System.out.println("StringBuilder使用了:"+(end2-start2));}
}

輸出結果:

String使用了:1356
StringBuffer使用了:2
StringBuilder使用了:1

6. 數學運算類?

6.1?Math數學運算類

public class 數學運算類 {public static void main(String[] args) {System.out.println(Math.ceil(1.2)); //向上取整(2.0)System.out.println(Math.floor(1.8));//向下取整(1.0)System.out.println(Math.round(1.5));//四舍五入(2)}
}

6.2 產生10個1-10的隨機數

import java.util.Random;
public class 數學運算類 {public static void main(String[] args) {//方法1for (int i=0;i<10;i++){double result = (Math.random()*10)+1;System.out.println((int)result);}//方法2Random r = new Random();for (int i=0;i<10;i++){int result = r.nextInt(10)+1;System.out.println(result);}}
}

7. 日期時間類

7.1?獲得系統時間

import java.util.Date;
public class 日期時間類1 {public static void main(String[] args) {//獲得當前系統時間Date now = new Date();System.out.println(now); //此刻時間//系統初始時間+1000msDate d = new Date(1000);System.out.println(d); //Thu Jan 01 08:00:01 CST 1970

7.2 比較時間前后

public class 日期時間類 {public static void main(String[] args) {//調用者日期是否在輸出者之后/前System.out.println(now.after(d)); //trueSystem.out.println(now.before(d)); //false//調用者日期在參數者之后,返回1,否則返回-1,相等返回0System.out.println(now.compareTo(d)); //1System.out.println(d.compareTo(now)); //-1}
}

7.3 設置日歷工具

import java.util.Calendar;
public class 日期時間類3 {public static void main(String[] args){//顯示此時此刻的日期時間Calendar calendar = Calendar.getInstance();System.out.println(calendar.get(Calendar.YEAR));System.out.println(calendar.get(Calendar.MONTH)+1);//默認初始值為0,而非1月System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//使用日歷工具設置日期calendar.set(2025,6,3);System.out.println(calendar.get(Calendar.YEAR)); //2025System.out.println(calendar.get(Calendar.MONTH)); //6System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //3}
}

7.4?日期時間格式化

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
public class 日期時間類4 {public static void main(String[] args) throws ParseException {//日期時間格式化DateFormat df = DateFormat.getInstance();//設置格式DateFormat df1 = DateFormat.getDateInstance(DateFormat.LONG);System.out.println(df1.format(new Date())); //2025年1月14日//自定義日期時間格式化工具SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//將日期格式的數據轉換成字符串System.out.println(sdf.format(new Date())); //2025-01-14 10:43:47//字符串轉換成日期格式Date date1 = sdf.parse("2025-01-14 10:43:47");System.out.println(date1);}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/66026.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/66026.shtml
英文地址,請注明出處:http://en.pswp.cn/web/66026.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【Varnish】:解決 Varnish 7.6 CDN 靜態資源緩存失效問題

項目場景&#xff1a; 在一個使用Varnish作為反向代理的Web應用中&#xff0c;我們依賴CDN&#xff08;內容分發網絡&#xff09;來緩存靜態資源&#xff08;如圖片、CSS、JavaScript文件等&#xff09;&#xff0c;以提高全球用戶的訪問速度并減輕源站服務器的負載。然而&…

理解機器學習中的參數和超參數

在機器學習中&#xff0c;參數和超參數是兩個重要但不同的概念&#xff0c;它們共同影響模型的性能和表現。以下是它們的定義和區別&#xff0c;以及如何通俗地理解它們&#xff1a; 1. 參數 定義 參數是模型在訓練過程中自動學習到的變量&#xff0c;它們直接決定了模型如何…

Win11右鍵菜單實現

主要參考Win11 Context Menu Demo 此工程是vs2022編譯&#xff0c;vs2019先修改下 base.h 方可編譯過 編譯好dll以后 拷貝至SparsePackage目錄下 生成稀疏包msix 就拿他工程里面的改&#xff0c;編輯AppxManifest.xml&#xff0c;配置都要對&#xff0c;一個不對可能都失敗&a…

R.swift庫的詳細用法

R.swift 是一個 Swift 工具庫,它提供了一個自動生成的類 R,使得你可以通過類型安全的方式訪問項目中的資源,例如圖片、字體、顏色、XIB 文件等。通過 R.swift,你可以避免字符串類型的錯誤,提升代碼的可維護性。 以下是 R.swift 庫的詳細用法: 1. 安裝 R.swift 使用 Sw…

像JSONDecodeError: Extra data: line 2 column 1 (char 134)這樣的問題怎么解決

問題介紹 今天處理返回的 JSON 的時候&#xff0c;出現了下面這樣的問題&#xff1a; 處理這種問題的時候&#xff0c;首先你要看一下當前的字符串格式是啥樣的&#xff0c;比如我查看后發現是下面這樣的&#xff1a; 會發現這個字符串中間沒有逗號&#xff0c;也就是此時的J…

what?ngify 比 axios 更好用,更強大?

文章目錄 前言一、什么是ngify&#xff1f;二、npm安裝三、發起請求3.1 獲取 JSON 數據3.2 獲取其他類型的數據3.3 改變服務器狀態3.4 設置 URL 參數3.5 設置請求標頭3.6 與服務器響應事件交互3.7 接收原始進度事件3.8 處理請求失敗3.9 Http Observables 四、更換 HTTP 請求實現…

Linux Kernel 之十 詳解 PREEMPT_RT、Xenomai 的架構、源碼、構建及使用

概述 現在的 RTOS 基本可以分為 Linux 陣營和非 Linux 陣營這兩大陣營。非 Linux 陣營的各大 RTOS 都是獨立發展,使用上也相對獨立;而 Linux 陣營則有多種不同的實現方法來改造 Linux 以實現實時性要求。本文我們重點關注 Linux 陣營的實時內核實現方法! 本文我們重點關注 …

【拒絕算法PUA】3065. 超過閾值的最少操作數 I

系列文章目錄 【拒絕算法PUA】0x00-位運算 【拒絕算法PUA】0x01- 區間比較技巧 【拒絕算法PUA】0x02- 區間合并技巧 【拒絕算法PUA】0x03 - LeetCode 排序類型刷題 【拒絕算法PUA】LeetCode每日一題系列刷題匯總-2025年持續刷新中 C刷題技巧總結&#xff1a; [溫習C/C]0x04 刷…

ClickHouse-CPU、內存參數設置

常見配置 1. CPU資源 1、clickhouse服務端的配置在config.xml文件中 config.xml文件是服務端的配置&#xff0c;在config.xml文件中指向users.xml文件&#xff0c;相關的配置信息實際是在users.xml文件中的。大部分的配置信息在users.xml文件中&#xff0c;如果在users.xml文…

《自動駕駛與機器人中的SLAM技術》ch9:自動駕駛車輛的離線地圖構建

目錄 1 點云建圖的流程 2 前端實現 2.1 前端流程 2.2 前端結果 3 后端位姿圖優化與異常值剔除 3.1 兩階段優化流程 3.2 優化結果 ① 第一階段優化結果 ② 第二階段優化結果 4 回環檢測 4.1 回環檢測流程 ① 遍歷第一階段優化軌跡中的關鍵幀。 ② 并發計算候選回環對…

type 屬性的用途和實現方式(圖標,表單,數據可視化,自定義組件)

1.圖標類型 <uni-icon>組件中&#xff0c;type可以用來指定圖標的不同樣式。 <uni-icons type"circle" size"30" color"#007aff"></uni-icons> //表示圓形 <uni-icons type"square" size"30" co…

網絡基礎知識指南|1-20個

1. IP地址: 即互聯網協議地址&#xff0c;是用于標識互聯網上的每一個設備或節點的唯一地址。IP地址的作用主要是進行網絡設備的定位和路由&#xff0c;確保數據包可以從源設備準確地傳送到目標設備。2. 子網掩碼: 是用于將一個IP地址劃分為網絡地址和主機地址的工具。它通常與…

GPT 系列論文精讀:從 GPT-1 到 GPT-4

學習 & 參考資料 前置文章 Transformer 論文精讀 機器學習 —— 李宏毅老師的 B 站搬運視頻 自監督式學習(四) - GPT的野望[DLHLP 2020] 來自獵人暗黑大陸的模型 GPT-3 論文逐段精讀 —— 沐神的論文精讀合集 GPT&#xff0c;GPT-2&#xff0c;GPT-3 論文精讀【論文精讀】…

lombok在高版本idea中注解不生效的解決

環境&#xff1a; IntelliJ IDEA 2024.3.1.1 Spring Boot Maven 問題描述 使用AllArgsConstructor注解一個用戶類&#xff0c;然后調用全參構造方法創建對象&#xff0c;出現錯誤&#xff1a; java: 無法將類 com.itheima.pojo.User中的構造器 User應用到給定類型; 需要:…

145.《redis原生超詳細使用》

文章目錄 什么是redisredis 安裝啟動redis數據類型redis key操作key 的增key 的查key 的改key 的刪key 是否存在key 查看所有key 「設置」過期時間key 「查看」過期時間key 「移除」過期時間key 「查看」數據類型key 「匹配」符合條件的keykey 「移動」到其他數據庫 redis數據類…

大數據技術Kafka詳解 ⑤ | Kafka中的CAP機制

目錄 1、分布式系統當中的CAP理論 1.1、CAP理論 1.2、Partitiontolerance 1.3、Consistency 1.4、Availability 2、Kafka中的CAP機制 C軟件異常排查從入門到精通系列教程&#xff08;核心精品專欄&#xff0c;訂閱量已達600多個&#xff0c;歡迎訂閱&#xff0c;持續更新…

riscv架構下linux4.15實現early打印

在高版本linux6.12.7源碼中&#xff0c;early console介紹&#xff0c;可參考《riscv架構下linux6.12.7實現early打印》文章。 1 什么是early打印 適配內核到新的平臺&#xff0c;基本環境搭建好之后&#xff0c;首要的就是要調通串口&#xff0c;方便后面的信息打印。 正常流…

improve-gantt-elastic(vue2中甘特圖實現與引入)

1.前言 項目開發中需要使用甘特圖展示項目實施進度&#xff0c;左側為表格計劃&#xff0c;右側為圖表進度展示。wl-gantt-mater&#xff0c;dhtmlx嘗試使用過可拓展性受到限制。gantt-elastic相對簡單&#xff0c;可操作性強&#xff0c;基礎版本免費。 甘特圖&#xff08;Gan…

力扣 全排列

回溯經典例題。 題目 通過回溯生成所有可能的排列。每次遞歸時&#xff0c;選擇一個數字&#xff0c;直到選滿所有數字&#xff0c;然后記錄當前排列&#xff0c;回到上層時移除最后選的數字并繼續選擇其他未選的數字。每次遞歸時&#xff0c;在 path 中添加一個新的數字&…

1/13+2

運算符重載 myString.h #ifndef MYSTRING_H #define MYSTRING_H #include <cstring> #include <iostream> using namespace std; class myString {private:char *str; //記錄c風格的字符串int size; //記錄字符串的實際長度int capacity; …