首頁  >  文章  >  Java  >  Java 集合泛型

Java 集合泛型

WBOY
WBOY原創
2024-08-30 15:47:22324瀏覽

Java集合泛型用於在指定的具體類型中添加一種方法,它是操作物件的類別和方法的通用目的,用於執行和抽象化它,以便它看起來更典型地在集合中使用。 Java 泛型與其他類別(而非集合類別)是展示 Java 泛型基礎知識的最簡單方法。儘管如此,它仍然是透過引用和取消引用來展示 Java 泛型基礎知識的最快方法。它使用 Collection 的泛型類型和其他類別(如 HashSet、HashMap 等)的設定

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

Java 集合泛型概述

Java 泛型對於建立排序及其方法更為重要;它首先執行轉換,然後使用資料類型數組,如整數、字串、字元或支援使用者資料排序的任何其他數組。 Java 程式設計師使用泛型方法和泛型類別來分別使用類別宣告在單一方法宣告中允許和宣告一組具有指定型別的相關方法。泛型類別還在指定的建置時間提供安全性,允許程式設計師透過特定的轉換捕獲錯誤的類型。此外,我們可以使用 Java 通用思想建構一​​個對物件數組進行排序的通用方法,而不是使用整數數組、雙精度數組、字串數組和其他資料類型過濾器等的通用方法來對數組內容進行排序。 🎜>

Java 中泛型集合的使用量

在Java程式語言中,泛型集合是最重要的,它允許使用類型安全性來建立單一類型的物件。如果類型是類型安全的,則表示編譯器將在使用編譯時驗證特定類型,即使如果類型值不受支援也會拋出錯誤。如果假設我們使用類型轉換,則表示不需要轉換 String、Integer、double 等類型。但大多數情況下,當我們在應用程式中執行此操作時,不需要泛型。在泛型之前,我們可以使用指定的集合來儲存任何對象,這些物件可以是任何類型,例如泛型和非泛型,現在泛型將強制Java 編碼器執行物件建立及其操作,例如儲存和檢索值後端到前端。如果我們停用泛型集合,那麼我們應該使用類型轉換來實現所需的結果,並且在泛型上下文中不需要類型轉換。

通用集合有一些預設方法來實現使用物件數組和集合來從數組集合中的所有物件中放入和獲取資料的功能。與在MapK、V>中一樣,通用Map介面(和地圖實作)是用兩個參數化類型來定義的。每個鍵的類型是 K,鍵關聯值的類型是 V。因此,我們聲明 MapString, Integer>;儲存一個地圖,指示每個單字在文字檔案中出現的頻率。我們將建立 MapString、ListInteger>>如果我們需要一個地圖來定義文字檔案中每個單字出現的行。值得注意的是,我們可以組合(或分層)泛型類型實例化。

Java 泛型的優點

這是 Java 泛型最重要特性的快速瀏覽。在這種情況下,「最重要」指的是「與使用泛型的所有其他語言程式設計師相比更重要」。但主要目標是對泛型有足夠的了解,以利用其他程式編寫的泛型。換句話說;我們必須充分理解泛型才能讓編譯器接受我們的程式而不會出現警告或錯誤。如果Java接受我們的程序,通用機制應該確保它的型別安全。

型別安全是優點之一,它只能儲存一種類型的物件。它不允許存放其他物品。我們可以儲存任何沒有泛型的物件。

當物件不需要型別轉換時,就沒有必要使用型別轉換。

在我們使用泛型之前,我們必須先對值進行型別轉換,然後它應該執行泛型。

編譯時檢查可確保執行時不會出現問題。根據優秀的程式方法,在編譯時處理問題比在執行時間處理問題好得多。

範例#1

代碼:

import java.util.*;
class TestGenerics1{
public static void main(String args[]){
ArrayList<String> lst=new ArrayList<String>();
lst.add("January is the first month ");
lst.add("February is the second month");
lst.add("March is the third month");
lst.add("April is the fourth month");
lst.add("May is the fifth month");
lst.add("June is the sixth month ");
lst.add("July is the seventh month");
lst.add("August is the eigth month");
lst.add("September is the ninth month");
lst.add("October is the tenth month");
lst.add("November is the eleventh month");
lst.add("December is the twelth month");
String str=lst.get(7);
System.out.println("Welcome To My Domain its the first example: "+str);
Iterator<String> iterators=lst.iterator();
while(iterators.hasNext()){
System.out.println(iterators.next());
}
}
}

輸出:

Java 集合泛型

In the above example, we used Array List collection classes in the generics. With the help of add() method we can add n number of input values and get() method to retrieve the values.

Example #2

Code:

import java.util.*;
class TestGenerics1{
public static void main(String args[]){
Map<Integer,String> first=new HashMap<Integer,String>();
first.put(1,"Tv is the electronic device");
first.put(2,"Laptop is the electronic device");
first.put(3,"Computer is the electronic device");
first.put(4,"Mobile is the electronic device");
first.put(5,"Tablet is the electronic device");
Set<Map.Entry<Integer,String>> second=first.entrySet();
Iterator<Map.Entry<Integer,String>> iterators=second.iterator();
while(iterators.hasNext()){
Map.Entry<Integer, String>result=iterators.next();
System.out.println(result.getKey()+" "+result.getValue());
}
}}

Output:

Java 集合泛型

In the above example, we used generic collections by using the Map interface. It contains some default and customized methods for achieving the operations. With the help of iterators, the values are listed on the loop.

Conclusion

Generally, it makes the programmer’s job easier and less error-prone when we use Java Generics. It’s a powerful addition to the Java language. It provides any correctness at compile time and, more crucially, implements generic algorithms without adding overhead to the Java programmers compared to the other language.

以上是Java 集合泛型的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java 集合反向下一篇:Java 集合反向