Collection
Collection:是集合的顶层接口,它的子体系有重复的,有唯一的,有有序的,有无序的。(后面会慢慢的讲解)
Collection的功能概述:
添加功能
boolean add(Object obj):添加一个元素
boolean addAll(Collection c):添加一个集合的元素
删除功能
void clear():移除所有元素
boolean remove(Object o):移除一个元素
boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
判断功能
boolean contains(Object o):判断集合中是否包含指定的元素
boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
boolean isEmpty():判断集合是否为空
获取功能
Iterator<E> iterator()(重点)
长度功能
int size():元素的个数
面试题:
数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢?
交集功能
boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
把集合转换为数组
Object[] toArray()
不带All的方法
package cn.itcast_01;
import java.util.ArrayList;
import java.util.Collection;
public class CollectionDemo {
public static void main(String[] args) {
// 测试不带All的方法
// 创建集合对象
// Collection c = new Collection(); //错误,因为接口不能实例化
Collection c = new ArrayList();
// boolean add(Object obj):添加一个元素
// System.out.println("add:"+c.add("hello"));
c.add("hello");
c.add("world");
c.add("java");
// void clear():移除所有元素
// c.clear();
// boolean remove(Object o):移除一个元素
// System.out.println("remove:" + c.remove("hello"));
// System.out.println("remove:" + c.remove("javaee"));
// boolean contains(Object o):判断集合中是否包含指定的元素
// System.out.println("contains:"+c.contains("hello"));
// System.out.println("contains:"+c.contains("android"));
// boolean isEmpty():判断集合是否为空
// System.out.println("isEmpty:"+c.isEmpty());
//int size():元素的个数
System.out.println("size:"+c.size());
System.out.println("c:" + c);
}
}
带All的方法
package cn.itcast_01;
import java.util.ArrayList;
import java.util.Collection;
/*
* boolean addAll(Collection c):添加一个集合的元素
* boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
* boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
* boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
*/
public class CollectionDemo2 {
public static void main(String[] args) {
// 创建集合1
Collection c1 = new ArrayList();
c1.add("abc1");
c1.add("abc2");
c1.add("abc3");
c1.add("abc4");
// 创建集合2
Collection c2 = new ArrayList();
// c2.add("abc1");
// c2.add("abc2");
// c2.add("abc3");
// c2.add("abc4");
c2.add("abc5");
c2.add("abc6");
c2.add("abc7");
// boolean addAll(Collection c):添加一个集合的元素
// System.out.println("addAll:" + c1.addAll(c2));
//boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
//只要有一个元素被移除了,就返回true。
//System.out.println("removeAll:"+c1.removeAll(c2));
//boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
//只有包含所有的元素,才叫包含
// System.out.println("containsAll:"+c1.containsAll(c2));
//boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
//假设有两个集合A,B。
//A对B做交集,最终的结果保存在A中,B不变。
//返回值表示的是A是否发生过改变。
System.out.println("retainAll:"+c1.retainAll(c2));
System.out.println("c1:" + c1);
System.out.println("c2:" + c2);
}
}
遍历
toArray()
package cn.itcast_01;
import java.util.ArrayList;
import java.util.Collection;
/*
* 集合的遍历。其实就是依次获取集合中的每一个元素。
*
* Object[] toArray():把集合转成数组,可以实现集合的遍历
*/
public class CollectionDemo3 {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 添加元素
c.add("hello"); // Object obj = "hello"; 向上转型
c.add("world");
c.add("java");
// 遍历
// Object[] toArray():把集合转成数组,可以实现集合的遍历
Object[] objs = c.toArray();
for (int x = 0; x < objs.length; x++) {
// System.out.println(objs[x]);
// 我知道元素是字符串,我在获取到元素的的同时,还想知道元素的长度。
// System.out.println(objs[x] + "---" + objs[x].length());
// 上面的实现不了,原因是Object中没有length()方法
// 我们要想使用字符串的方法,就必须把元素还原成字符串
// 向下转型
String s = (String) objs[x];
System.out.println(s + "---" + s.length());
}
}
}
iterator()
package cn.itcast_03;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/*
* Iterator iterator():迭代器,集合的专用遍历方式
* Object next():获取元素,并移动到下一个位置。
* NoSuchElementException:没有这样的元素,因为你已经找到最后了。
* boolean hasNext():如果仍有元素可以迭代,则返回 true。(
*/
public class IteratorDemo {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建并添加元素
// String s = "hello";
// c.add(s);
c.add("hello");
c.add("world");
c.add("java");
// Iterator iterator():迭代器,集合的专用遍历方式
Iterator it = c.iterator(); // 实际返回的肯定是子类对象,这里是多态
// Object obj = it.next();
// System.out.println(obj);
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());
// 最后一个不应该写,所以,我们应该在每次获取前,如果有一个判断就好了
// 判断是否有下一个元素,有就获取,没有就不搭理它
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// 最终版代码
while (it.hasNext()) {
// System.out.println(it.next());
String s = (String) it.next();
System.out.println(s);
}
}
}
需要注意的两个问题
问题1:能用while循环写这个程序,我能不能用for循环呢?
问题2:不要多次使用it.next()方法,因为每次使用都是访问一个对象。
package cn.itcast_03;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class IteratorTest2 {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("令狐冲", 33);
Student s4 = new Student("武鑫", 25);
Student s5 = new Student("刘晓曲", 22);
// 把学生添加到集合中
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
// 遍历
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
// NoSuchElementException 不要多次使用it.next()方法
// System.out.println(((Student) it.next()).getName() + "---"
// + ((Student) it.next()).getAge());
}
// System.out.println("----------------------------------");
// for循环改写
// for(Iterator it = c.iterator();it.hasNext();){
// Student s = (Student) it.next();
// System.out.println(s.getName() + "---" + s.getAge());
// }
}
}
迭代器使用图解及与原理解析
迭代器的源码
/*部分关键代码*/
public interface Inteator {
boolean hasNext();
Object next();
}
public interface Iterable {
Iterator iterator();
}
public interface Collection extends Iterable {
Iterator iterator();
}
public interface List extends Collection {
Iterator iterator();
}
public class ArrayList implements List {
public Iterator iterator() {
return new Itr();
}
private class Itr implements Iterator {
public boolean hasNext() {}
public Object next(){}
}
}
练习题
需求:存储字符串并遍历。
分析:
A:创建集合对象
B:创建字符串对象
C:把字符串对象添加到集合中
D:遍历集合
package cn.itcast_04;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionTest {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建字符串对象
// 把字符串对象添加到集合中
c.add("林青霞");
c.add("风清扬");
c.add("刘意");
c.add("武鑫");
c.add("刘晓曲");
// 遍历集合
// 通过集合对象获取迭代器对象
Iterator it = c.iterator();
// 通过迭代器对象的hasNext()方法判断有没有元素
while (it.hasNext()) {
// 通过迭代器对象的next()方法获取元素
String s = (String) it.next();
System.out.println(s);
}
}
}
需求:存储自定义对象并遍历Student(name,age)
分析:
A:创建学生类
B:创建集合对象
C:创建学生对象
D:把学生对象添加到集合对象中
E:遍历集合
package cn.itcast_04;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionTest2 {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建学生对象
Student s1 = new Student("貂蝉", 25);
Student s2 = new Student("小乔", 16);
Student s3 = new Student("黄月英", 20);
Student s4 = new Student();
s4.setName("大乔");
s4.setAge(26);
// 把学生对象添加到集合对象中
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(new Student("孙尚香", 18)); // 匿名对象
// 遍历集合
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
}
}