import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
public class CollectionTest1 {
@Test
public void test1() {
Collection coll=new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
boolean contains=coll.contains(123);
System.out.println(contains);
System.out.println(coll.contains(new String("Tom")));
System.out.println(coll.contains(new Person("Jerry",20)));
Collection coll1=Arrays.asList(123,456);
System.out.println(coll.containsAll(coll1));
}
@Test
public void test2() {
Collection coll=new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
coll.remove(1234);
System.out.println(coll);
coll.remove(new Person("Jerry",20));
System.out.println(coll);
Collection coll1=Arrays.asList(123,456);
coll.removeAll(coll1);
System.out.println(coll);
}
@Test
public void test3() {
Collection coll=new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
Collection coll1=new ArrayList();
coll1.add(456);
coll1.add(123);
coll1.add(new Person("Jerry",20));
coll1.add(new String ("Tome"));
coll.add(false);
System.out.println(coll.equals(coll1));
}
@Test
public void test4() {
Collection coll=new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
System.out.println(coll.hashCode());
Object[] arr=coll.toArray();
for(int i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
List<String> list=Arrays.asList(new String[] {"AA","BB","CC"});
System.out.println(list);
List arr1=Arrays.asList(new int[] {123,456});
System.out.println(arr1.size());
List arr2=Arrays.asList(new Integer[] {123,456});
System.out.println(arr2.size());
}
}
-------------------------------------------------------------------------------------------------------
public class Person {
private String name;
private int age;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
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;
Person other = (Person) obj;
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 Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person() {
super();
}
}