1、Collection接口中的常用方法
- add(E e):将原始e添加到集合中;
- size(): 获取添加的元素的个数;
- addAll(Collection c): 将Collection 集合中的数据添加到当前集合中
- clear() : 清空集合中的数据元素
- isEmpty() : 判断集合中是否为空 为空返回true
- contains(Object obj) 是否包含obj , 如果是引用数据类型,在判断时会调用所在类的equals()方法
- containAll(Collection coll) : 判断形参中的coll中所有的元素是否都包含
- remove(Object obj) : 移出集合中的某个元素,在移出之前先判断是否存在,判断是否存在调用的是equals()方法
- removeAll(Collection coll) : 从当前集合中移出coll中的所有元素
- retainAll(Collection<?> c) 交集,获取当前集合和 c 集合的交集,并返回给当前集合。保留一样的,删除不一样的
- equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同
- hashCode(): 返回当前对象的哈希值
- 集合可以转换为数组 toArray()
拓展:数组转换成集合 。Arrays.asList();
**** Collection中的常用方法* @author 杨磊* @create 2021-09-28 9:16*/public class CollectionTest {public static void main(String[] args) {test();}private static void test() {Collection coll = new ArrayList();//1、add(E e):将原始e添加到集合中;coll.add("aa");coll.add("bb");coll.add("cc");coll.add(11);//size(): 获取添加的元素的个数System.out.println(coll.size());// addAll(Collection c): 将Collection 集合中的数据添加到当前集合中Collection coll1 = new ArrayList();coll1.add("aa");coll1.add("bb");coll.addAll(coll1);System.out.println(coll.size());//clear() : 清空集合中的数据元素coll.clear();//isEmpty() : 判断集合中是否为空 为空返回trueSystem.out.println(coll.isEmpty());// contains(Object obj) 是否包含obj ,// 如果是引用数据类型,在判断时会调用所在类的equals()方法System.out.println(coll.contains(11));//containAll(Collection coll) : 判断形参中的coll中所有的元素是否都包含Collection coll1 = new ArrayList();coll1.add("aa");coll1.add("bb");coll.addAll(coll1);System.out.println(coll.containsAll(coll1));//remove(Object obj) : 移出集合中的某个元素,在移出之前先判断是否存在,判断是否存在调用的是equals()方法//removeAll(Collection coll) : 从当前集合中移出coll中的所有元素// retainAll(Collection<?> c) 交集,获取当前集合和 c 集合的交集,并返回给当前集合。保留一样的,删除不一样的//equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同//hashCode(): 返回当前对象的哈希值//集合可以转换为数组//toArray():Object[] array = coll.toArray();for (Object o : array) {System.out.println(o);}//拓展:数组转换成集合//Arrays.asList();}}
2、使用Iterator接口遍历集合元素
2.1、使用hasNext() 和 next()方法配合使用
集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前
/*** @author 杨磊* @create 2021-09-28 10:11*/public class IteratorTest {public static void main(String[] args) {test();}private static void test() {Collection coll = new ArrayList();//1、add(E e):将原始e添加到集合中;coll.add("aa");coll.add("bb");coll.add("cc");coll.add(11);Iterator iterator = coll.iterator();//方式一// System.out.println(iterator.next());// System.out.println(iterator.next());// System.out.println(iterator.next());// System.out.println(iterator.next());//方式二// for (int i = 0 ; i < coll.size() ; i ++){// System.out.println(iterator.next());// }//推荐 iterator.hasNext() 判断集合中是否还有元素while (iterator.hasNext()){System.out.println(iterator.next());}}}
2.2、Iterator迭代器的执行原理
2.3、remove()方法的使用
```java import org.junit.Test;
import java.util.ArrayList; import java.util.Collection; import java.util.Iterator;
/**
- 集合元素的遍历操作,使用迭代器Iterator接口
- 1.内部的方法:hasNext()和 next()
- 2.集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。
3.内部定义了remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合直接调用remove() */ public class IteratorTest {
//测试Iterator中的remove()方法 @Test public void test3(){
Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new Person("Jerry",20));coll.add(new String("Tom"));coll.add(false);//删除集合中”Tom”//如果还未调用next()或在上一次调用 next 方法之后已经调用了 remove 方法,// 再调用remove都会报IllegalStateException。Iterator iterator = coll.iterator();while(iterator.hasNext()){
// iterator.remove();
Object obj = iterator.next();if("Tom".equals(obj)){iterator.remove();
// iterator.remove();
}}//遍历集合iterator = coll.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}
} }
``` 注意:
- Iterator可以删除集合的元素,但是是遍历过程中通过迭代器对象的remove方法,不是集合对象的remove方法。
- 如果还未调用next()或在上一次调用next方法之后已经调用了remove方法,再调用remove都会报IllegalStateException
