正式讲课之前,我们先来搞明白三个小问题:
-
为什么需要集合类?
很多情况下,我们需要对一组对象进行操作。而且很可能事先并不知道到底有多少个对象。为了解决这个问题呢,Java 就提供了集合类供我们使用。 -
集合类的特点
a. 只能存储引用数据类型
b. 可以自动地调整自己的大小 -
数组和集合类都是容器,它们有何不同?
a. 数组可以存储基本数据类型的数据,集合不可以。
b. 数组的长度是固定的,集合可以自动调整自己的大小。
c. 数组的效率高,相对来说集合效率比较低。
d. 数组没有API,集合有丰富的API。
鉴于以上特点,在开发中更建议使用集合。
Collection表示一种按类收集的容器
Collection接口
概述:Collection是层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。
Collection 的含义大家应该清楚了,那么它应该包含哪些 API 呢?
Collection中的equals方法和hashCode方法都应当被重写,因为我们应当比较的是一个Collection中的元素个数以及元素值是否相等甚至元素次序(如LinkedList)是否相等,而不应该比较这两个集合是否是同一个集合(不应比较这两个集合指向的内存地址是否相同)。既然equals方法重写了,hashCode方法也应当被重写。
API:
-
增:
boolean add(E e)
boolean addAll(Collection c) -
删:
void clear()
boolean remove(Object o)
boolean removeAll(Collection c)
boolean retainAll(Collection c) // retain 保留 -
查:
boolean contains(Object o) // contain 包含
boolean containsAll(Collection c) -
获取集合的属性:
boolean isEmpty() // 判断集合是否为空
int size() // 集合中元素的个数 -
遍历:
Object[] toArray() // 把集合转换成数组
()中的类型和Collection无关的API:
- boolean add(E e)
会发现ArrayList允许重复元素,HashSet不允许重复元素。这就是多态。增加成功返回true,失败返回false。 - boolean remove(Object o)
ArrayList只会删除第一个值为o的元素。删除与指定对象相等的元素,成功返回true,失败返回false。 - void clear()
- boolean contains(Object o)
包含值为o的元素,成功返回true,失败返回false。 - boolean isEmpty()
判断对象里的元素是否为空,成功返回true,失败返回false。 - int size()
返回Collection对象容纳的元素个数。
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
// boolean add(E e)
c.add("hello");
c.add("world");
c.add("java");
System.out.println(c); // [hello, world, 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
System.out.println(c.isEmpty()); // 3
c.clear();
System.out.println(c.isEmpty()); // 0
// boolean remove(Object o)
System.out.println(c.remove("world")); // true
System.out.println(c);
System.out.println(c.remove("wuhan")); // false
System.out.println(c); // [hello, world, java]
// 集合中有两个重复的元素,要怎么删除?
c.add("hello");
System.out.println(c); // [hello, world, java, hello]
System.out.println(c.remove("hello"));
System.out.println(c); // [world, java, hello]
// boolean contains(Object o)
System.out.println(c.contains("hello")); // true
System.out.println(c.contains("wuhan")); // false
}
}
()中的类型和Collection相关的API:
- boolean addAll(Collection c)
如果集合发生了改变返回true, 否则返回false - boolean removeAll(Collection c)
如果集合发生了改变返回true, 否则返回false - boolean retainAll(Collection c)
如果集合发生了改变返回true, 否则返回false - boolean containsAll(Collection c)
如果c中所有的元素都在当前集合中存在返回true, 否则返回false
集合类的特点
a. 容量可变
b. 只能存储引用数据类型
public class Demo01 {
public static void main(String[] args) {
Collection c = new ArrayList(10);
for (int i = 0; i < 30; i++) {
c.add(i); // 自动装箱
}
//Collection重写了toString方法,打印出来的是数组的值,而不是地址
System.out.println(c);
System.out.println(c.size());
}
}
我们讲,既然集合类只能存储引用数据类型的数据,为什么for循环中是int类型的数据呢?
因为自动装箱拆箱。
提供基本数据类型是为了提高性能的,因为数据很简单。但在java中奉行一种原理:一切数据皆对象。所以提供了基本数据类型对应的引用数据类型。
JDK5特性:自动装箱 和 自动拆箱
自动装箱:基本数据类型转换成对应的引用数据类型
自动拆箱:引用数据类型转换成对应的基本数据类型
基本数据类型 引用数据类型
char —> Character
byte —> Byte
short —> Short
int —> Integer
long —> Long
float —> Float
double —> Double