概述
集合
类是Java数据结构的实现。常见的数据结构包括数组
、链表
、队列
、哈希表
、红黑树
、B+树
等等。
在实际处理数据过程中,选用合适的数据结构对程序的运行效率是非常重要的。Java集合类就是将一些基本的和使用频率极高的基础类进行封装和增强后再以一个类的形式提供。
Java的集合类是java.util包中的重要内容,它允许以各种方式将元素分组,并定义了各种使这些元素更容易操作的方法。
对象数组:
public class ObjectArrayDemo {
public static void main(String[] args) {
// 创建学生数组(对象数组)。
Student[] students = new Student[5];
// 创建5个学生对象,并赋值。
Student s1 = new Student("lili", 27);
Student s2 = new Student("jack", 30);
Student s3 = new Student("lucy", 30);
Student s4 = new Student("serven", 60);
Student s5 = new Student("shwen", 35);
// 把C步骤的元素,放到数组中。
students[0] = s1;
students[1] = s2;
students[2] = s3;
students[3] = s4;
students[4] = s5;
for(Student s : students){
System.out.println(s);
}
}
}
输出:
Student [name=lili, age=27]
Student [name=jack, age=30]
Student [name=lucy, age=30]
Student [name=serven, age=60]
Student [name=shwen, age=35]
集合:
public class CollectionDemo{
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
Student s1 = new Student("lili", 27);
c.add(s1);
c.add(100);
c.add(true);
System.out.println(c);
}
}
输出:
[Student [name=lili, age=27], 100, true]
注意:
数组
既可以存储基本数据类型,也可以存储引用类型。它存储引用类型的时候的数组就叫对象数组。而集合只能存储引用类型。- 数组固定,集合可变的。
- 数组只能存储同一种类型数据,集合可以存储不同类型数据(但一般集合也是存储同一种类型数据)。
其实,整形数据、布尔型数据存储的是其对应类引用Integer、Boolean,查询反编译文件如下:
c.add(s1);
c.add(Integer.valueOf(100));
c.add(Boolean.valueOf(true));
集合分类
Java中的集合类可以分为两大类:一类是实现 Collection 接口(public interface Collection<E> extends Iterable<E>);另一类是实现 Map 接口(public interface Map<K,V>)。
Collection
是一个基本的集合接口,可以容纳一组集合元素。Collection有两个重要的子接口:List
(public interface List<E> extends Collection<E>)和 Set
(public interface Set<E> extends Collection<E>)。
Map提供键(key)到值(value)的映射。一个Map中不能包含相同的键,每个键只能映射一个值。
![](https://i-blog.csdnimg.cn/blog_migrate/995f49f537b970c1735ea9a56da041f2.jpeg)
Collection的功能概述:
- 1:添加功能
- boolean
add
(Object obj):添加一个元素 - boolean
addAll
(Collection c):添加一个集合的元素
- boolean
- 2:删除功能
- void
clear
():移除所有元素 - boolean
remove
(Object o):移除一个元素 - boolean
removeAll
(Collection c):移除一个集合的元素
- void
- 3:判断功能
- boolean
contains
(Object o):判断集合中是否包含指定的元素 - boolean
containsAll
(Collection c):判断集合中是否包含指定的集合元素 - boolean
isEmpty
():判断集合是否为空
- boolean
- 4:获取功能
- Iterator
iterator
(): 迭代器
- Iterator
- 5:长度功能
- int
size
():元素的个数
- int
- 6:交集功能
- boolean
retainAll
(Collection c):两个集合都有的元素
- boolean
- 7:把集合转换为数组
- Object[]
toArray
()
- Object[]
public class CollectionDemo {
public static void main(String[] args) {
// 创建集合1
Collection c1 = new ArrayList();
c1.add("abc1");
c1.add("abc2");
c1.add("abc3");
// 创建集合2
Collection c2 = new ArrayList();
c2.add("abc3");
c2.add("abc6");
c2.add("abc7");
// boolean addAll(Collection c):添加一个集合的元素
System.out.println("addAll:" + c1.addAll(c2));
System.out.println("addAll c1:" + c1);
System.out.println("addAll c2:" + c2);
//boolean removeAll(Collection c):移除一个集合的元素 只要有一个元素被移除了,就返回true。
System.out.println("removeAll:"+c1.removeAll(c2));
System.out.println("removeAll c1:" + c1);
System.out.println("removeAll c2:" + c2);
//boolean containsAll(Collection c):判断集合中是否包含指定的集合元素 只有包含所有的元素,才返回true。
c1.add("abc3");
System.out.println("containsAll c1:" + c1);
System.out.println("containsAll c2:" + c2);
System.out.println("contains:"+ c1.contains("abc3"));
System.out.println("containsAll:"+c1.containsAll(c2));
//boolean retainAll(Collection c):两个集合都有的元素
System.out.println("retainAll:"+c1.retainAll(c2));
System.out.println("retainAll c1:" + c1);
System.out.println("retainAll c2:" + c2);
}
}
输出:
addAll:true
addAll c1:[abc1, abc2, abc3, abc3, abc6, abc7]
addAll c2:[abc3, abc6, abc7]
removeAll:true
removeAll c1:[abc1, abc2]
removeAll c2:[abc3, abc6, abc7]
containsAll c1:[abc1, abc2, abc3]
containsAll c2:[abc3, abc6, abc7]
contains:true
containsAll:false
retainAll:true
retainAll c1:[abc3]
retainAll c2:[abc3, abc6, abc7]
removeAll
:只要有一个元素被移除了,就返回true。
containsAll
:只有包含所有的元素,才返回true。
retainAll
:A对B做交集,最终的结果保存在A中,B不变。返回值表示的是A是否发生过改变。
public class CollectionDemo {
public static void main(String[] args) {
// 创建集合1
Collection c1 = new ArrayList();
c1.add("abc1");
c1.add("abc2");
// 创建集合2
Collection c2 = new ArrayList();
c2.add("abc1");
c2.add("abc2");
c2.add("abc7");
System.out.println("retainAll c1:" + c1);
System.out.println("retainAll c2:" + c2);
System.out.println("retainAll:"+c1.retainAll(c2));
System.out.println("retainAll c1:" + c1);
System.out.println("retainAll c2:" + c2);
}
}
输出:
retainAll c1:[abc1, abc2]
retainAll c2:[abc1, abc2, abc7]
retainAll:false
retainAll c1:[abc1, abc2]
retainAll c2:[abc1, abc2, abc7]
虽然c1、c2有共同元素abc1, abc2,但是执行retainAll 返回false,因为 c1的元素没有变化。如果注释掉c2.add("abc2")
, 结果如下:
retainAll c1:[abc1, abc2]
retainAll c2:[abc1, abc7]
retainAll:true
retainAll c1:[abc1]
retainAll c2:[abc1, abc7]
![](https://i-blog.csdnimg.cn/blog_migrate/6d2448ecfb8b6bffb26b56d0bcedc7d2.png)
遍历
public class IteratorDemo {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
Student s1 = new Student("lili", 27);
Student s2 = new Student("lucy", 30);
Student s3 = new Student("jack", 33);
c.add(s1);
c.add(s2);
c.add(s3);
//遍历一 转对象数组
Object[] objects = c.toArray();
for(Object obj : objects ){
Student s = (Student) obj;
System.out.println(s);
}
// 遍历二 迭代器
Iterator it = c.iterator();
while (it.hasNext()) {
// System.out.println(it.next());
Student s = (Student) it.next();
System.out.println(s);
}
// 遍历三 增强 for
for(Object s : c){
Student stu = (Student) s;
System.out.println(stu);
}
}
}
其实增强for,编译器会自动换成 Iterator迭代器。查看反编译文件如下:
for (Object s = c.iterator(); ((java.util.Iterator)s).hasNext(); ) {
Object s = ((java.util.Iterator)s).next();
Student stu = (Student)s;
System.out.println(stu);
}
注意点
集合类容纳的对象都是Object
类型,一旦把一个对象置入集合类中,它的类信息将丢失,也就是说,集合类中容纳的都是指向 Object 类对象的指针。
这样的设计是为了使集合类具有通用性,因为 Object 类是所有类的祖先,所以可以在这些集合中存放任何类而不受限制。当然这也带来了不便,这令使用集合成员之前必须对它强制转换
。
集合好处:
- 降低编程难度:在编程中会经常需要链表、向量等集合类,直接调用Java中提供的这些接口和类。
- Java提供的集合类具有较高的质量,运行时速度也较快,提升程序的运行速度和质量。
- 借助泛型,无需为每一种数据类型学习不同的API。