1.Collection接口
常用的方法:
public static void main(String[] args) {
//创建集合对象
Collection collection=new ArrayList();
//1.添加
collection.add("张三");
collection.add("张三");
collection.add("李四");
collection.add("晨晨");
System.out.println("元素的个数:"+collection.size());
System.out.println("打印:"+collection.toString());
//2.删除
collection.remove("张三");
System.out.println("删除之后:"+collection.toString());
//3.清空
/*collection.clear();
System.out.println("清空之后:"+collection.toString());
*/
//4.遍历
//4.1 使用增强for,不允许删除,如果删除出现并发修改异常
System.out.println("-----------增强for--------------");
for (Object o : collection) {
System.out.println(o);
}
//4.2使用迭代器
System.out.println("---------迭代器--------------");
Iterator it=collection.iterator();
//判断是否有元素
while (it.hasNext()){
System.out.println(it.next());
it.remove();//使用迭代器删除
}
System.out.println("迭代之后:" + collection.size());
//5判断
//5.1判断是否存在
System.out.println(collection.contains("李四"));
//5.2判断是否为空
System.out.println(collection.isEmpty());
//5.3其他方法 all方法
System.out.println("------------all---------------");
Collection num1=new ArrayList();
num1.add(4);
num1.add(5);
num1.add(6);
Collection num2=new ArrayList();
num2.add(7);
num2.add(8);
num2.add(9);
num1.addAll(num2);
System.out.println("num1:"+num1.toString());
System.out.println(num1.containsAll(num2));
System.out.println("----------------------");
num1.retainAll(num2);
System.out.println("num1:"+num1.toString());
System.out.println("-------------------");
num1.removeAll(num1);
System.out.println(num1.size());
}
2.List接口
2.1List接口的存储特点
(1)相对有序存储,可以存储相同元素,可以通过下标访问集合元素
(2)List接口中可以使用独有的迭代器ListIterator,具有反向遍历的功能
2.2List接口的实现类
2.2.1 ArrayList 类
是List接口的大小可变数组的实现,ArrayList类允许包括Null在内的所有元素
存储特点:相对有序存储,可以存储相同元素,可以通过下标遍历,是通过数组实现的集合
常用方法:
(1)字符串类型:
public static void main(String[] args) {
//创建一个ArrayList集合
ArrayList<String> arrayList=new ArrayList<>();//构造方法中的方法中的泛型可以省略
//1.添加
arrayList.add("薛之谦");
arrayList.add("华晨宇");
arrayList.add("周震南");
arrayList.add("毛不易");
arrayList.add("王晨艺");
arrayList.add("姚琛");
//2.删除
// arrayList.remove(0);
//3.遍历
//3.1增强for
System.out.println("--------增强for----------");
for (String s : arrayList) {
System.out.println(s);
}
//3.2迭代器
System.out.println("----------迭代器------------");
Iterator<String> iterator=arrayList.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//3.3使用ListIterator迭代器遍历
System.out.println("----------ListIterator迭代器-------");
ListIterator<String> l=arrayList.listIterator();
while (l.hasNext()){
System.out.println(l.next());
}
//反向遍历(注意:只有正向遍历后才能反向遍历)
while (l.hasPrevious()){
System.out.println(l.previous());
}
//4.判断
System.out.println("---------------------------");
//4.1判断是否为空集合
System.out.println(arrayList.isEmpty());
//4.2查看元素个数
System.out.println(arrayList.size());
//4.3获取下标为1的元素
System.out.println(arrayList.get(1));
//4.4修改下标为2的元素
System.out.println(arrayList.set(2, "刘德华"));
//4.5查看集合中是否有某个元素
System.out.println(arrayList.contains("毛不易"));
}
(2)Student类型:
public static void main(String[] args) {
ArrayList<Student> students=new ArrayList<>();
//1.添加
students.add(new Student("张三",20));
students.add(new Student("李四",20));
students.add(new Student("王五",20));
students.add(new Student("周六" +
"",20));
System.out.println(students.toString());
//2.删除(equals)
students.remove(new Student("张三",20));
System.out.println("删除之后:"+students.toString());
//3.遍历
for (Student student : students) {
System.out.println(student.toString());
}
//4.判断
System.out.println(students.contains(new Student("王五",20)));
//5.获取位置
System.out.println(students.indexOf(new Student("周六",20)));
}
public class Student {
String name;
int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
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 &&
name.equals(student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
2.2.2 LinkedList类
是List接口的链接列表实现,允许所有元素(包括null)
存储结构:双向链表
存储特点:相对有序存储,可以存储相同元素,可以通过下标遍历,是通过链表实现的集合
public static void main(String[] args) {
//创建集合
LinkedList linkedList=new LinkedList();
//1.添加元素
linkedList.add("张三");
linkedList.add("李四");
linkedList.add("王五");
linkedList.add("张三");
System.out.println(linkedList.toString());
//2.删除
linkedList.removeFirst();
System.out.println("删除之后:" + linkedList);
//3.遍历
//3.1增强for
for (Object o : linkedList) {
System.out.println(o);
}
//3.2迭代器
Iterator iterator=linkedList.listIterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//3.3for
System.out.println("-------for------");
for (int i = 0; i < linkedList.size(); i++) {
System.out.println(linkedList.get(i));
}
}
2.3 ArrayList和LinkedList的总结
相同点:
(1)相对有序存储,可以存储相同元素
(2)可以通过下标遍历,也可以使用独有的ListIterator迭代器遍历(具有反向遍历功能)
不同点:
(1)ArrayList存储结构是数组;而LinkedList存储结构是双向链表
(2)ArrayList集合适用于对元素查询、遍历,不适合插入和删除
LinkedList集合适用于插入和删除,不适合遍历和查询