泛型基础(一)
程序员文章站
2022-07-15 21:00:26
...
GenericFoo
package org.wp.activity;
public class GenericFoo<T> {
private T foo;
public T getFoo() {
return foo;
}
public void setFoo(T foo) {
this.foo = foo;
}
public static void main(String args[]) {
GenericFoo<Boolean> foo1 = new GenericFoo<Boolean>();
GenericFoo<Integer> foo2 = new GenericFoo<Integer>();
GenericFoo foo3 = new GenericFoo();
foo1.setFoo(new Boolean(false));
foo2.setFoo(new Integer(3));
foo3.setFoo("hello");
Boolean b = foo1.getFoo();
Integer i = foo2.getFoo();
String s = (String) foo3.getFoo();
System.out.println(b);
System.out.println(i);
System.out.println(s);
}
}
Generic
package org.wp.activity;
public class Generic<T1, T2> {
private T1 foo1;
private T2 foo2;
public T1 getFoo1() {
return foo1;
}
public void setFoo1(T1 foo1) {
this.foo1 = foo1;
}
public T2 getFoo2() {
return foo2;
}
public void setFoo2(T2 foo2) {
this.foo2 = foo2;
}
public static void main(String args[]) {
Generic<Integer, Boolean> foo = new Generic<Integer, Boolean>();
foo.setFoo1(new Integer(-20));
foo.setFoo2(new Boolean(false));
System.out.println(foo.getFoo1());
System.out.println(foo.getFoo2());
}
}
GenericArray
package org.wp.activity;
public class GenericArray<T> {
private T[] fooArray;
public T[] getFooArray() {
return fooArray;
}
public void setFooArray(T[] fooArray) {
this.fooArray = fooArray;
}
public static void main(String args[]) {
GenericArray<String> foo = new GenericArray<String>();
String[] str1 = { "hello", "world", "welcome" };
String[] str2 = null;
foo.setFooArray(str1);
str2 = foo.getFooArray();
for (String s : str2) {
System.out.println(s);
}
}
}
SimpleCollection
package org.wp.activity;
public class SimpleCollection<T> {
private T[] objArr = null;
private int index = 0;
public SimpleCollection() {
objArr = (T[]) new Object[10];
}
public SimpleCollection(int capacity) {
objArr = (T[]) new Object[capacity];
}
public void add(T t) {
objArr[index++] = t;
}
public T get(int i) {
return objArr[i];
}
public int getLength() {
return this.index;
}
public static void main(String args[]) {
SimpleCollection<Integer> c = new SimpleCollection<Integer>();
for (int i = 0; i < 10; i++) {
c.add(new Integer(i));
}
for (int i = 0; i < c.getLength(); i++) {
Integer j = c.get(i);
System.out.println(j);
}
}
}
WrapperFoo
package org.wp.activity;
public class WrapperFoo<T> {
private GenericFo<T> foo;
public GenericFo<T> getFoo() {
return foo;
}
public void setFoo(GenericFo<T> foo) {
this.foo = foo;
}
public static void main(String args[]) {
GenericFo<Integer> foo = new GenericFo<Integer>();
foo.setFoo(new Integer(-10));
WrapperFoo<Integer> wrapper = new WrapperFoo<Integer>();
wrapper.setFoo(foo);
GenericFo<Integer> gf = wrapper.getFoo();
System.out.println(gf.getFoo());
}
}
class GenericFo<T> {
private T foo;
public T getFoo() {
return foo;
}
public void setFoo(T foo) {
this.foo = foo;
}
}
ArrayListTest
package org.wp.activity;
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");
list.add("e");
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);
}
}
}
SetTest
package org.wp.activity;
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);
}
Set<People> setpeo = new HashSet<People>();
setpeo.add(new People("zhangsan", 20, "beijing"));
setpeo.add(new People("lisi", 30, "shanghai"));
setpeo.add(new People("wangwu", 40, "tianjin"));
for (Iterator<People> iter = setpeo.iterator(); iter.hasNext();) {
People people = iter.next();
String name = people.getName();
int age = people.getAge();
String address = people.getAddress();
System.out.println("People [name=" + name + ", address=" + address + ", age=" + age + "]");
}
}
}
class People {
private String name;
private int age;
private String address;
public People(String name, int 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;
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;
People other = (People) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (age != 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
MapTest
package org.wp.activity;
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("a", "aa");
map.put("b", "bb");
map.put("c", "cc");
map.put("d", "dd");
map.put("e", "ee");
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(value);
}
System.out.println("=====================================");
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Iterator<Map.Entry<String, String>> iter = entrySet.iterator(); iter.hasNext();) {
Map.Entry<String, String> entry = iter.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println("key:" + key + ",value:" + value);
}
}
}
上一篇: Java泛型---泛型的嵌套
下一篇: JAVA 泛型(一)