1. Collection
集合类的特点:提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变
1.2 集合类体系结构
Collection 集合用于存储单列信息,Map 集合用于存储双列集合也就是键值对(key and value)
List 集合存储的元素是可以重复的,Set 集合存储元素是不可以重复的。
蓝色框表示接口,红色的是实现类。使用时要使用具体实现类,因为接口不能直接创建对象并实例化,必须通过具体的实现类创建对象并实例化。
1.3 Collection集合的概述和使用
Collection 集合概述
- 是单列集合的顶层接口,它表示一组对象,这些对象也成为Collection的元素
- JDK不提供此(Collection)接口的任何直接实现:它提供了更具体的子接口的实现,如Set和List 。
创建Collection 集合的对象
- 多态的方式
- 具体的实现类ArrayList
package Java18.Collection;
import java.util.ArrayList;
import java.util.Collection;
public class CollectionDemo1 {
public static void main(String[] args) {
//多态创建Collection集合对象
Collection<String> c = new ArrayList<String>();
//添加元素:boolean add(E e)
c.add("hello");
c.add("world");
c.add("java");
//ArrayList中重写了toString方法,所以输出的不是地址
System.out.println(c);
}
}
1.4 Collection 集合常用方法
package Java18.Collection;
import java.util.ArrayList;
import java.util.Collection;
/*
* Alt+7 可以打开一个窗口,能够看到类的所有信息
* */
public class demo2 {
public static void main(String[] args) {
Collection<String> c = new ArrayList<String>();
//输出是否添加元素成功
// System.out.println(c.add("hello"));
// System.out.println(c.add("world"));
// System.out.println(c.add("world"));
c.add("hello");
c.add("world");
c.add("java");
//boolean remove(Object o):从集合中移除指定元素
// System.out.println(c.remove("javaee"));//false,因为集合中没有javaee元素
c.remove("world");
//void clear():返回值是void,直接用对象调用,清空集合中的所有元素
// c.clear();
//boolean contains(Object o):判定集合中是否有该元素
System.out.println(c.contains("java"));
//boolean isEmpty():判断集合是否为空,返回值为boolean类型
System.out.println(c.isEmpty());
//int size():集合长度
System.out.println(c.size());
//输出集合对象
System.out.println(c);
}
}
1.5 Collection 集合的遍历
Iterator:迭代器,集合的专用遍历方式
public interface Iterator < E > 是一个接口
- Iterator < E > iterator():返回此集合元素的迭代器,通过集合的iterator()方法得到
- 迭代器是通过集合的iterator()方法得到的,所以它是依赖于集合而存在的
Iterator中的常用方法:
E next():返回迭代器的下一个元素
boolean hasNext():如果迭代具有更多的元素,则返回true
package Java18.Collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
//Collection 集合的遍历
public class demo3 {
public static void main(String[] args){
//创建集合对象
Collection<String> c = new ArrayList<String>();
//添加元素
c.add("hello");
c.add("world");
c.add("java");
//Iterator<E> iterator()返回此集合的迭代器
//Iterator是一个接口
Iterator<String> it = c.iterator();//多态的方式得到Iterator的对象
/*
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
...
}
* */
//E next()返回迭代中的下一个元素。
/*
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());//NoSuchElementException:表示被请求的元素不存在。
*/
//boolean hasNext()如果迭代具有更多的元素,则返回true
// 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循环
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
//for循环
/* for(int i=0;i<c.size();i++){
if(it.hasNext()){
System.out.println(it.next());
}
}*/
}
}
1.6集合的使用步骤
- 创建集合对象
- 添加元素
- 遍历集合
通过集合对象获取迭代器
通过迭代器对象的hasNext()方法判定是否还有元素
通过迭代器对象的next()方法获取下一个元素
案例:Collection 集合存储学生对象并遍历
1、建立学生类:
package Java18.Collection;
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name,int age){
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 +
'}';
}
}
执行学生类对象遍历操作
package Java18.Collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class StudentDemo {
public static void main(String[] args) {
//创建学生集合对象
Collection<Student> c = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("lyy",23);
Student s2 = new Student("LYQ",23);
Student s3 = new Student("kkk",32);
//把学生添加到集合
c.add(s1);
c.add(s2);
c.add(s3);
//遍历集合
Iterator<Student> it = c.iterator();
while(it.hasNext()){
Student s = it.next();
// System.out.println(s.getName()+","+s.getAge());
//重写toString方法
System.out.println(s);
}
}
}