package com.btp.t4;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import org.junit.Test;
/*
* 1.存储对象:①数组②集合
* 2.数组存储对象的特点:Student[] stu=new Student[20];stu[0]=new Student[];...
* >弊端:①一旦创建,其长度不可变 ②真实的数组存放的对象的个数是不可知的
* 3.集合
* Collection接口
* --------List接口:存储有序的,可以重复的元素
* --------ArrayList(主要的实现类),LinkedList(适合频繁的插入,删除操作),Vector(古老的,线程安全的)
* --------Set接口:存储无序的,不可重复的元素
* --------HashSet,LinkedHashSet,TreeSet
* Map接口:存储“键-值”对的数据
* ---------HashMap,LinkedHashMap,TreeMap,Hashtable(子类-Properties)
*
* 下面是Collection接口常用的方法
*/
public class TestCollection {
@Test
public void testCollection1(){
Collection coll=new ArrayList();
//size():返回集合中元素的个数
System.out.println(coll.size());
//2.add(Object obj);向集合中添加对象
coll.add(123);
coll.add("AA");
coll.add(new Date());
coll.add("BB");
System.out.println(coll.size());
//3.addAll(Collection coll):将形参coll中包含的所有元素添加到当前集合中
Collection coll1=Arrays.asList("hehe","12",new Date());
System.out.println(coll1.size());
coll.addAll(coll1);
//查看集合元素
System.out.println(coll);
System.out.println(coll.size());
//4.isEmpty();判断集合是否为空
System.out.println(coll.isEmpty());
//5.clear():清空集合元素
coll.clear();
System.out.println(coll.isEmpty());
}
@Test
public void testCollection2(){
Collection coll=new ArrayList();
coll.add(123);
coll.add("AA");
coll.add(new Date());
coll.add("BB");
//6.contains(Object obj);判断集合中是否包含指定的obj元素。如果包含,返回true。没有包含,返回false
//判断的依据:根据元素所在类的equals()方法进行判断
//明确:如果存入集合中的元素是自定义类的对象。要求:自定义的类要重写equals()方法
boolean b1=coll.contains("AA");
System.out.println(b1);
Person p=new Person("xiaohei",15);
// coll.add(p);
// b1=coll.contains(p);
// System.out.println(b1);//true
coll.add(new Person("xiaohei",15));
b1=coll.contains(p);
System.out.println(b1);//false,因为引用不同
//要使得内容相同,就返回true,必须要重写equals()方法
//7.containsAll(Collection coll)判断当前集合中是否包含coll中所有的元素
Collection coll1=Arrays.asList(123,new String("AA"));
b1=coll.containsAll(coll1);
System.out.println("#"+b1);
//8.retainAll(Collection coll);求当前集合和coll的共有的元素,返回给当前集合
coll.retainAll(coll1);
System.out.println(coll);
//9.remove(Object obj);删除集合中的obj元素。若删除成功,发挥true,否则,返回false
boolean b2=coll.remove("BB");
System.out.println(b2);
}
@Test
public void testCollection3(){
Collection coll=new ArrayList();
coll.add(123);
coll.add("AA");
coll.add(new Date());
coll.add("BB");
Collection coll1=Arrays.asList(123,new String("AA"));
//10.removeAll(Collection coll);从当前集合中删除coll包含的元素
coll.removeAll(coll1);
System.out.println(coll);
//equals(Collection coll);判断当前集合中的所有元素是否和coll相等
System.out.println(coll.equals(coll1));
//12.hashCode();
System.out.println(coll.hashCode());
//13.toArray();将集合转换为数组
Object[] obj=coll.toArray();
for(int i=0;i<obj.length;i++)
{
System.out.println(obj[i]);
}
System.out.println();
//14.iterator();返回一个Iterator接口实现类的对象,进而实现集合的遍历
Iterator I=coll.iterator();
//方式一
for(int i=0;i<coll.size();i++)
System.out.println(I.next());
System.out.println();
//方式二
while(I.hasNext()){
System.out.println(I.next());
}
}
}
class Person{
String name;
int age;
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;
}
@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;
}
}