首页  >  文章  >  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