------- android培训、java培训、期待与您交流! ----------
集合框架
简单概述
1. 集合是一个容器, 可以存储Object类型的数据
2. 数组和集合区别:
数组虽然也可以存储对象,但长度是固定的;集合长度是可变的。数组中可以存储基本数据类型,集合只能存储对象
3. 集合的特点:
集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象
4. Collection体系结构图
5. Collection特点(子接口List和Set)
List: 元素排列有顺序, 可以有重复元素
Set: 元素排列无顺序, 不可以有重复元素
重复指的是两个元素equals为true时
Collection接口中的方法
1. add方法
声明: public booleanadd(Object obj)
参数: Object对象
返回值: boolean 代表是否添加成功
作用: 向集合中添加元素
2. clear方法
声明: public void clear()
作用: 清空集合中的所有元素
3. contains方法
声明: public Boolean contains(Object obj)
作用: 判断是否包含指定元素
注意: 元素.equals(obj)为true是包含
4. isEmpty方法
声明: public boolean isEmpty()
作用: 判断集合是否为空
5. size方法
声明: public int size()
作用: 返回集合中元素个数
6. remove方法
声明: public Boolean remove(Object obj)
作用: 移除指定元素obj, 多个元素相同, 值删除第一个
7. addAll方法
声明: public boolean addAll(Collection c)
作用: 将指定集合中的左右元素加入当前集合
8. containsAll方法
声明: public boolean contains(Collection c)
作用: 判断指定集合是否为当前集合的子集
9. removeAll方法
声明: public boolean removeAll(Collection c)
作用: 将当前集合中所有指定集合包含的元素删除
返回值: 如果指定集合中的元素在当前集合一个都不包含返回false, 否则true
10. retainAll()方法
声明: public Boolean retainAll(Collection c)
作用: 保留指定集合与当前集合都有的元素
返回值: 没有共同元素返回false, 否则true
iterator方法(重点)
声明: publicIterator iterator()
作用: 返回在当前集合元素进行迭代的迭代器, 就是遍历集合
1. Iterator迭代器
迭代器中的三个方法
public boolean hasNext()//判断是否有下一个元素
public Object next()//将集合元素取出
public void remove()//移除元素
2. 使用迭代器步骤:
Collection coll = new ArrayList() //创建一个集合对象
Iteratoriter = coll.iterator() //1. 创建迭代器
while(iter.hasNext()) { //2. 判断是够有元素可以迭代
Object obj = iter.next() //3. 得到集合中元素
}
3. 关于迭代器的应用
/*
向集合中添加随机的10个数, 不能重复
分析:
1. 集合中的add() 方法添加
2. contains() 判断是否包含当前传进来的随机数
*/
import java.util.*;
class AddIterTest {
public static void main(String[] args) {
// 将getColl方法获取集合赋给Collection类型的引用
Collection coll = getCollection();
// System.out.println(coll);
// 用while循环迭代器的方法打印集合内容
System.out.println("下面是while迭代器打印结果");
iter1(coll);
System.out.println("--------------------------------");
// 用for循环迭代器的方法打印集合内容
System.out.println("下面是for迭代器打印结果");
iter2(coll);
}
// 定义返回包含10个不同随机数的集合
public static Collection getCollection() {
// 创建一个集合对象
Collection coll = new ArrayList();
// 定义Random对象
Random ran = new Random();
// 循环将不重复的随机数加入集合
while (coll.size() != 10) {
int x = ran.nextInt(10);
// 判断集合中是否存在当前随机数. 如果不存在, 将随机数加入集合
if (!coll.contains(x)) {
coll.add(x);
}
}
return coll;
}
// 定义一个迭代器打印集合内容的方法
public static void iter1(Collection coll) {
// 创建一个Iterator对象
Iterator iter = coll.iterator();
// 当迭代器检查到集合中有下一个元素师循环
int count = 0;
while (iter.hasNext()) {
count++;
// 将取出来的值转换为Integer类型
int i = (Integer) (iter.next());
System.out.println(i);
}
System.out.println("循环了 " + count + " 次");
}
// 定义一个迭代器打印集合内容的方法
public static void iter2(Collection coll) {
// 当迭代器检查到集合中有下一个元素师循环
for (Iterator iter = coll.iterator(); iter.hasNext();) {
// 将取出来的值转换为Integer类型
int i = (Integer) (iter.next());
System.out.println(i);
}
}
}
4. 迭代器问题
Iterator返回的都是内部类对象, 内部类实现了Iterator接口
注意: 在对集合使用迭代器的过程中, 不允许使用集合提供的remove方法移除, 只能用Iterator的remove方法移除
集合中方法的使用示例
import java.util.*;
class CollectionTest {
public static void main(String[] args) {
// 创建一个Collection对象
Collection collAdd = new ArrayList();
// add方法
collAdd.add('A'); // 加入字符
collAdd.add(new char[] { 'b', 'c' }); // 加入字符数组
collAdd.add(123); // 添加int型
collAdd.add("Hello"); // 添加String
boolean add = collAdd.add(true); // 添加boolean型, 返回boolean结果
System.out.println(add); // 打印结果: true
System.out.println(collAdd); // 打印结果: [A, [C@45a27a8f, 123, Hello, true]
// clear方法
collAdd.clear();
System.out.println(collAdd); // 打印结果: []
// contains方法
Collection collContains = new ArrayList();
collContains.add(1);
collContains.add(2);
collContains.add(3);
collContains.add(4);
collContains.add(5);
boolean contains = collContains.contains(3);
System.out.println(contains); // 打印结果: true
contains = collContains.contains(6);
System.out.println(contains); // 打印结果: false
// isEmpty方法
boolean isEmp = collContains.isEmpty();
System.out.println(isEmp); // 打印结果: false
// 清空集合
collContains.clear();
isEmp = collContains.isEmpty();
System.out.println(isEmp); // 打印结果: true
// size方法
Collection collSize = new ArrayList();
collSize.add(1);
collSize.add('d');
collSize.add("hello");
collSize.add('d');
int size = collSize.size();
System.out.println(size); // 打印结果: 4
// remove方法
collSize.remove('d');
System.out.println(collSize); // 打印结果: [1, hello, d]
// addAll方法
Collection addAll = new ArrayList();
addAll.add(1);
addAll.add(2);
addAll.add(3);
addAll.addAll(collSize);
System.out.println(addAll); // 打印结果: [1, 2, 3, 1, hello, d]
// containsAll方法
Collection conAll = new ArrayList();
conAll.add(1);
conAll.add(2);
conAll.add(3);
conAll.add(4);
conAll.add(5);
Collection con = new ArrayList();
con.add(1);
con.add(2);
con.add(3);
// 是否包含con中的所有元素
boolean containsAll = conAll.containsAll(con);
System.out.println(containsAll); // 打印结果: true
// removeAll方法
boolean remove = conAll.removeAll(con);
System.out.println(remove); // 打印结果: true
System.out.println(conAll); // 打印结果: [4, 5]
// retainsAll
Collection collRet = new ArrayList();
collRet.add(1);
collRet.add(2);
collRet.add(3);
collRet.add(4);
collRet.add(5);
Collection collR = new ArrayList();
collR.add(3);
collR.add(4);
collR.add(5);
// collRet集合只留下它与collR的交集
boolean collRetain = collRet.retainAll(collR);
System.out.println(collRetain); // 打印结果: true
System.out.println(collRet); // 打印结果: [3, 4, 5]
}
}