Collection体系集合
-
Collection:该体系结构的根接口,代表一组对象,称为“集合”。
-
List接口:有序、有下标、元素可重复;
- ArrayList(Class):数组列表
- LinkedList(Class):链表
- Vector:(以不用)
-
Set接口:无序、无下标、元素不能重复;
- HashSet(Class)
- SortedSet接口:
- TreeSet(Class)
如图
Collection父接口
特点:代表一组任意类型的对象,无序(整体来说)、无下标、不能重复。
创建集合
Collection collection = new ArrayList();
常用方法:
-
boolean add (Object obj) //添加一个对象
添加元素:
collection.add();
-
boolean addAll (Collection c) //将一个集合中的所有对象添加到此集合中
-
void clear () //清空此集合中的所有对象
清空所有对象:
collection.clear();
-
boolean contains (Object o) //检查此集合中是否包含o对象
-
boolean containsAll (Collection<?> c) //包含指定collection中的所有元素,则返回true
-
boolean equals (Object o) //比较此集合是否与指定对象相等
-
boolean isEmpty () //判断此集合是否为空
-
boolean remove (Object o) //在此集合中移除o对象
删除元素:
collection.remove();
-
int size () //返回此集合中的元素个数
-
Object[] toArray () //将此集合转换成数组
-
Iterator iterator() //返回在此collection的元素上进行迭代的迭代器
public interface Iterator<E> //对collection进行迭代的迭代器
-
遍历
1. 使用Iterator方法(迭代器)
- boolean hasNext () //如果仍有元素可以迭代,则返回true
- E next () //返回迭代的下一个元素
- void remove () //从迭代器指向的collection中移除迭代器返回的最后一个元素
使用方法1、迭代器
//haNext(); 有没有下一个元素 //next(); 获取下一个元素 //remove(); 删除当前元素 Iterator it = collection.iterator(); while(it.hasNext()){ String object = (String)it.next(); //强制转化为字符串类型 // 可以使用it.remove(); 移除元素 // collection.remove(); 不能用collection其他方法 会报并发修改异常 }
使用方法2、增强for(无下标)
for (Object object : collection) { }
参考代码
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo01 {
public static void main(String[] args) {
//创建集合
Collection collection = new ArrayList();
System.out.println("添加元素add------------");
//一、添加元素
collection.add("挪威的森林");
collection.add("白夜行");
collection.add("云边有个小卖部");
System.out.println("元素个数:" + collection.size());
System.out.println(collection);
System.out.println("删除元素remove、clear------------");
//二、删除元素
collection.remove("白夜行"); //删除
//collection.clear(); //清空
System.out.println("删除之后:" + collection.size());
System.out.println("遍历元素------------");
//三、遍历元素
System.out.println("增强for循环遍历------------");
// 1 使用增强for循环
for (Object object : collection) {
System.out.println(object);
}
System.out.println("迭代器遍历------------");
//2 迭代器(迭代器专门用来遍历集合的一种方式)重点
//hasNext(); 有没有下一个元素
//next(); 获取下一个元素
//remove(); 删除当前元素
Iterator it = collection.iterator();
while (it.hasNext()) {
String st = (String) it.next();
System.out.println(st);
//collection.remove(st); //不能使用collection中的remove方法删除
//it.remove(); //使用it.remove();来操作删除
}
System.out.println("元素个数:" + collection.size());
System.out.println("判断contains------------");
//四、判断
System.out.println(collection.contains("挪威的森林")); //判断是否有"挪威的森林"这个元素
System.out.println(collection.isEmpty()); //判断此集合是否为空
}
}
参考代码2
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo02 {
public static void main(String[] args) {
//新建Collection对象
Collection collection = new ArrayList();
Student s1 = new Student("张三", 20);
Student s2 = new Student("王二", 18);
Student s3 = new Student("李明", 22);
//1.添加数据
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println("元素个数为:" + collection.size());
System.out.println(collection.toString());
//2.删除
collection.remove(s1);
System.out.println("元素个数为:" + collection.size());
System.out.println(collection.toString());
//删除所有
/*collection.clear(); //只是清除地址而非数据对象
System.out.println("元素个数为:" + collection.size());
System.out.println(collection.toString());*/
//3.遍历
//3.1 增强for
System.out.println("---------增强for----------");
for (Object object : collection) {
Student s = (Student)object;
System.out.println(s.toString());
}
//3.2 迭代器 hasNext(); next(); remove(); 迭代过程中不能使用collection的删除方法
System.out.println("---------迭代器----------");
Iterator it = collection.iterator();
while (it.hasNext()) {
Student s = (Student)it.next();
System.out.println(s.toString());
}
//4.判断
System.out.println(collection.contains(s2));
System.out.println(collection.isEmpty()); //判断是否为空
}
}
/*
public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
*/