collection
collection中的7种方法
1.boolean add(E e)
确保此集合包含指定的元素(可选操作)。 如果此集合由于调用而更改,则返回true 。 (如果此集合不允许重复,并且已包含指定的元素,则返回false。 )
支持此操作的集合可能会限制可能添加到此集合的元素。 特别是一些集合拒绝添加null种元素,和其他人将强加可添加元素的类型限制。 收集类应在其文档中明确说明可能添加哪些元素的限制。
如果一个集合拒绝添加一个特定的元素,除了它已经包含该元素之外,它必须抛出异常(而不是返回false )。 这保留了一个集合在此调用返回后始终包含指定元素的不变量。
参数
e - 要确保在此集合中的存在的元素
结果
true如果此集合由于调用而更改
异常
UnsupportedOperationException - 如果此 集合不支持add操作
ClassCastException - 如果指定元素的类阻止将其添加到此集合
NullPointerException - 如果指定的元素为空,并且该集合不允许空元素
IllegalArgumentException - 如果元素的某些属性阻止其添加到此集合
IllegalStateException - 如果由于插入限制,此时无法添加该元素
代码:
Collection<String> coll=new ArrayList<>();//使用多态创建
System.out.println(coll);//重写了toString方法;[]
//1.boolean add(E e)
boolean b1=coll.add("乔峰");
System.out.println(b1);//true
System.out.println(coll);//[乔峰]
2.int size()
返回此集合中的元素数。 如果此收藏包含超过Integer.MAX_VALUE个元素,则返回Integer.MAX_VALUE 。
结果
此集合中的元素数
代码:
int length=coll.size();
System.out.println(length);//6
3.boolean remove(Object o)
boolean remove(Object o)从该集合中删除指定元素的单个实例(如果存在)(可选操作)。 更正式地,删除元素e ,使得(onull ? enull : o.equals(e)) ,如果该集合包含一个或多个这样的元素。 如果此集合包含指定的元素(或等效地,如果此集合由于调用而更改),则返回true 。
参数
o - 要从此集合中删除的元素(如果存在)
结果
true如果一个元素因此调用而被删除
异常
ClassCastException - 如果指定元素的类型与此集合不兼容( optional )
NullPointerException - 如果指定的元素为空,并且此集合不允许空元素( optional )
UnsupportedOperationException - 如果此 集合不支持remove操作
代码:
boolean b2=coll.remove("郭靖");
System.out.println(b2);//false
System.out.println(coll.remove("李秋水"));//true
4.boolean contains(Object o)
boolean contains(Object o)如果此集合包含指定的元素,则返回true 。 更正式地,返回true如果且仅当该集合至少包含一个元素e使得(onull ? enull : o.equals(e)) 。
参数
o - 要在此集合中存在的元素要进行测试
结果
true如果此集合包含指定的元素
异常
ClassCastException - 如果指定元素的类型与此集合不兼容( optional )
NullPointerException - 如果指定的元素为空,并且此集合不允许空元素( optional )
代码:
System.out.println(coll.contains(“特朗普”));//false
System.out.println(coll.contains(“乔峰”));//true
5.boolean isEmpty()
boolean isEmpty()如果此集合不包含元素,则返回 true 。
结果
true如果此集合不包含元素
代码:
System.out.println(coll.isEmpty());//false
6.Object[] toArray()
返回一个包含此集合中所有元素的数组。 如果此集合对其迭代器返回的元素的顺序做出任何保证,则此方法必须以相同的顺序返回元素。
返回的数组将是“安全的”,因为该集合不保留对它的引用。 (换句话说,这个方法必须分配一个新的数组,即使这个集合是由数组支持的)。 因此,调用者可以自由地修改返回的数组。
此方法充当基于阵列和基于集合的API之间的桥梁。
代码:
Object[] array = coll.toArray();
for (int i = 0; i < coll.size(); i++) {
System.out.println(array[i]);
}//乔峰 段誉 虚竹 逍遥子 天山童姥
7.void clear()
从此集合中删除所有元素(可选操作)。 此方法返回后,集合将为空。
异常
UnsupportedOperationException - 如果此 集合不支持clear操作
代码:
coll.clear();
System.out.println(coll);//[]
System.out.println(coll.isEmpty());//true
System.out.println(coll.size());//0
完整代码
public class Demo01Collection {
public static void main(String[] args) {
Collection<String> coll=new ArrayList<>();//使用多态创建
System.out.println(coll);//重写了toString方法;[]
//1.boolean add(E e)
boolean b1=coll.add("乔峰");
System.out.println(b1);//true
System.out.println(coll);//[乔峰]
coll.add("段誉");
coll.add("虚竹");
coll.add("逍遥子");
coll.add("天山童姥");
coll.add("李秋水");
// coll.add("郭靖");
System.out.println(coll);//[乔峰, 段誉, 虚竹, 逍遥子, 天山童姥, 李秋水]
//2.int size()
int length=coll.size();
System.out.println(length);//6
//3.boolean remove(Object o)
boolean b2=coll.remove("郭靖");
System.out.println(b2);//false
System.out.println(coll.remove("李秋水"));//true
//4.boolean contains(Object o)
System.out.println(coll.contains("特朗普"));//false
System.out.println(coll.contains("乔峰"));//true
//5.boolean isEmpty()
System.out.println(coll.isEmpty());//false
//6.Object[] toArray()
Object[] array = coll.toArray();
for (int i = 0; i < coll.size(); i++) {
System.out.println(array[i]);
}//乔峰 段誉 虚竹 逍遥子 天山童姥
//7.void clear()
coll.clear();
System.out.println(coll);//[]
System.out.println(coll.isEmpty());//true
System.out.println(coll.size());//0
}
}