集合类(Collection)
Collection接口有两个子接口:
List(链表|线性表)
Set(集)
特点:
Collection中描述的是集合共有的功能(CRUD)
List可存放重复元素,元素存取是有序的
Set不可以存放重复元素,元素存取是无序的
java.util.Collection ---| Collection 描述所有接口的共性 ----| List接口 可以有重复元素的集合 ----| Set 接口 不可以有重复元素的集合
2:学习集合对象
学习Collection中的共性方法,多个容器在不断向上抽取就出现了该体系。发现Collection接口中具有所有容器都具备的共性方法。查阅API时,就可以直接看该接口中的方法。并创建其子类对象对集合进行基本应用。当要使用集合对象中特有的方法,在查看子类具体内容。
查看api 文档Collection在在java.util 中(注意是大写Collection)
注意在现阶段遇到的 E T 之类的类型,需要暂时理解为object 因为涉及到了泛型.
3:创建集合对象,使用Collection中的List的具体实现类ArrayList
1:Collection coll=new Arraylist();
1.Collection接口的共性方法
增加: 1:add() 将指定对象存储到容器中 add 方法的参数类型是Object 便于接收任意对象 2:addAll() 将指定集合中的元素添加到调用该方法和集合中 删除: 3:remove() 将指定的对象从集合中删除 4:removeAll() 将指定集合中的元素删除 修改 5:clear() 清空集合中的所有元素 判断 6:isEmpty() 判断集合是否为空 7:contains() 判断集合何中是否包含指定对象 8:containsAll() 判断集合中是否包含指定集合 使用equals()判断两个对象是否相等 获取: 9:int size() 返回集合容器的大小 转成数组10: toArray() 集合转换数组
1.1. 增加:
public static void main(String[] args) { Collection list = new ArrayList(); // 增加:add() 将指定对象存储到容器中 list.add("计算机网络"); list.add("现代操作系统"); list.add("java编程思想"); System.out.println(list); // [计算机网络, 现代操作系统, java编程思想] // 增加2 将list容器元素添加到list2容器中 Collection list2 = new ArrayList(); list2.add("java核心技术"); list2.addAll(list); list2.add("java语言程序设计"); System.out.println(list2); // [java核心技术, 计算机网络, 现代操作系统, java编程思想, java语言程序设计] }
1.2. 删除:
// 删除1 remove boolean remove = list2.remove("java核心技术"); System.out.println(remove); // true System.out.println(list2); // //删除2 removeAll() 将list中的元素删除 boolean removeAll = list2.removeAll(list); System.out.println(removeAll);//true System.out.println(list2);//[java语言程序设计]
1.3. 修改:
public static void main(String[] args) { Collection list = new ArrayList(); // 增加:add() 将指定对象存储到容器中 list.add("计算机网络"); list.add("现代操作系统"); list.add("java编程思想"); list.add("java核心技术"); list.add("java语言程序设计"); System.out.println(list); // 修改 clear() 清空集合中的所有元素 list.clear(); System.out.println(list); //[] }
1.4. 判断:
public static void main(String[] args) { Collection list = new ArrayList(); // 增加:add() 将指定对象存储到容器中 list.add("计算机网络"); list.add("现代操作系统"); list.add("java编程思想"); list.add("java核心技术"); list.add("java语言程序设计"); System.out.println(list); boolean empty = list.isEmpty(); System.out.println(empty);// false boolean contains = list.contains("java编程思想"); System.out.println(contains);// true Collection list2 = new ArrayList(); list2.add("水许传"); boolean containsAll = list.containsAll(list2); System.out.println(containsAll);// false }
1.5. 获取:
public static void main(String[] args) { Collection list = new ArrayList(); // 增加:add() 将指定对象存储到容器中 list.add("计算机网络"); list.add("现代操作系统"); list.add("java编程思想"); list.add("java核心技术"); list.add("java语言程序设计"); System.out.println(list); // 获取 集合容器的大小 int size = list.size(); System.out.println(size); }
1.6. 练习:集合中添加自定义对象
public static void main(String[] args) { // 创建集合对象 Collection coll = new ArrayList(); // 创建Person对象 Person p1 = new Person("jack", 25); Person p2 = new Person("rose", 22); Person p3 = new Person("lucy", 20); Person p4 = new Person("jack", 25); // 集合中添加一些Perosn // 删除指定Person // 删除所有Person // 判断容器中是否还有Person // 判断容器中是否包含指定Person // 获取容器中Person的个数 // 将容器变为数组,遍历除所有Person }
分析:
1:Person类
1:姓名和年龄
2:重写hashCode和equals方法
1:如果不重写,调用Object类的equals方法,判断内存地址,为false
1:如果是Person类对象,并且姓名和年龄相同就返回true
2:如果不重写,调用父类hashCode方法
1:如果equals方法相同,那么hashCode也要相同,需要重写hashCode方法
3:重写toString方法
1:不重写,直接调用Object类的toString方法,打印该对象的内存地址
Person类
class Person { private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } @Override public int hashCode() { return this.name.hashCode() + age; } @Override public boolean equals(Object obj) { if (!(obj instanceof Person)) { return false; } Person p = (Person) obj; return this.name.equals(p.name) && this.age == p.age; } @Override public String toString() { return "Person :name=" + name + ", age=" + age; } }
public static void main(String[] args) { Person p1 = new Person("张三", 19); Person p2 = new Person("李四", 20); Person p3 = new Person("王五", 18); Collection list = new ArrayList(); list.add(p1); list.add(p2); list.add(p3); // isEmpty() 判断集合是否为空 boolean empty = list.isEmpty(); System.out.println(empty); // 返回集合容器的大小 int size = list.size(); System.out.println(size); // contains()判断集合何中是否包含指定对象 boolean contains = list.contains(p1); System.out.println(contains); // remove(); 将指定的对象从集合中删除 list.remove(p1); // clear() 清空集合中的所有元素 list.clear(); System.out.println(list); }
//使用集合存储自定义对象2 class Book { private String name; private double price; public Book() { } public Book(String name, double price) { this.name = name; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public int hashCode() { return (int) (this.name.hashCode() + price); } @Override public boolean equals(Object obj) { if (!(obj instanceof Book)) { return false; } Book book = (Book) obj; return this.name.equals(book.name) && this.price == book.price; } @Override public String toString() { return "book:@ name:" + this.name + ", price:" + this.price; } } public class Demo1 { public static void main(String[] args) { Collection col = new ArrayList(); col.add(new Book("think in java", 100)); col.add(new Book("core java", 200)); System.out.println(col); } }