Collection集合常用方法

方法名

说明

boolean add(E e)

添加元素

boolean remove(Object o)

从集合中移除指定的元素

void clear()

清空集合中的元素

boolean contains(Object o)

判断集合中是否存在指定的元素

boolean isEmpty()

判断集合是否为空

int size()

适合的长度,也就是中集合的个数

 

package com.itheima_72;

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

/*
boolean add(E e);添加元素
boolean remove(Object o);从集合中移除指定的元素
void clear();清空集合中的元素
boolean contains(Object o);判断集合中是否存在指定的元素
boolean isEmpty();判断集合是否为空
int size();适合的长度,也就是中集合的个数

*/
public class CollectionDemo02 {
public static void main(String[] args) {
//创建集合对象
Collection<String> c = new ArrayList<>();

//boolean add(E e);添加元素
// System.out.println(c.add("hello"));
// System.out.println(c.add("world"));
// System.out.println(c.add("world"));
c.add("hello");
c.add("world");
c.add("java");

//boolean remove(Object o);从集合中移除指定的元素
// c.remove("java");
// c.remove("javaee");
// System.out.println(c.remove("hello"));
// c.clear();
// boolean contains(Object o);判断集合中是否存在指定的元素
// c.contains("world");
c.contains("javaee");

//boolean isEmpty();判断集合是否为空
// System.out.println(c.isEmpty());
System.out.println(c.size());


//输出集合对象
System.out.println(c);
}
}