package day8_15;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class TestCollection {
public static void main(String[] args) {
//创建集合
Collection<String> c = new ArrayList<>();
//集合是否为空,元素个数为0则为true
System.out.println(c.isEmpty());
//添加
c.add("aa");
c.add("bb");
c.add("cc");
System.out.println(c);
/*for(String s:c) {
System.out.println(s);
}*/
//集合中元素的个数
System.out.println(c.size());
//
Collection<String> c1 = new ArrayList<>();
c1.add("ee");
c1.add("ff");
System.out.println(c1);
//把参数集合c1中的所有元素添加到当前集合c中,只要当前集合发生了改变true
c.addAll(c1);
System.out.println(c);
//删除指定参数的元素,只要当前集合发生了改变true
c.remove("aa");
System.out.println(c);
//删除参数几个钟的所有元素,只要当前集合发生了改变true
c.removeAll(c1);
System.out.println(c);
//[bb, cc, eeee, fffffffffff]
c.add("eeee");
c.add("fffffffffff");
System.out.println(c);
//按照条件删除[bb, cc]
/*c.removeIf(new Predicate<String>() {
@Override
public boolean test(String t) {
return t.length()>2;
}
});*/
c.removeIf(s->s.length()>2);
System.out.println(c);
//是否存在参数指定的元素,存在true
System.out.println(c.contains("cc"));//true
//是否存在参数结合中的全部元素,都存在true
System.out.println(c.containsAll(c1));//false
// String[] strs = new String[] {"fasfsadf","sadfsa","sdfsag"};
//把数组转换成集合 Arraylist是Arrays的静态内部类
//不能添加,移除元素
List<String> list = Arrays.asList(new String[] {"ee","ff"});
// list.add("sda");
System.out.println(list);
c.addAll(list);
System.out.println(c);
System.out.println(c.containsAll(c1));//true
//集合转数组
// Object [] obj = c.toArray();
Object [] obj = c.toArray(new Object[0]);//数组扩容
for(Object o:obj) {
System.out.println(o);
}
String [] str = c.toArray(new String[4]);
//清空集合中的所有元素
c.clear();
System.out.println(c.isEmpty());
}
}