Java中常用接口:
一、Collection接口
一、概述
单列集合类的根接口,用于存储一系列符合某种规则的元素,它有两个重要的子接口,分别是java.util.List
和java.util.Set
。其中,List
的特点是元素有序、元素可重复。Set
的特点是元素无序,而且不可重复。List
接口的主要实现类有java.util.ArrayList
和java.util.LinkedList
,Set
接口的主要实现类有java.util.HashSet
和java.util.TreeSet
。
Collection接口是所有单列集合的最顶层接口,里边定义了所有单列集合共性的方法,任意的单列集合都可以使用Collection接口中的方法。
二、共性的方法
常见的共性方法有如下几种:
方法 | 说明 |
---|---|
public boolean add(E e) | 把给定的对象添加到当前集合中 |
public void clear() | 清空集合中所有的元素 |
public boolean remove(E e) | 把给定的对象在当前集合中删除 |
public boolean contains(E e) | 判断当前集合中是否包含给定的对象 |
public boolean isEmpty() | 判断当前集合是否为空 |
public int size() | 返回集合中元素的个数 |
public Object[] toArray() | 把集合中的元素,存储到数组中 |
下面对这几种方法进行简单的代码说明:
import java.util.ArrayList;
import java.util.Collection;
public class Demo01Collection {
public static void main(String[] args) {
// 创建集合对象,可以使用多态
Collection<String> collection = new ArrayList<>();
System.out.println(collection); // [] 重写了toString方法
System.out.println("================");
/*
public boolean add(E e): 把给定的对象添加到当前集合中
返回值是一个boolean值,一般都返回true,所以可以不用接收
*/
collection.add("nxy");
System.out.println(collection);
collection.add("dzy");
collection.add("wky");
collection.add("jhn");
System.out.println(collection); // [nxy, dzy, wky, jhn]
System.out.println("===============================");
/*
public boolean remove(E e): 把给定的对象在当前集合中删除
返回值是一个boolean值,集合中存在元素,删除元素,返回true
集合中不存在元素,删除失败,返回false
*/
boolean nxyRemove = collection.remove("nxy");
System.out.println(nxyRemove); // true
System.out.println(collection); // [dzy, wky, jhn]
boolean rxzRemove = collection.remove("rxz");
System.out.println(rxzRemove); // false 没有"rxz"这一元素
System.out.println(collection);// [dzy, wky, jhn]
System.out.println("===============================");
/*
public boolean contains(E e): 判断当前集合中是否包含给定的对象
包含: 返回true
不包含:返回false
*/
boolean contains1 = collection.contains("jhn");
System.out.println(contains1); // true
boolean contains2 = collection.contains("rxz");
System.out.println(contains2); // false
System.out.println("===============================");
/*
public boolean isEmpty(): 判断当前集合是否为空
集合为空返回true,不为空返回false
*/
boolean empty = collection.isEmpty();
System.out.println(empty);
System.out.println("===============================");
/*
public int size():返回集合中元素的个数
*/
int size = collection.size();
System.out.println(size);
System.out.println("==============================");
/*
public Object[] toArray():把集合中的元素,存储到数组中
*/
Object[] arr = collection.toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("========================");
/*
public void clear():清空集合中所有的元素
*/
collection.clear();
System.out.println(collection);
System.out.println(collection.isEmpty());
}
}
二、Iterator接口
一、概述
在程序开发中,经常需要遍历集合中的所有元素。针对这种需求,JDK专门提供了一个接口java.util.Iterator
。Iterator
接口也是Java集合中的一员,但它与Collection
、Map
接口有所不同,Collection
接口与Map
接口主要用于存储元素,而Iterator
主要用于迭代访问(即遍历)Collection
中的元素,因此Iterator
对象也被称为迭代器。
二、两个常用的方法
- boolean hasNext() :如果仍有元素可以迭代,则返回true。用于判断判断集合当中还有没有下一个元素,有就返回true,没有就返回false。
- E next() 返回迭代的下一个元素,取出集合中的下一个元素。
三、迭代器的使用
Iterator迭代器是一个接口,我们无法直接使用,需要使用Iterator接口的实现类对象,获取实现类的方式比较特殊。在Collection接口中有一个方法,叫做iterator(),这个方法返回的就是迭代器的实现类对象。
Iterator iterator() 返回在此Collection的元素上进行迭代的迭代器。
迭代器的使用步骤(重点):
- 使用集合中的方法iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)
- 使用Iterator接口中的方法hasNext判断还有没有下一个元素
- 使用Iterator接口中的方法next取出集合中的下一个元素
代码示例:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo01Iterator {
public static void main(String[] args) {
// 创建一个集合对象
Collection<String> collection = new ArrayList<>();
// 往集合中添加元素
collection.add("nxy");
collection.add("jhn");
collection.add("wky");
collection.add("dzy");
collection.add("rxz");
/*
1. 使用集合中的方法iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)
注意:
Iterator<E>接口也是有泛型的,迭代器的泛型跟着集合走,集合是什么泛型迭代器就是什么泛型
*/
// 多态的形式
// 接口 实现类对象
Iterator<String> it = collection.iterator();
/*
发现使用迭代器取出集合中元素的代码是一个重复的过程
所以我们可以使用循环优化
不知道含有多少元素,使用while循环
循环结束的条件,hasNext方法返回false
*/
while (it.hasNext()){
String element = it.next();
System.out.println(element);
}
System.out.println("==============");
for(Iterator<String> it2 = collection.iterator(); it2.hasNext();){
String element2 = it2.next();
System.out.println(element2);
}
//下面是原始的写法
// /*
// 2. 使用Iterator接口中的方法hasNext判断还有没有下一个元素
// */
// boolean b = it.hasNext();
// System.out.println(b); // true
//
// /*
// 3. 使用Iterator接口中的方法next取出集合中的下一个元素
// */
//
// b = it.hasNext();
// System.out.println(b); // true
//
// String str = it.next();
// System.out.println(str); // nxy
//
// System.out.println("----------------");
//
// b = it.hasNext();
// System.out.println(b); // true
//
// str = it.next();
// System.out.println(str); // jhn
//
// System.out.println("----------------");
//
// b = it.hasNext();
// System.out.println(b); // true
//
// str = it.next();
// System.out.println(str); // wky
//
// System.out.println("----------------");
//
// b = it.hasNext();
// System.out.println(b); // true
//
// str = it.next();
// System.out.println(str); // dzy
//
// System.out.println("----------------");
//
// b = it.hasNext();
// System.out.println(b); // true
//
// str = it.next();
// System.out.println(str); // rxz
//
// System.out.println("===============");
//
// b = it.hasNext();
// System.out.println(b); // 没有元素,返回false
//
// str = it.next(); // 没有元素,再取出元素会抛出NoSuchElementException异常
// System.out.println(str);
}
}
三、增强for循环
一、概述
增强for循环(也称for each循环)是JDK1.5以后出来的一个高级for循环,专门用来遍历数组和集合的。它的内部原理其实是个Iterator迭代器,所以在遍历的过程中,不能对集合中的元素进行增删操作。
Collection< E> extends Iterable< E>:所有的单列集合都可以使用增强for
public interface Iterable< T>实现这个接口允许对象成为"foreach"语句的目标
二、格式
增强for循环:用来遍历集合和数组
格式:
for(集合/数组的数据类型 变量名:集合名/数组名){
sout(变量名);
}
三、代码示例
import java.util.ArrayList;
public class Demo02Foreach {
public static void main(String[] args) {
demo01();
demo02();
}
private static void demo01() {
int[] arr = {1,2,3,4,5};
for (int i : arr) {
System.out.println(i);
}
System.out.println("=============");
}
private static void demo02() {
ArrayList<String> list = new ArrayList<>();
list.add("nxy");
list.add("wky");
list.add("dzy");
list.add("jhn");
for (String s:list) {
System.out.println(s);
}
}
}