一.集合的概念
package javaa.study.collections;
/**
* 集合、数组都是对多个对象进行存储的结构,简称Java容器
* 这里的容器存储指的是内存层面的存储,不涉及到持久层的存储
*
* 数组的特点:
* 1.一旦初始化完成,长度就确定且不可改了
* 2.数据一旦定义好,元素的类型就确定,只能存放指定类型的数据
* 比如String[]只能放String,int[]只能放int(可以利用多态性存储Object[]类型)
* 3.数组提供的方法非常有限,对于数据的增删改查非常不便
* 4.数组数据都是有序且可重复的,对于无序,不可重复的需求不能满足
*
* 集合的分类
* /-----Collection接口:单列集合,用来存储一个个对象
* /-----list接口:存储有序、可重复的数据(“动态”数组)
* /-----ArrayList,LinkList,Vector
* /-----Set接口:存储无序、不可重复的数据(集合概念)
* /-----HashSet,LinkHashSet,TreeSet
* /-----Map接口:双列集合,用于存储一对(key-value)一对的数据(类似于函数)
* /-----HashMap,LinkHashMap,TreeMap,Hashtable,Properties
*
* @author Dionysus_xu
* @create 2022-03-12-14:09
*/
public class CollectionsTest {
public static void main(String[] args) {
}
}
二.Collection接口中的常用方法
package javaa.study.collections;
import org.junit.Test;
import java.util.*;
/**
* Collection接口的通用方法
* @author Dionysus_xu
* @create 2022-03-12-14:44
*/
public class CollectionMethods {
public static void main(String[] args) {
Collection coll = new ArrayList();
//1.add(Object e):将e增加到集合中
coll.add("hello");
coll.add("hello");
coll.add("hello");
System.out.println(coll);
//2.size():确定集合长度
int size = coll.size();
System.out.println(size);
//3.addAll(Collection c):将另一个集合的所有元素添加到本集合中
Collection colls = new ArrayList();
colls.add("world");
colls.add("world");
colls.add("world");
colls.add("world");
coll.addAll(colls);
System.out.println(coll);
Collection c = new ArrayList();
c.add("hello");
//4.clear():清空当前集合
c.clear();
//5.isEmpty():判断当前集合是否为空
System.out.println(c.isEmpty());
//6.contains(Object o):查看集合中是否含有o
coll.add(new String("tom"));
System.out.println(coll.contains("hello"));
System.out.println(coll.contains(new String("tom")));
//虽然都是new的,但是在contains方法中判断的是内容,不是地址
//调用了equals()方法
//7.containsAll(Collection c):
// 判断当前集合是否包含另一个集合c,
// 要求当前对象和集合c里的所有集合元素所在类都要重写equals方法
boolean b = coll.containsAll(colls);
System.out.println(b);
//8.remove():移除某个元素
//数组对象涉及到某个类的时候,需要重写类的equals()方法
boolean isExit = coll.remove("hello");
System.out.println(isExit);
//9.removeAll(Collection c):从当前集合中移除所有c含有的元素
//要重写双方数组所有元素涉及类的equals()方法
//new A= A-A∩B
System.out.println(coll.removeAll(colls));
//10.retainAll(Collection c):求当前集合和集合c的交集
//new A = A∩B
System.out.println(coll.retainAll(colls));
//11.equals(Object o):比较两个集合
System.out.println("*************************");
Collection cc = new ArrayList(coll);
System.out.println(cc.equals(coll));
//12.HashCode():计算数组的hash值
System.out.println(coll.hashCode());
//13.toArray():把集合转换成数组
Object[] objects = coll.toArray();
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i]);
}
//Arrays.asList():把数组转换成集合
List list = Arrays.asList(123,456);
System.out.println(list);
}
@Test
public void testIterator(){
//14.iterator():返回Iterator接口的实例,用于遍历数组元素
//iterator()接口:迭代器接口
//GOF为迭代器的定义为:提供一种方法访问一种容器对象中的各个元素,而不需要知道其内部细节
//"迭代器模式,就是为了容器(数组,集合)而生"
//迭代器的方法:hasNext(),next(),remove()
Collection coll = new ArrayList();
coll.add("hello1");
coll.add("hello2");
coll.add("hello3");
coll.add("hello4");
//调用iterator()方法,返回值是一个Iterator接口对象
Iterator iterator = coll.iterator();
//遍历方式1:
//iterator.next():返回iterator的下一个元素
// System.out.println(iterator.next());//1
// System.out.println(iterator.next());//2
// System.out.println(iterator.next());//3
// System.out.println(iterator.next());//4
// //报异常的情况
// System.out.println(iterator.next());//5
//遍历方式2:
// for (int i = 0; i < coll.size(); i++) {
// System.out.println(iterator.next());
// }
//遍历方式3(推荐):
// while(iterator.hasNext()){
// System.out.println(iterator.next());
// }
//Iterator.remove():移除迭代器某个元素
while (iterator.hasNext()){
Object next = iterator.next();
if("hello1".equals(next)){//防止空指针
iterator.remove();
}
}
//指针走向迭代器最后一位,想要重新遍历集合需要重新建一个迭代器
iterator = coll.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//jdk5.0以后迭代器增加了foreach()方法,用于遍历数组、集合
int[] list = {123,456,777};
//遍历数组
//for(数组对象 局部变量名 : 遍历的数组)
for(int i : list){
System.out.println("数组"+i);
}
//遍历集合
//for(集合对象 局部变量名 : 遍历的集合)
for(Object obj : coll) {
System.out.println("集合" + obj);
}
}
}