集合是java中提供的一种容器,可以用来存储多个数据。
集合和数组既然都是容器,有啥区别呢?
-
数组的长度是固定的。集合的长度是可变的。
-
数组中可以存储基本数据类型值,也可以存储对象,而集合中只能存储对象
数组: (1)可以装基本数据类型的数据,也可以装对象 (2)数组的大小是固定的,除非你手动进行扩容、复制,即原有的数组是不能更改大小,只能通过新数组来实现扩容等操作。 (3)数组的元素是连续存储,在内存中会开辟一整块连续的存储空间,那么它的访问速度是很高,可以根据数组名[下标]直接定位到某个元素。 (4)数组结构比较单一 集合: (1)只能装对象,不能装基本数据的值。当把基本数据类型的值放到集合中时,会自动装箱为对象。 (2)集合对于使用者来说,是可以自动扩容,不用使用者去做任何的额外的操作。相对来说,比较简单。 (3)集合元素有的是连续存储的,有的不是连续存储,因为底层有的也是数组,有的底层是链表等。 如果底层不是数组的话,元素的访问效率就不高了。 (4)集合的种类非常多,有ArrayList、HashSet、TreeSet等。
集合主要分为两大系列:Collection和Map,Collection 表示一组对象,Map表示一组映射关系或键值对。
1.Collection基本特性
Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。
JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List、Queue)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。
2.基本方法使用
Collection<E>是所有单列集合的父接口,因此在Collection中定义了单列集合(List和Set)通用的一些方法,这些方法可用于操作所有的单列集合。方法如下:
2.1添加
(1)add(Object obj):添加元素对象到当前集合中 (2)addAll(Collection other):添加other集合中的所有元素对象到当前集合中,即this = this ∪ other
@Test
public void test01(){
//创建一个容器对象
/*
Collection是一个接口,不能直接创建对象,必须创建实现类的对象。ArrayList是Collection接口的实现类之一。
这里左边写Collection的目的是为了让我们更加关注Collection接口中的方法,
因为编译时看左边,只能调用左边Collection里面声明的方法,ArrayList增加的方法编译时就看不到了。
*/
Collection coll = new ArrayList();
coll.add("hello");
coll.add("world");
coll.add("html");
coll.add("mysql");
coll.add("bigdata");
coll.add("lv-tinghu");
System.out.println(coll);//[hello, world, html, mysql, bigdata, lv-tinghu]
}
2.2删除
(1) boolean remove(Object obj) :从当前集合中删除第一个找到的与obj对象equals返回true的元素。
(2)boolean removeAll(Collection coll):从当前集合中删除所有与coll集合中相同的元素。
即this = this - this ∩ coll
(3)boolean removeIf(Predicate filter) :删除满足给定条件的此集合的所有元素。
(4)boolean retainAll(Collection coll):从当前集合中删除两个集合中不同的元素,使得当前集合仅保留与coll集合中的元素相同的元素,即当前集合中仅保留两个集合的交集,即this = this ∩ coll;
(5)clear():清空集合
@Test
public void test04() {
Collection coll = new ArrayList();
coll.add("hello");
coll.add("world");
coll.add("html");
coll.add("mysql");
coll.add("bigdata");
Collection other = new ArrayList();
other.add("hello");
other.add("world");
other.add("mysql");
other.add("hi");
coll.retainAll(other);
System.out.println(coll);//[hello, world, mysql]
}
@Test
public void test03(){
Collection coll = new ArrayList();
coll.add("hello");
coll.add("world");
coll.add("html");
coll.add("mysql");
coll.add("bigdata");
//删除world
// coll.remove("world");
// System.out.println(coll);
Collection other = new ArrayList();
other.add("hello");
other.add("world");
other.add("mysql");
coll.removeAll(other);
System.out.println(coll);//[html, bigdata]
}
2.3查询
(1)boolean isEmpty():判断当前集合是否为空集合。 (2)boolean contains(Object obj):判断当前集合中是否存在一个与obj对象equals返回true的元素。 (3)boolean containsAll(Collection c):判断c集合中的元素是否在当前集合中都存在。即c集合是否是当前集合的“子集”。 (4)int size():获取当前集合中实际存储的元素个数 (5)Object[] toArray():返回包含当前集合中所有元素的数组
public void test06() {
Collection coll = new ArrayList();
coll.add("hello");
coll.add("world");
coll.add("html");
coll.add("mysql");
coll.add("bigdata");
System.out.println(coll.contains("atguigu"));
System.out.println(coll.contains("hello"));
Collection other = new ArrayList();
other.add("hello");
other.add("world");
other.add("mysql");
other.add("hi");
System.out.println(coll.containsAll(other));//false
}