package test4;
import org.junit.Test;
import test2.Person;
import java.util.*;
/**
* 〈一句话功能简述〉<br>
* 〈collection接口的常用方法〉
*
* @author abu
* @create 2019/7/20
* @since 1.0.0
*/
public class TestCollection {
@Test
public void testCollection3(){
Collection coll = new ArrayList();
coll.add(123);
coll.add("新吧唧");
coll.add(new Date());
coll.add("卡古拉酱~");
coll.add(new Person("夏目",18));
Collection coll1 = new ArrayList();
coll1.add("新吧唧");
coll1.add(123);
coll1.add(345);
coll1.add(new Person("夏目",18));
//removeAll(collection coll);从当前集合中删除包含在coll中的元素(相当于差集)
coll.removeAll(coll1);
System.out.println(coll);
//判断两个集合所有元素是否完全相等
Collection coll2 = new ArrayList();
coll2.add("新吧唧");
coll2.add(123);
coll2.add(345);
//coll2.add(new Person("夏目",18));
System.out.println(coll1.equals(coll2));
System.out.println(coll.hashCode());
//toArray() 将集合转化为数组
Object[] obj = coll.toArray();
for (int i = 0; i < obj.length; i ++){
System.out.println(obj[i] + "#");
}
//将数组转化为集合
Collection coll4 = Arrays.asList("银桑","is a ","charming man");
System.out.println(coll4);
//iterator();返回一个Iterator接口实现类的对象,进而实现集合的遍历
Iterator iterator = coll4.iterator();
/*System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());*/
/* for(int i = 0; i < coll4.size(); i ++){
System.out.println(iterator.next() + "&");
}*/
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testCollection2(){
Collection coll = new ArrayList();
System.out.println(coll.size());
coll.add(123);
coll.add("新吧唧");
coll.add(new Date());
coll.add("卡古拉酱~");
//判断集合中是否包含指定的obj元素
//判断依据:元素所在类的equals()方法判断
//若存入集合的元素是自定义类的对象,则该自定义类要重写equals()方法
boolean b1 = coll.contains("新吧唧");
System.out.println(b1);
Person p = new Person("猫咪森赛",89);
coll.add(p);
System.out.println(coll);
//
coll.add(new Person("夏目",18));
boolean b2 = coll.contains(new Person("夏目",18));
System.out.println(b2);
//containsAll(coll):判断当前集合中是否包含coll1中所有元素
Collection coll1 = new ArrayList();
coll1.add("新吧唧");
coll1.add(123);
//coll1.add(new Date());这个不得行,不知道什么原因
boolean b3 = coll.containsAll(coll1);
System.out.println(coll1);
System.out.println("#" + b3);
//retainAll(coll):求当前集合与coll的共有元素,并返回给当前集合
coll.retainAll(coll1);
System.out.println(coll);
//remove(Object obj):删除集合中的obj元素,删除成功返回true,否则返回false。
boolean b4 = coll.remove("新吧唧");
System.out.println(coll);
System.out.println(b4);
}
@Test
public void testCollection1(){
Collection coll = new ArrayList();
//返回集合中元素的个数
System.out.println(coll.size());
//向集合中添加元素
((ArrayList) coll).add(123);
((ArrayList) coll).add("新吧唧");
((ArrayList) coll).add(new Date());
((ArrayList) coll).add("卡古拉酱~");
System.out.println(coll.size());
//将形参coll中的所有元素添加到当前集合
Collection coll1 = Arrays.asList("银桑","is a ","charming man");
((ArrayList) coll).addAll(coll1);
System.out.println(coll);
System.out.println(coll.size());
//判断集合是否为空
System.out.println(coll.isEmpty());
//清空集合元素
coll.clear();
System.out.println(coll.isEmpty());
}
}
test16——collection接口的常用方法
最新推荐文章于 2022-08-19 19:03:56 发布