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);}
}