泛型是JDK1.5中一个最重要的特征。通过引入泛型,我们将获得编译时类型的安全和运行时更小地抛出ClassCastException的可能。
在JDK1.5中,你可以声明一个集合将接收/返回的对象的类型。
泛型特点:只要编译的时候没有问题,执行的时候肯定没有问题。
泛型之前:
类别定义时的逻辑完全一样,只是里面成员变量的类型不同。
如果需要多个相似的类,需要定义多个文件,不同的只是变量的类型,而逻辑是完全一样的。
以后使用集合的时候必须要使用泛型(可以避免很多强制类型转换)
迭代器迭代过程中迭代器也要加上泛型
以下是各种
集合类在遍历的时候使用泛型的标准写法,以后把这个作为标准都这么写:
class="java" name="code">package com.shengshiyuan4;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ArrayListTest {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
for (int i = 0; i < list.size(); i++) {
String value = list.get(i);
System.out.println(value);
}
for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
String value = iter.next();
System.out.println(value);
}
}
}
package com.shengshiyuan4;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SetTest {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
set.add("aa");
set.add("bb");
set.add("cc");
for (Iterator<String> iter = set.iterator(); iter.hasNext();) {
String value = iter.next();
System.out.println(value);
}
System.out.println("--------------------------------");
Set<People> set2 = new HashSet<People>();
set2.add(new People("zhangsan", 20, "beijing"));
set2.add(new People("lishi", 30, "shanghai"));
set2.add(new People("wangwu", 40, "guangzhou"));
for (Iterator<People> iter = set2.iterator(); iter.hasNext();) {
People people = iter.next();
String name = people.getName();
String address = people.getAddress();
int age = people.getAge().intValue();
System.out.println(name + "," + age + "," + address);
}
}
}
class People {
private String name;
private Integer age;
private String address;
public People(String name, Integer age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final People other = (People) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.shengshiyuan4;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapTest {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "aa");
map.put("2", "bb");
map.put("3", "cc");
Set<String> set = map.keySet();
for (Iterator<String> iter = set.iterator(); iter.hasNext();) {
String key = iter.next();
String value = map.get(key);
System.out.println(key + ";" + value);
}
Set<Map.Entry<String, String>> set2 = map.entrySet();
for (Iterator<Map.Entry<String, String>> iter = set2.iterator(); iter
.hasNext();) {
Map.Entry<String, String> entry = iter.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ";" + value);
}
}
}