Collection集合常用的功能

package demo06;

import java.util.ArrayList;
import java.util.Collection;

/*
* java.util
接口 Collection<E>
*
* 所有单列集合的最顶层接口,里面定义了所有单列集合共性的方法
* 任意的单列集合都可以使用Collection接口中的方法
* 共性方法
* boolean add(E e)
确保此 collection 包含指定的元素(可选操作)。
*
*
*
void clear()
移除此 collection 中的所有元素(可选操作)。
* */
public class demo06{
public static void main(String[] args) {
//创建一个集合对象 使用多态
Collection<String>coll=new ArrayList<>();
System.out.println(coll);//打印了空 重写了toString方法

/*
* public boolean add(E e) 把给定的对象添加到当前集合中;
* 返回值是一个boolean值,一般都返回true,所以可以不用接收;
* */
boolean b1=coll.add("张三");
System.out.println("b1"+b1);//b1true
System.out.println(coll);

// public boolean remove(E e);存在则删除返回true
boolean b2=coll.remove("张三");
System.out.println(coll);

}
}

猜你喜欢

转载自www.cnblogs.com/Damocless/p/11875854.html