Set和Map,元素都不能重复,但是Map是覆盖,Set是添加失败
一、集合框架体系
(1)集合的理解和好处
-
数组的不足之处:
-
数组长度在定义时必须指定,一旦指定,不能更改
-
数组保存的元素必须为同一类型
-
使用数组进行元素增加比较麻烦(如果数组的元素已经满了,此时我们还想往里面添加元素,就只能创建一个更大的数组,并把原数组的数据拷贝进去)
-
-
集合:
-
可以动态保存任意多个元素,使用比较方便
-
提供了一系列方便的操作对象的方法
-
使用集合添加元素、删除元素的代码简洁了
-
(2)集合的框架体系图
(3)Collection方法
-
Collection接口没有直接的实现子类,是通过它的子接口List和Set来实现的
-
Collection接口的常用方法:
add 添加单个元素 remove 删除单个元素(根据索引/元素删) contains 查找单个元素是否存在 size 获取元素个数 isEmpty 判断是否为空 clear 清空 addAll 添加多个元素 containsAll 查找多个元素是否都存在 removeAll 删除多个元素 -
以实现子类ArrayLIst来演示:
public class CollectionMethod { @SuppressWarnings({"all"}) public static void main(String[] args) { List list = new ArrayList(); //1.add添加单个元素 list.add("jack"); list.add(10);//放整数有自动装箱过程 list.add(true); System.out.println("list=" + list); //2.remove删除指定元素 //list.remove(0); //list.remove("jack");//指定删除某个元素对象 System.out.println("list=" + list); //3.contains查找元素是否存在 System.out.println(list.contains("jack")); //4.size获取元素个数 System.out.println(list.size()); //5.isEmpty判断是否为空 System.out.println(list.isEmpty()); //6.clear清空 //list.clear(); //System.out.println("list=" + list); //7.addAll添加多个元素,参数是Collection的实现子类 ArrayList list2 = new ArrayList(); list2.add("红楼梦"); list2.add("三国演义"); //list.addAll(list2); list.addAll(1,list2);//list=[jack, 红楼梦, 三国演义, 10, true] System.out.println("list=" + list); //8.containsAll判断多个元素是否存在 System.out.println(list.containsAll(list2)); //9.removeAll删除多个元素 list.removeAll(list2); System.out.println("list=" + list); } }
(4) Collection接口遍历元素的方式——使用Iterator(迭代器)
- 迭代器基本介绍:
- Iterator对象称为迭代器,主要用于遍历Collection集合中的元素
- 所有实现了Collection接口的集合类都有一个iterator()方法,用以返回一个迭代器对象
- iterator的结构。Collection接口的父接口是Iterable,它里面有一个方法
- 迭代器的执行原理:
- 集合对象.iterator() 用以得到一个迭代器对象
- iterator.hasNext() 判断是否还有下一个元素
- iterator.next() 取出下一个元素,并往下移动一个位置
- 注意:在调用iterator.next()方法之前,必须先调用iterator.hasNext()方法。如果直接调用iterator.next()方法,会抛出NoSuchElementException异常
- 使用迭代器遍历集合的代码演示(快速生成while循环的快捷键是itit+回车):
public class CollectionIterator { @SuppressWarnings({"all"}) public static void main(String[] args) { Collection col = new ArrayList(); col.add(new Book("三国演义","罗贯中",10.1)); col.add(new Book("小李飞刀","古龙",5.1)); col.add(new Book("红楼梦","曹雪芹",34.6)); //System.out.println("col=" + col); //如果我们希望遍历col集合 Iterator iterator = col.iterator(); while(iterator.hasNext()){ Book book = (Book)iterator.next(); System.out.println("book=" + book); } } } class Book{ private String name; private String author; private double price; public Book(String name, String author, double price) { this.name = name; this.author = author; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + '}'; } }
- 注意:当退出while循环后,迭代器的游标是在集合最后一个元素的位置。如果希望再次遍历,我们需要重置迭代器iterator = col.iterator();
(5)Collection接口遍历元素的方式——for循环增强
-
for循环增强:增强for循环,可以代替iterator迭代器。特点:增强for就是简化版的iterator,本质一样(我们可以在for(Object book:col)处下断点,然后追进去看),只能用来遍历集合或数组
-
代码演示(增强for的快捷键是fori+回车):
public class CollectionFor { @SuppressWarnings({"all"}) public static void main(String[] args) { Collection col = new ArrayList(); col.add(new Book("三国演义","罗贯中",10.1)); col.add(new Book("小李飞刀","古龙",5.1)); col.add(new Book("红楼梦","曹雪芹",34.6)); //使用迭代for for(Object book:col){ System.out.println("book=" + book); } } }
二、Collection
(1)List(三种遍历方式)
- List接口基本介绍:
- List集合类中元素有序(即添加顺序和取出顺序一致)、且可重复
- List集合类(LinkedList也包括)中的每个元素都有其对应的顺序索引。索引从0开始
- List接口常用的实现类有:ArrayList、LinkedList、Vector
public class List_ { @SuppressWarnings({"all"}) public static void main(String[] args) { //1.List集合类中元素有序(即添加顺序和取出顺序一致)、且可重复 案例 List list = new ArrayList(); list.add("jack"); list.add("tom"); list.add("mary"); list.add("tom"); System.out.println("list=" + list);//list=[jack, tom, mary, tom] //2.List集合中的每个元素都有其对应的顺序索引。且索引是从0开始的 System.out.println(list.get(2));//mary } }
- List接口的常用方法:
void add(int index, Object ele);
在index位置插入ele元素 boolean addAll(int index, Collection eles);
从index位置开始,将eles中的所有元素添加进来 Object get(int index);
获取指定index位置的元素 int indexOf(Object obj);
返回obj在集合中首次出现的位置 int lastIndexOf(Object obj);
返回obj在集合中末次出现的位置 Object remove(int index);
移除指定index位置的元素,并返回此元素 Object set(int index, Object ele);
设置指定index位置的元素为ele,相当于替换 List subList(int fromIndex, int toIndex);
返回从fromIndex到toIndex位置的子集合,包前不包后 - 代码演示:
public class ListMethod { @SuppressWarnings({"all"}) public static void main(String[] args) { List list = new ArrayList(); list.add("张三丰"); list.add("贾宝玉"); //1.void add(int index, Object ele);在index位置插入ele元素 list.add(1,"jack");//如果没有索引,那么默认加在最后 System.out.println("list=" + list); //2.boolean addAll(int index, Collection eles);从index位置开始,将eles中的所有元素添加进来 List list2 = new ArrayList(); list2.add("tom"); list2.add("mary"); list.addAll(2,list2);//如果没有索引,那么默认加在最后 System.out.println("list=" + list);//list=[张三丰, jack, tom, mary, 贾宝玉] //3.Object get(int index);获取指定index位置的元素 //4.int indexOf(Object obj);返回obj在集合中首次出现的位置 list.add("jack"); System.out.println("list=" + list);//list=[张三丰, jack, tom, mary, 贾宝玉, jack] System.out.println(list.indexOf("jack"));//1 //5.int lastIndexOf(Object obj);返回obj在集合中末次出现的位置 System.out.println(list.lastIndexOf("jack"));//5 //6.Object remove(int index);移除指定index位置的元素,并返回此元素 System.out.println(list.remove(2));//tom System.out.println("list=" + list);//list=[张三丰, jack, mary, 贾宝玉, jack] //7.Object set(int index, Object ele);设置指定index位置的元素为ele,相当于替换 list.set(4,"tom"); System.out.println("list=" + list);//list=[张三丰, jack, mary, 贾宝玉, tom] //8.List subList(int fromIndex, int toIndex);返回从fromIndex到toIndex位置的子集合,包前不包后的 System.out.println(list.subList(1, 4));//[jack, mary, 贾宝玉] System.out.println("list=" + list);//list=[张三丰, jack, mary, 贾宝玉, tom] } }
- List的三种遍历方式:(1)iterator迭代器(2)增强for循环(3)普通for
package com.study.list; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class ListFor { @SuppressWarnings({"all"}) public static void main(String[] args) { List list = new LinkedList(); list.add("tom"); list.add("jack"); list.add("smith"); list.add("mary"); list.add("kristina"); System.out.println("===使用iterator遍历==="); Iterator iterator = list.iterator(); while (iterator.hasNext()) { Object next = iterator.next(); System.out.println(next); } System.out.println("===使用增强for遍历==="); for (Object obj :list) { System.out.println(obj); } System.out.println("===使用普通for遍历===");//快捷键fori