集合的基本操作

  1. 添加数据
import java.util.ArrayList;
import java.util.Collection;


public class Demo3 {
	public static void main(String[] args) {
		Collection col1 = new ArrayList<>();
		Collection col2 = new ArrayList<>();
		//1.添加数据
		col1.add(10);
		col1.add(new String("hello"));
		col2.addAll(col1);
		
		System.out.println(col1);
		System.out.println(col2);
	}

}

运行结果:
集合的基本操作

  1. 删除元素
//2.删除
		col1.remove(10);

运行结果:
集合的基本操作

//删除与col2集合中相同的元素
		col1.removeAll(col2);

运行结果:
集合的基本操作

//删除与col2集合中不相同的元素,如果有删除则返回true
		boolean retainAll = col1.retainAll(col2);

运行结果:
集合的基本操作

//删除与col2集合中不相同的元素,如果有删除则返回true
		boolean retainAll = col2.retainAll(col1);
		
		System.out.println(col1);
		System.out.println(col2);
		System.out.println(retainAll);

运行结果:
集合的基本操作

  1. 清空
//清空
		col1.clear();

运行结果:
集合的基本操作

  1. 查询
	//查询
		//查询集合中元素的个数
		int size = col1.size();
		System.out.println(size);
		//查询(打印)集合中所有的元素
		System.out.println(col1);

运行结果:
集合的基本操作

  1. contains
//4.contains:查询集合中是否包含某个元素
		boolean contains = col1.contains("hello");
		System.out.println(contains);

运行结果:
集合的基本操作

	boolean containsAll = col1.containsAll(col2);
		System.out.println(containsAll);

运行结果:
集合的基本操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值