一、导读
这一章我们学习java中的单列集合,Collection下分List和Set,Collection,List,Set都是接口。
List下分ArrayList和LinkedList,Set下分HashSet,LinkedHashSet,TreeSet。
二、Collection
认识collection
student类:
package 基础.week2.day11;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
测试类:
package 基础.week2.day11;
import java.util.ArrayList;
import java.util.Collection;
public class Demo1 {
public static void main(String[] args) {
//Collection 单列集合的祖宗
//List 有序,可重复,有索引
//Set 无须,不重复,无索引
//Collection 的方法,单列集合都可以用
Collection<String > collection=new ArrayList<>();
//1.add
collection.add("aaa");
collection.add("bbb");
System.out.println(collection);
//2.clear
// collection.clear();
// System.out.println(collection);
//3.remove()
collection.remove("aaa");
System.out.println(collection);
//4.contains()
//底层是equals()
boolean flag = collection.contains("bbb");
System.out.println(flag);
System.out.println("=====");
Collection<Student> students=new ArrayList<>();
Student s1 = new Student("张三", 23);
Student s2 = new Student("李四", 30);
Student s3 = new Student("王五", 45);
students.add(s1);
students.add(s2);
students.add(s3);
System.out.println(students);
//同姓名,同年龄是同一个人(即同属性是同一个人)
Student s4 = new Student("张三", 23);
boolean contains = students.contains(s4);
System.out.println(contains);
//5.isempty()
boolean empty = collection.isEmpty();
System.out.println(empty);
//6.size()
int size = collection.size();
System.out.println(size);
}
}
[aaa, bbb]
[bbb]
true
=====
[Student{name = 张三, age = 23}, Student{name = 李四, age = 30}, Student{name = 王五, age = 45}]
true
false
1
Collection3种遍历方式
package 基础.week2.day11;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo2 {
public static void main(String[] args) {
//Collection的遍历方式
//1.迭代器遍历
System.out.println("===1.迭代器遍历===");
Collection<String >collection=new ArrayList<>();
collection.add("aaa");
collection.add("bbb");
collection.add("ccc");
System.out.println(collection);
Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()){
String next = iterator.next();
System.out.println(next);
}
//迭代器删除元素
System.out.println("===迭代器删除元素====");
Iterator<String> iterator1 = collection.iterator();
while (iterator1.hasNext()){
String next = iterator1.next();
if("bbb".equals(next)) {
iterator1.remove();
}
}
System.out.println(collection);
//2.增强for 适用:数组,单列集合
System.out.println("===2.增强for===");
Collection<String > collection1=new ArrayList<>();
collection1.add("aaa");
collection1.add("bbb");
collection1.add("ccc");
for (String s : collection1) {
System.out.println(s);
}
//3.Lambda jdk8开始
System.out.println("===3.Lambda===");
Collection<String > collection2=new ArrayList<>();
collection2.add("aaa");
collection2.add("bbb");
collection2.add("ccc");
collection2.forEach(s->{
System.out.println(s);
});
}
}
===1.迭代器遍历===
[aaa, bbb, ccc]
aaa
bbb
ccc
===迭代器删除元素====
[aaa, ccc]
===2.增强for===
aaa
bbb
ccc
===3.Lambda===
aaa
bbb
cccProcess finished with exit code 0
三、Lis
认识List
package 基础.week2.day11;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class Demo3 {
public static void main(String[] args) {
//List集合(接口) 单列集合,
//List 有序,可重复,有索引(单列集合)
//List的特有方法
List<String > list=new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
System.out.println(list);
//1.通过索引添加元素
list.add(1,"ppp");
System.out.println(list);
//2.通过索引删除元素
String remove = list.remove(0);
System.out.println(remove);
System.out.println(list);
//3.通过索引修改
String set = list.set(0, "aaa");
System.out.println(set);
//4.根据索引拿值
String s = list.get(0);
System.out.println(s);
//List集合的遍历方式,List集合继承Collection
System.out.println("===List集合的遍历方式===");
//1.迭代器 2.增强for 3.Lambda表达式 4普通for 5.列表迭代器
//在遍历过程中,需要删除元素,使用迭代器
//在遍历过程中,需要添加元素,使用列表迭代器
List<String > list1=new ArrayList<>();
list1.add("aaa");
list1.add("bbb");
list1.add("ccc");
ListIterator<String> stringListIterator = list1.listIterator();
while (stringListIterator.hasNext()){
String next = stringListIterator.next();
if("bbb".equals(next)){
stringListIterator.add("777");
}
}
System.out.println(list1);
}
}
[aaa, bbb, ccc]
[aaa, ppp, bbb, ccc]
aaa
[ppp, bbb, ccc]
ppp
aaa
===List集合的遍历方式===
[aaa, bbb, 777, ccc]Process finished with exit code 0
ArrayList
package 基础.week2.day11.List;
import java.util.ArrayList;
public class Demo_Example01 {
public static void main(String[] args) {
//认识ArrayList
//ArrayList的底层是数组
ArrayList arrayList=new ArrayList();
arrayList.add("aaa");
arrayList.add("bbb");
arrayList.add("ccc");
arrayList.add("ddd");
System.out.println("集合的长度:"+arrayList.size());
System.out.println("获取第二个值:"+arrayList.get(1));
}
}
集合的长度:4
获取第二个值:bbbProcess finished with exit code 0
LinkedList
package 基础.week2.day11.List;
import java.util.LinkedList;
public class Demo_Example02 {
public static void main(String[] args) {
//认识LinkedList
LinkedList list=new LinkedList();
System.out.println("===添加元素操作===");
list.add("aaa");//普通添加
list.add("bbb");
list.add("ccc");
System.out.println(list);
list.addFirst("First");//添加第一个元素
System.out.println(list);
list.addLast("end");//添加第一个元素
System.out.println(list);
list.add(1,"元素");//按索引添加元素
System.out.println(list);
System.out.println("===获取元素操作===");
Object first = list.getFirst();
Object last = list.getLast();
Object o = list.get(1);
System.out.println("第一个元素:"+first);
System.out.println("最后一个元素:"+ last);
System.out.println("索引是1的元素:"+o);
System.out.println("===移除元素操作===");
System.out.println(list);
list.removeFirst();//移除起始元素
System.out.println(list);
list.removeLast();//移除最后元素
System.out.println(list);
list.remove(1);//移除索引为1的元素
System.out.println(list);
}
}
===添加元素操作===
[aaa, bbb, ccc]
[First, aaa, bbb, ccc]
[First, aaa, bbb, ccc, end]
[First, 元素, aaa, bbb, ccc, end]
===获取元素操作===
第一个元素:First
最后一个元素:end
索引是1的元素:元素
===移除元素操作===
[First, 元素, aaa, bbb, ccc, end]
[元素, aaa, bbb, ccc, end]
[元素, aaa, bbb, ccc]
[元素, bbb, ccc]Process finished with exit code 0
迭代器
package 基础.week2.day11.List;
import java.util.ArrayList;
import java.util.Iterator;
public class Demo_Example03 {
public static void main(String[] args) {
//认识迭代器
System.out.println("===认识迭代器===");
ArrayList list=new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
Iterator it = list.iterator();
while (it.hasNext()){
Object next = it.next();
System.out.println(next);
}
System.out.println("===用迭代器删除元素===");
Iterator iterator = list.iterator();
while (iterator.hasNext()){
Object next = iterator.next();
if("bbb".equals(next)){
iterator.remove();
}
}
System.out.println(list);
}
}
===认识迭代器===
aaa
bbb
ccc
===用迭代器删除元素===
[aaa, ccc]Process finished with exit code 0
四、Set
HashSet
package 基础.week2.day11.set;
import java.util.HashSet;
import java.util.Iterator;
public class Demo_Example07 {
public static void main(String[] args) {
//认识HashSet
//底层哈希表:比较自定义对象要重写hashCode()和equals()方法
//元素无序,不重复
HashSet set=new HashSet();
set.add("aaa");
set.add("bbb");
set.add("ccc");
set.add("bbb");
Iterator it = set.iterator();
while (it.hasNext()){
Object next = it.next();
System.out.println(next);
}
}
}
aaa
ccc
bbbProcess finished with exit code 0
HashSet添加自定义对象
student类:
package 基础.week2.day11.set;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
public String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
}
测试类:
package 基础.week2.day11.set;
import java.util.HashSet;
import java.util.Iterator;
public class Demo_Example08 {
public static void main(String[] args) {
//HashSet添加自定义对象
HashSet<Student> set=new HashSet<>();
Student student1 = new Student("张三", 23);
Student student2 = new Student("李四", 35);
Student student3 = new Student("王五", 42);
Student student4 = new Student("王五", 42);
//我们要求添加的Student姓名年龄都相同就是同一个人
set.add(student1);
set.add(student2);
set.add(student3);
set.add(student4);
Iterator<Student> it = set.iterator();
while (it.hasNext()){
Student student = it.next();
System.out.println(student);
}
}
}
Student{name = 张三, age = 23}
Student{name = 李四, age = 35}
Student{name = 王五, age = 42}Process finished with exit code 0
LinkedHashSet
package 基础.week2.day11.set;
import java.util.LinkedHashSet;
public class Demo_Example09 {
public static void main(String[] args) {
//认识LinkedHashSet
//元素有序,不重复
//为了实现有序
LinkedHashSet set=new LinkedHashSet();
set.add("aaa");
set.add("bbb");
set.add("ccc");
set.add("aaa");
set.add("bbb");
System.out.println(set);
}
}
[aaa, bbb, ccc]
Process finished with exit code 0
TreeSet
package 基础.week2.day11.set;
import java.util.TreeSet;
public class Demo_Example10 {
public static void main(String[] args) {
//认识TreeSet
//元素 可以排序,不重复
//为了实现排序
//TreeSet分自动排序和自定义排序
TreeSet set=new TreeSet();
set.add(1);
set.add(3);
set.add(2);
set.add(1);
set.add(2);
System.out.println(set);
}
}
[1, 2, 3]
Process finished with exit code 0
TreeSet的自定义排序
car类:
package 基础.week2.day11.set;
public class Car implements Comparable<Car>{
private String brand;
private int price;
public Car() {
}
public Car(String brand, int price) {
this.brand = brand;
this.price = price;
}
/**
* 获取
* @return brand
*/
public String getBrand() {
return brand;
}
/**
* 设置
* @param brand
*/
public void setBrand(String brand) {
this.brand = brand;
}
/**
* 获取
* @return price
*/
public int getPrice() {
return price;
}
/**
* 设置
* @param price
*/
public void setPrice(int price) {
this.price = price;
}
public String toString() {
return "Car{brand = " + brand + ", price = " + price + "}";
}
@Override
public int compareTo(Car car) {
//比较规则,按照价格升序
return this.price- car.price;
}
}
测试类:
package 基础.week2.day11.set;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class Demo_Example11 {
public static void main(String[] args) {
//TreeSet的自定义排序
//方法一:实现Comparator接口,重写compare()方法(比较器排序)
//方法二:匿名内部类
System.out.println("===比较器排序===");
TreeSet<Car> set=new TreeSet<>();
Car car1 = new Car("宝马", 70000);
Car car2 = new Car("奔驰", 40000);
Car car3 = new Car("凯迪拉克", 50000);
set.add(car1);
set.add(car2);
set.add(car3);
Iterator<Car> it = set.iterator();
while (it.hasNext()){
Car car= it.next();
System.out.println(car.getBrand()+": "+car.getPrice());
}
System.out.println("===匿名内部类排序===");
TreeSet<Car> set1=new TreeSet<>(new Comparator<Car>() {
@Override
public int compare(Car car1, Car car2) {
return car1.getPrice()-car2.getPrice();
}
});
Car car11 = new Car("宝马", 70000);
Car car22 = new Car("奔驰", 40000);
Car car33 = new Car("凯迪拉克", 50000);
set1.add(car11);
set1.add(car22);
set1.add(car33);
Iterator<Car> it1 = set.iterator();
while (it1.hasNext()){
Car car= it1.next();
System.out.println(car.getBrand()+": "+car.getPrice());
}
}
}
===比较器排序===
奔驰: 40000
凯迪拉克: 50000
宝马: 70000
===匿名内部类排序===
奔驰: 40000
凯迪拉克: 50000
宝马: 70000Process finished with exit code 0