集合01 根接口Collection<E>

根接口的方法

Collection<String> collection = new ArrayList<>();
/**
 * 返回集合中元素的数量
 */
int size = collection.size();
/**
 * 判断集合是否为空
 */
boolean isEmpty = collection.isEmpty();
/**
 * 判断集合是否包含指定元素
 */
boolean isContain = collection.contains("jack");
/**
 * 返回集合的迭代器,用于遍历集合
 */
Iterator<String> iterator = collection.iterator();
/**
 * 集合转数组
 */
Object[] array = collection.toArray();
/**
 * 集合转数组,并指定类型
 * 1、当数组参数长度 < collection的长度,数组会自动扩容,大小和collection的元素个数一致
 * 2、当数组参数长度 = collection的长度,直接将元素放入参数数组中
 * 3、当数组参数长度 > collection的长度,直接将元素放入参数数组中,多余的空位用null填充
 */
String[] strArray = collection.toArray(new String[0]);
/**
 * 新增元素
 */
boolean add = collection.add("amy");
/**
 * 移除指定元素
 */
boolean remove = collection.remove("jack");
/**
 * 判断collection是否包含参数集合中的所有元素
 */
List<String> list = Arrays.asList("amy");
boolean containsAll = collection.containsAll(list);
/**
 * 将指定集合中的元素添加到collection中
 */
boolean addAll = collection.addAll(list);
/**
 * 删除collection中包含指定集合中的元素
 */
boolean removeAll = collection.removeAll(list);
/**
 * 按照一定规则过滤集合中的元素
 */
Collection<Person> persons = new ArrayList<>();
persons.add(new Person("张三", 18, "F"));
persons.add(new Person("李四", 38, "F"));
persons.add(new Person("王五", 28, "F"));
boolean removeIf = persons.removeIf(person -> person.getAge()>=30);
/**
 * 删除collection中包含但指定集合list中不包含的所有元素。
 */
boolean retainAll = collection.retainAll(list);
/**
 * 清空集合
 */
collection.clear();
/**
 * 比较指定对象与集合是否相等
 */
boolean equals = collection.equals(list);

/**
 * 返回集合的hashCode值
 */
int hashCode = collection.hashCode();
/**
 * 创建一个分割迭代器
 */
Spliterator<String> spliterator = collection.spliterator();
/**
 * 创建stream流
 */
Stream<String> stream = collection.stream();
/**
 * 创建一个并行stream流
 */
Stream<String> parallelStream = collection.parallelStream();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值