集合概述
- 为了保存数量不确定的数据,以及具有映射关系的数据,Java提供了集合类(也称容器类)。
- 所有的集合类都位于java.util包下。
- 数组既可以保存基本类型的值,也可以保存对象,而集合里只能保存对象。(实际上它俩保存的是对象的引用变量)。
- Java的集合类主要由两个接口派生而出:Collection和Map。它们是集合框架的根接口。
- 我们主要说四种集合:Set、List、Queue、Map。对于这四种集合,最常用的实现类有:HashSet、TreeSet、ArrayList、ArrayDeque、LinkedList、HashMap、TreeMap等。
- Set是无序集合,它无法记住添加元素的顺序,所以Set元素不能重复。如果我们要访问Set集合中的元素,只能根据元素本身访问。
- List是有序集合,它很像数组,可以记住每次添加元素的顺序,且长度可变。如果访问List集合的数组,可以根据元素的索引访问。
- Map的实现类用于保存具有映射关系的数据,Map保存的每项数据都是Key-value对,就像科目名和成绩,数学-78,语文-88。我们通常通过科目名查成绩,而不是通过成绩差科目名,因而,key值是不可以重复的。如果我们要访问Map集合中的元素,可以根据key值访问其value。
Collection和Iterator接口
- Collection接口是Set、List、Queue接口的父接口。下面是Collection接口中定义的一些方法:
- boolean add(Object o):向集合里添加一个元素,如果集合对象被添加元素改变了,则返回true
- boolean addAll(Collection c):将集合c中的所有元素添加到指定集合里。
- void clear():清除集合里的所有元素
- int size():该方法返回集合中元素的个数
- Object[] toArray(): 将集合转换成数组。
- boolean contains(Object o):如果此 collection 包含指定的元素,则返回 true
- boolean containsAll(Collection c):如果此 collection 包含指定 collection 中的所有元素,则返回
true
。 - boolean isEmpty(): 如果此 collection 为空,则返回
true
- boolean remove(Object o):从集合中删除指定元素o,如果该集合包含多个o,该方法只删除第一个o
- boolean removeAll(Collection c):从集合中删除集合c里包含的所有元素
- Iterator iterator():返回一个iterator对象,用于遍历集合里的元素
import java.util.ArrayList;
import java.util.Collection;
public class Demo {
public static void main(String[] args) {
Collection c = new ArrayList();
//添加元素
c.add("sehun");
c.add("love");
c.add(777);
System.out.println("集合中的元素个数为:"+c.size());
//删除元素
boolean b1 = c.remove("love");
System.out.println("\"love\"是否删除成功:"+b1);
//判断集合是否包含某个元素
boolean b2 = c.contains("love");
System.out.println("集合是否包含\"love\":"+b2);
c.clear();
boolean empty = c.isEmpty();
System.out.println("集合是否为空:"+empty);
}
}
//结果
集合中的元素个数为:3
"love"是否删除成功:true
集合是否包含"love":false
集合是否为空:true
遍历集合
-
用lambda表达式遍历集合
public class Demo { public static void main(String[] args) { Collection c = new ArrayList(); c.add("sehun"); c.add("love"); c.add(777); //用Iterable的forEach(Consumer action)方法遍历集合元素 //该方法所需参数的类型是一个函数式接口,因而可以使用lambda表达式来遍历集合元素 //Iterable是Collection接口的父接口,因此Collection接口也可以直接调用该方法 c.forEach(obj-> System.out.println(obj)); } }
-
用Iterator遍历集合元素
-
Iterator接口 主要用于遍历(迭代访问)Collection集合中的元素,Iterator对象也被称为迭代器。
-
Iterator接口中的方法如下
-
boolean hasNext(): 如果仍有元素可以迭代,则返回 true
-
Object next(): 返回迭代的下一个元素
-
void remove(): 从迭代器指向的 collection 中移除迭代器返回的最后一个元素
-
-
public class Demo {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("sehun");
c.add("love");
c.add("p");
//获取集合c对应的迭代器
Iterator iterator = c.iterator();
while (iterator.hasNext()){
//iterator.next()返回的类型是Object类型,进行强制类型转换
String s = (String)iterator.next();
System.out.println(s);
if(s.equals("love")){
//当使用Iterator迭代访问Collection集合元素时,Collection集合不能被改变
//只能通过Iterator的remove()方法删除上一次next()方法返回的元素才可以;
iterator.remove();
//会出现异常
//c.remove(s);
}
}
System.out.println(c);
}
}
//结果
sehun
love
p
[sehun, p]
-
用forEach循环遍历
public class Demo { public static void main(String[] args) { Collection c = new ArrayList(); c.add("sehun"); c.add("love"); c.add("p"); for (Object obj:c) { System.out.println(obj); } } }