天天看点

java非泛型集合_Java非泛型与泛型集合

与非泛型代码相比,使用泛型的程序有很多好处。

代码重用:使用泛型,只需编写一次方法/类/接口一次,即可用于任何类型。 在非泛型中,需要在需要时一次又一次地编写代码。

类型安全性:与在运行时相比,泛型在显示编译时会出错(最好在编译时知道代码中的问题,而不是使代码在运行时失败)。

例如:创建一个存储学生姓名的ArrayList,如果程序员错误地添加了一个整数对象而不是字符串,则编译器允许它。 但是,当从ArrayList检索此数据时,它将在运行时导致非通用ArrayList出现问题。

// A Simple Java program to demonstrate that NOT using

// generics can cause run time exceptions

import java.util.*;

class Test {

public static void main(String[] args)

{

// Creating an ArrayList without any type specified

ArrayList al = new ArrayList();

al.add("One");

al.add("Two");

al.add(10); // Compiler allows this

String s1 = (String)al.get(0);

String s2 = (String)al.get(1);

try {

// Causes Runtime Exception

String s3 = (String)al.get(2);

}

catch (Exception e) {

System.out.println("Exception: " + e);

}

}

}

运行结果如下:

Exception:

java.lang.ClassCastException:

java.lang.Integer cannot be cast to java.lang.String

泛型如何解决此问题:如果将此列表设为泛型,则在任何其他情况下,它将仅使用String对象并引发编译时错误。

// Using generics converts run time exceptions into

// compile time errors

import java.util.*;

class Test {

public static void main(String[] args)

{

// Creating an ArrayList with String specified

ArrayList al = new ArrayList();

al.add("Sachin");

al.add("Rahul");

// Now Compiler doesn't allow this

al.add(10);

String s1 = al.get(0);

String s2 = al.get(1);

String s3 = al.get(2);

}

}

运行结果时间:

15: error: no suitable method found for add(int)

al.add(10);

^

不需要单独的类型转换:如果不需要泛型,则在上面的示例中,每次要从ArrayList检索数据时,都需要进行类型转换。 在每个检索操作中进行类型转换都是一个很大的麻烦。 如果已经通过某种方式知道列表仅保存字符串数据,则可以避免这种情况。

例如:

// A Simple Java program to demonstrate that

// type casting is needed everytime in Non-Generic

import java.util.*;

class Test {

public static void main(String[] args)

{

// Creating an ArrayList without any type specified

ArrayList al = new ArrayList();

al.add("Sachin");

al.add("Rahul");

// For every retrieval,

// it needs to be casted to String for use

String s1 = (String)al.get(0);

String s2 = (String)al.get(1);

}

}

泛型如何解决此问题:如果将此列表设为泛型,则它将仅使用String对象,并且在检索时仅返回String对象。 因此,将不需要单独的类型转换。

// A Simple Java program to demonstrate that

// type casting is not needed in Generic

import java.util.*;

class Test {

public static void main(String[] args)

{

// Creating an ArrayList with String specified

ArrayList al = new ArrayList();

al.add("One");

al.add("Two");

// Retrieval can be easily

// without the trouble of casting

String s1 = al.get(0);

String s2 = al.get(1);

}

}

实现泛型算法:通过使用泛型,人们可以实现适用于不同类型对象的算法,并且它们同样是类型安全的。

泛型和非泛型的区别对比表如下:

比较项

非一般集合

一般集合

语法

ArrayList list = new ArrayList();

ArrayList list = new ArrayList();

类型安全

可以保存任何类型的数据。 因此不是类型安全的。

只能保存定义的数据类型。 因此输入安全。

类型转换

每次检索都需要进行个体类型转换。

无需类型转换。

编译时检查

在运行时检查类型安全。

在编译时检查类型安全性。

欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果。

转载请注明:文章转载自 有区别网 [http://www.hasdiffer.com]

本文标题:Java非泛型与泛型集合的区别

本文链接:http://www.vsdiffer.com/non-generic-vs-generic-collection-in-java.html

免责声明:本站部分内容除注明转载外,均为本站网站用户投稿或互联网整理。对于该内容的正确性如何,本站不负任何责任。同时,如本网站内容无意之中冒犯了您的权益,请联系站长,邮箱:1478761107#qq.com(使用@代替#),我们核实并会尽快处理。

相关主题

随机