資料開發_Java基本類和介面原始碼和程式碼
阿新 • • 發佈:2020-12-17
前言
讀原始碼,寫程式碼
基本的包lang的基本情況
1.java.lang.Object java.util.Objects Object: 01.hashCode() equals() toString() 02.clone() :Object類的clone()方法是一個native方法 clone方法首先會判物件是否實現了 Cloneable 介面,若無則丟擲CloneNotSupportedException public boolean equals(Object obj) { return (this == obj);} shallow copy" of this object, not a "deep copy" operation. 03.getClass notify() notifyAll() wait() finalize() 2.java.lang.Cloneable public interface Cloneable {} 說明: 實現此介面的類應當重寫 Object.clone方法 (被保護)帶有public 深度拷貝 用instanceof關鍵字運算子來進行型別查詢 if (obj instanceof Cloneable) { 3.java.lang.Comparable public interface Comparable<T> { int compareTo(T o)} 要用Comparable介面,則必須實現這個介面,並重寫comparaTo()方法;確定該類物件的排序方式 java.util.Comparator Comparator{ compare()} Comparable 更像是一個內部排序介面,一個類實現了Comparable比較器,就意味著它本身支援排序; 可以用Collections.sort() 或者 Arrays.sort() 進行排序 compare() 方法的用法和Comparable 的 compareTo() 用法基本一樣,這個方法不允許進行null值比較,會丟擲空指標異 對於一些自定義類,它們可能在不同情況下需要實現不同的比較策略,我們可以新建立 Comparator 介面, 然後使用特定的 Comparator 實現進行比較 4.java.lang.Iterable public interface Iterable<T> {Iterator<T> iterator();} 只是返回了一個Iterator物件 實現了Iterable的類可以再實現多個Iterator內部類 java.util.Iterator public interface Iterator<E> Iterator 為Java中的迭代器物件,而 iterable 接口裡定義了返回 iterator 的方法 public interface Iterator<E> { boolean hasNext(); E next(); } 5.java.lang.Class getPrimitiveClass java.lang.ClassLoader java.lang.Runnable public interface Runnable java.io.Serializable public interface Serializable {} java.lang.Readable //Readable 是一個字元源。read方法的呼叫方能夠通過 CharBuffer 使用 Readable 中的字元 public interface Readable { java.util.Collection public interface Collection<E> extends Iterable<E> public interface Map<K,V> { public abstract class AbstractMap<K,V> implements Map<K,V> { public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable { public interface SortedMap<K,V> extends Map<K,V> { public interface NavigableMap<K,V> extends SortedMap<K,V> { public class TreeMap<K,V>extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, java.io.Serializable 6.Java中標記介面 以用作描述類的元資料,執行時通過反射機制去獲取元資料,1.5之後註解(Annotation)才成為了最好的維護元資料的方式 Java中的標記介面(Marker Interface),又稱標籤介面(Tag Interface) RandomAccess 介面 Cloneable 介面 Serializable 介面
常用基礎類
包裝類 String類 Array陣列類 具體情況: 1.包裝類 Boolean.valueOf() 靜態方法 裝箱操作--將基本型別轉化為包裝型別 (Java1.5自動裝箱和拆箱) b.booleanValue() 拆箱操作 共同點: 重寫了 Object 的方法 : equals() hashCode() toString() 實現了 Comparable 介面 不可變性 -- final 不可繼承, 變數設定為private且沒有setter方法 2.String public final class String implements java.io.Serializable, Comparable<String>, CharSequence { 方法: 自身的情況,和其他字元的情況 判斷: isEmpty startsWith endsWith contains matches equals() equalsIgnoreCase() 獲取: indexOf lastIndexOf charAt() length() codePointAt()方法在字串的指定索引處返回字元的Unicode值 變換: toLowerCase toUpperCase trim substring() replace replaceFirst replaceAll split 正則 compareTo compareToIgnoreCase equals equalsIgnoreCase() valueOf 資料型別轉換 其他: 結合其他的語句: 迭代迴圈訪問 說明: equals() hashCode() toString() public int compareTo(String anotherString) { 可變動 StringBuffer 執行緒安全 StringBuilder = + += 的背後是採用StringBuilder的append() 3.Array 陣列 同類型元素(元素可以為:primitive or reference 基本型別 或 引用型別) interface type/abstract class 陣列長度建立時指定 陣列的長度不是陣列型別的一部分,在方法中,如果引數的型別是例如 String[] ,呼叫傳入的陣列可以包含任意個元素 陣列型別不是類,但陣列例項是物件 陣列實現了 Serializable,即 陣列中元素可以序列化,則陣列可以被序列化 元素: 沒有名稱,使用的 index values 來進行檢視 Every array type implements the interfaces Cloneable and java.io.Serializable type annotations 建立: An array creation expression is used to create new arrays getLength get getBoolean getByte getChar getShort getInt getLong getFloat getDouble set setBoolean setByte setChar setShort setInt setLong setFloat setDouble Arrays: 複製 比較 批量設定值 計算Hash值 排序 查詢 toString 儲存固定大小的同類型元素 -- component type of the array 元素的型別 元素的值 陣列 element type Arrays must be indexed by int values copyOf copyOfRange equals fill 相關: myIDArray[0] 陣列元素的值, 陣列的索引 length是陣列物件的屬性 int[] myIDArray = new int[5]; int len = myIDArray.length; 初始化: 原始資料型別的陣列元素被初始化為其資料型別的預設值 List<Integer> list=new ArrayList(Arrays.asList(sz));//**須定義時就進行轉化** 說明:宣告 int[] a; 建立和初始化陣列-- 建立即new 宣告和初始化 int[] a = {1,2,3}; ==這種條件下可以省略new關鍵字和元素型別 第二種:先使用new關鍵字建立陣列,然後再分別為陣列中的元素賦值,完成初始化操作
其他情況
方法
帶括號的() - 無引數的情況
類名 物件名= new 構造方法; (構造方法 構造方法和類名相同)
()是引數,裡面沒東西就是沒有引數的構造方法
宣告變數,儲存一個 XXX的物件
建立這個物件 ,括號中可選的引數列表,會傳入類的構造方法,初始化新物件的內部欄位
匿名類
匿名類是一種沒有名稱的區域性類,匿名類是一種表示式 構成 = new 類名稱(){} 花括號中的類主體 匿名類沒有構造方法,括號內常為空 2、匿名類實現HelloWorld介面 HelloWorld frenchGreeting = new HelloWorld() { String name = le monde"; public void greet() { greetSomeone("tout le monde");} };
lambda表示式
引數列表 方法主體
() -> {}
Stream 表達
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
IntStream mapToInt(ToIntFunction<? super T> mapper);
LongStream mapToLong(ToLongFunction<? super T> mapper);
public class Java8Example {
public static void main(String[] args) {
List<Integer> transactionsIds = Arrays.asList(3, 2, null,2, 3, 7, 3, 5);
IntSummaryStatistics stats = transactionsIds
.stream()
.filter(string ->string!=null)
.mapToInt((x) ->x)
.summaryStatistics();
System.out.println("列表中最大的數 : " + stats.getMax());
System.out.println("列表中最小的數 : " + stats.getMin());
System.out.println("所有滿足條件數之和 : " + stats.getSum());
System.out.println("所有滿足條件數的個數 : " + stats.getCount());
System.out.println("平均數 : " + stats.getAverage());
}
}
參考
Comparable 和 Comparator的理解 https://www.cnblogs.com/cxuanBlog/p/10927495.html
Java中的Iterable與Iterator詳解 https://www.cnblogs.com/litexy/p/9744241.html