package com.lee2;
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>();
foo1.setFoo(new Boolean(false));
foo2.setFoo(new Integer(3));
Boolean b = foo1.getFoo();
Integer i = foo2.getFoo();
System.out.println(b);
System.out.println(i);
}
}
package com.lee2;
public class Generic2<T>
{
private T [] fooArray;
public T[] getFooArray()
{
return fooArray;
}
public void setFooArray(T[] fooArray)
{
this.fooArray = fooArray;
}
public static void main(String[] args)
{
Generic2<String> foo = new Generic2<String>();
String [] str1 = {"hello", "world", "welcome"};
String [] str2 = null;
foo.setFooArray(str1);
str2 = foo.getFooArray();
for(int i = 0; i < str2.length; i++)
{
System.out.println(str2[i]);
}
}
}
package com.lee2;
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>();
foo1.setFoo(new Boolean(false));
foo2.setFoo(new Integer(3));
Boolean b = foo1.getFoo();
Integer i = foo2.getFoo();
System.out.println(b);
System.out.println(i);
}
}
package com.lee2;
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("lisi",30 ,"shanghai"));
set2.add(new People("wangwu", 40 ,"tianjin"));
for(Iterator<People> iter = set2.iterator(); iter.hasNext();)
{
People people = iter.next();
String name = people.getName();
int age = people.getAge();
String address = people.getAddress();
System.out.println(name + "," + age + "," + address);
}
}
}
class People
{
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String getAddress()
{
return address;
}
private String name;
private int age;
private String address;
public People(String name, int age, String address)
{
this.name = name;
this.address = address;
this.age = age;
}
@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;
}
}