首页 新闻 会员 周边

Java 基础 Collection

0
[已解决问题] 解决于 2018-03-06 09:13

public static void main(String[] args) {

Collection<String> col = new ArrayList<String>();
Collection<String> cols = new ArrayList<String>();

col.add("a");
col.add("b");
col.add("c");
cols.addAll(col);
cols.remove("b");
cols.clear();

System.out.println("col:"+col.toString());
System.out.println("cols:"+cols.toString());

System.out.println(col.contains(cols));

boolean contains = col.containsAll(cols);

if (contains) {

System.out.println("Set集合包含List集合的内容"+contains);

} else {

System.out.println("Set集合不包含List集合的内容"+contains);

}



}

wpw的主页 wpw | 初学一级 | 园豆:26
提问于:2018-03-05 16:00

什么问题?

BUTTERAPPLE 6年前

@BUTTERAPPLE: 为什么 结果是 true

wpw 6年前
< >
分享
最佳答案
0

哈哈,去本地模拟了一下这个程序。也是有点懵的,然后去看了看这个方法的Api

Returns true if this collection contains all of the elements in the specified collection.

因为 cols 为空啊,所以 col 肯定包含了cols 中的全部,明白了吗?

ContainsAll List Java

奖励园豆:5
BUTTERAPPLE | 老鸟四级 |园豆:3190 | 2018-03-05 17:27

谢谢 龇牙

wpw | 园豆:26 (初学一级) | 2018-03-06 09:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册