Collection
文章目录
概述
Collection 层次结构中的根接口。
Collection 表示一组对象,这些对象也称为Collection 的元素。
- 一些 collection 允许有重复的元素,而另一些则不允许。
- 一些 collection 是有序的,而另一些则是无序的。
可重复 | 不可重复 | |
---|---|---|
有序 | List(ArrayList、LinkedList、Vector) | |
无序 | Set(HashSet、LinkedHashSet、TreeSet) |
集合 VS 数组
集合 | 数组 |
---|---|
不可以存入基本类型 | 可以存入基本类型 |
长度不固定 | 长度固定 |
和数组相比效率较低 | 效率较高 |
有丰富的API | 没有api |
API
Collection接口中的方法,是子类及子接口中一定会实现(或继承的)
增
boolean add(E e)
在集合的最后添加 元素 e , 成功返回true,不成功返回false
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("world");
System.out.println(c);
c.add("java");
System.out.println(c);
}
}
boolean addAll(Collection c)
在集合的最后将 集合 c 中的全部元素放入原集合中(指调用的集合)
成功修改返回true,没有修改返回false
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
Collection c1 = new ArrayList();
c1.add("beijing");
c1.add("shanghai");
c1.add("shenzhen");
System.out.println(c.addAll(c1)); // true
System.out.println(c); // [hello, world, java, beijing, shanghai, wuhan]
System.out.println(c1); // [beijing, shanghai, wuhan]
Collection c2 = new ArrayList();
System.out.println(c.addAll(c2)); // false
System.out.println(c);//[hello, world, java]
System.out.println(c2);//[]
}
}
删
void clear()
clear() 方法只是将集合内的全部元素清空,并不是将集合置为null;
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
// void clear()
System.out.println(c);
c.clear();
System.out.println(c);
System.out.println(c == null); // false
}
}
boolean remove(Object o)
指定删除集合中的第一个和 o 相等的元素
有满足条件的元素,返回true,如果没有,则返回false
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("hello");
c.add("hello");
c.add("world");
c.add("java");
System.out.println(c);
// boolean remove(Object o)
System.out.println(c.remove("hello"));
System.out.println(c);
System.out.println(c.remove("php"));
System.out.println(c);
}
}
boolean removeAll(Collection c)
删除和集合 c 中元素相同的元素
存在返回true,不存在则返回false
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("hello");
c.add("hello");
c.add("world");
c.add("java");
Collection c1 = new ArrayList();
c1.add("beijing");
c1.add("shanghai");
c1.add("shenzhen");
System.out.println(c);
// boolean removeAll(Collection c)
System.out.println(c.removeAll(c1)); // false
System.out.println(c); // [hello, world, java]
System.out.println(c1); //[beijing, shanghai, shenzhen]
Collection c2 = new ArrayList();
c2.add("hello");
c2.add("world");
System.out.println(c.removeAll(c2)); // false
System.out.println(c); // [hello, world, java]
System.out.println(c2); //[beijing, shanghai, shenzhen]
}
}
boolean retainAll(Collection c)
仅在列表中保留指定 collection 中所包含的元素
修改返回true,否则返回false
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("hello");
c.add("hello");
c.add("world");
c.add("java");
Collection c1 = new ArrayList();
c1.add("beijing");
c1.add("shanghai");
c1.add("shenzhen");
System.out.println(c);
// boolean retainAll(Collection c)
System.out.println(c.retainAll(c1)); // true
System.out.println(c); // []
System.out.println(c1); //[beijing, shanghai, shenzhen]
c1.add("hello");
c1.add("world");
c1.add("java");
System.out.println(c.retainAll(c1)); // false
System.out.println(c); // [hello, world, java]
System.out.println(c1);
c.add("beijing");
c.add("beijing");
c.add("beijing");
c.add("shanghai");
System.out.println(c);
System.out.println(c.retainAll(c1));
System.out.println(c);
System.out.println(c1);
}
}
查
boolean contains(Object o)
boolean containsAll(Collection c)
如果集合包含指定 c 的所有元素,则返回 true。
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("hello");
c.add("hello");
c.add("world");
c.add("java");
Collection c1 = new ArrayList();
c1.add("beijing");
c1.add("shanghai");
c1.add("shenzhen");
System.out.println(c);
// boolean containsAll(Collection c)
System.out.println(c.containsAll(c1)); //false
c.add("beijing");
c.add("shanghai");
System.out.println(c.containsAll(c1)); // false
c.add("shenzhen");
System.out.println(c.containsAll(c1)); // true
c1.add("beijing");
c1.add("shanghai");
System.out.println(c);
System.out.println(c1);
System.out.println(c.containsAll(c1)); // true
}
}
获取集合的属性
boolean isEmpty()
判断集合是否为空。(并不是 null)
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("hello");
c.add("hello");
c.add("world");
c.add("java");
// boolean isEmpty(): 集合中是否有元素
System.out.println(c.isEmpty()); // false
// c = null;
// System.out.println(c.isEmpty()); // NullPointerException
c.clear();
System.out.println(c.isEmpty()); // true
}
}
int size()
返回集合的长度
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("hello");
c.add("hello");
c.add("world");
c.add("java");
// int size()
System.out.println(c);
System.out.println(c.size()); // 3
/*c = null;
System.out.println(c.size()); // NullPointerException*/
c.clear();
System.out.println(c.size()); // 0
}
}
遍历
Object[] toArray()
就是转化成数组,然后遍历,数组和集合并不相互影响
package week6;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
/**
* @author : 魏铭志
* @date : 2020/4/6 19:08
*/
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
Object[] array = c.toArray();
for (int i = 0; i < array.length; i++) {
String s = ((String) array[i]);
System.out.println(s.toUpperCase());
}
// 修改数组不会影响到集合。
array[2] = "javase";
System.out.println(Arrays.toString(array)); //
System.out.println(c);
// 修改集合也不会影响到数组.
c.add("javase");
System.out.println(Arrays.toString(array));
System.out.println(c);
}
}
Iterator iterator()
java提供的迭代器
Iterator本身是一个接口,需要子类实现
- boolean hasNext():如果仍有元素可以迭代,则返回 true。
- E next(): 返回迭代的下一个元素。
- void remove(): 删除的是最近返回的元素
注意:
在迭代器中不要对元素用集合API进行增删操作。
迭代器实现删除元素
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
Iterator it = c.iterator();
while (it.hasNext()) {
String s = (String) it.next();
if ("world".equals(s)) {
it.remove();
}
}
for( Iterator it = c.iterator();。。。)
System.out.println(c);
}
}
总结
boolean addAll(Collection c)
boolean removeAll(Collection c)
boolean retainAll(Collection c)
boolean containsAll(Collection c)
从数学的角度看这四个函数
c.iterator();。。。)
System.out.println©;
}
}
[外链图片转存中...(img-IRREhcrN-1586319449326)]
# 总结
boolean addAll(Collection c)
boolean removeAll(Collection c)
boolean retainAll(Collection c)
boolean containsAll(Collection c)
从数学的角度看这四个函数
[外链图片转存中...(img-iq3ypcTM-1586319449328)]
[外链图片转存中...(img-nQxfk7ld-1586319449329)]
[外链图片转存中...(img-XHy9OwXx-1586319449330)]
[外链图片转存中...(img-fduf7pFL-1586319449331)]