应用场景
- 无法预测存储数据的数量
- 同时存储具有一对一关系的数据
- 需要进行数据的增删改查
- 数据重复问题
集合框架的体系结构
List(列表)
- List是元素有序并且可以重复的集合,称为序列
- List可以精确的控制每个元素的插入位置,或删除某个位置的元素
- List的两个主要实现类是ArrayList和LinkedList
ArrayList
- ArrayList底层是由数组实现的
- 动态增长,以满足应用程序的需求
- 在列表尾部插入或删除非常有效
- 更适合查找和更新元素
- ArrayList中的元素可以为null
public static void main(String[] args) {
//用ArrayList存储数据,并输出
List list = new ArrayList();
list.add("java");
list.add("C");
list.add("C++");
list.add("Python");
list.add("PHP");
//输出列表中的个数
System.out.println("列表中的元素个数为:"+list.size());
//遍历输出所有元素
System.out.println("===================");
for (int i = 0; i <list.size() ; i++) {
System.out.print(list.get(i)+" ");
}
//移除列表中的元素
System.out.println();
//list.remove(0);
list.remove("java");
System.out.println("===================");
System.out.println("移除后列表中的元素:");
for (int j = 0; j <list.size() ; j++) {
System.out.print(list.get(j)+" ");
}
}
输出结果:
列表中的元素个数为:5
===================
java C C++ Python PHP
===================
移除后列表中的元素:
C C++ Python PHP
Process finished with exit code 0
public class Notice {
private int id;//公告的ID
private String title;//标题
private String creator;//创建人
private Date creatTime;//创建时间
public Notice(int id, String title, String creator, Date creatTime) {
this.id = id;
this.title = title;
this.creator = creator;
this.creatTime = creatTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public Date getCreatTime() {
return creatTime;
}
public void setCreatTime(Date creatTime) {
this.creatTime = creatTime;
}
public class NoticeTest {
public static void main(String[] args) {
//创建Notice类的对象,生成三条公告
Notice notice1 = new Notice(1,"欢迎来到课堂!","管理员",new Date());
Notice notice2 = new Notice(1,"请遵守课堂纪律!","老师",new Date());
Notice notice3 = new Notice(1,"及时完成课后作业!","学生",new Date());
//添加公告
ArrayList noticeList = new ArrayList();
noticeList.add(notice1);
noticeList.add(notice2);
noticeList.add(notice3);
//显示公告
System.out.println("公告的内容为:");
for (int i = 0; i <noticeList.size() ; i++) {
System.out.println(i+1+":"+((Notice)(noticeList.get(i))).getTitle());
}
}
输出结果
公告的内容为:
1:欢迎来到课堂!
2:请遵守课堂纪律!
3:及时完成课后作业!
Process finished with exit code 0
public class NoticeTest {
public static void main(String[] args) {
//创建Notice类的对象,生成三条公告
Notice notice1 = new Notice(1,"欢迎来到课堂!","管理员",new Date());
Notice notice2 = new Notice(1,"请遵守课堂纪律!","老师",new Date());
Notice notice3 = new Notice(1,"及时完成课后作业!","学生",new Date());
//添加公告
ArrayList noticeList = new ArrayList();
noticeList.add(notice1);
noticeList.add(notice2);
noticeList.add(notice3);
//显示公告
System.out.println("公告的内容为:");
for (int i = 0; i <noticeList.size() ; i++) {
System.out.println(i+1+":"+((Notice)(noticeList.get(i))).getTitle());
}
System.out.println("=======================");
//添加公告
Notice notice4 = new Notice(4,"班长点名了!","班长",new Date());
noticeList.add(1,notice4);
System.out.println("添加后公告的内容为:");
for (int i = 0; i <noticeList.size() ; i++) {
System.out.println(i+1+":"+((Notice)(noticeList.get(i))).getTitle());
}
System.out.println("=======================");
//删除公告4
noticeList.remove(3);
System.out.println("删除后公告的内容为:");
for (int i = 0; i <noticeList.size() ; i++) {
System.out.println(i+1+":"+((Notice)(noticeList.get(i))).getTitle());
}
System.out.println("=======================");
//修改第二条公告为“学委点名了”
notice4.setTitle("学委点名了!");
noticeList.set(1,notice4);
System.out.println("修改后公告的内容为:");
for (int i = 0; i <noticeList.size() ; i++) {
System.out.println(i+1+":"+((Notice)(noticeList.get(i))).getTitle());
}
}
公告的内容为:
1:欢迎来到课堂!
2:请遵守课堂纪律!
3:及时完成课后作业!
=======================
添加后公告的内容为:
1:欢迎来到课堂!
2:班长点名了!
3:请遵守课堂纪律!
4:及时完成课后作业!
=======================
删除后公告的内容为:
1:欢迎来到课堂!
2:班长点名了!
3:请遵守课堂纪律!
=======================
修改后公告的内容为:
1:欢迎来到课堂!
2:学委点名了!
3:请遵守课堂纪律!
Process finished with exit code 0
Set
- Set是元素无序并且不可以重复的集合,被称为集
HashSet
- HashSet是Set的一个重要实现类,称为哈希集
- HashSet中的元素无序并且不可以重复
- HashSet中只允许一个null元素
- 具有良好的存取和查找性能
Iterator(迭代器)
- Iterator接口可以以统一的方式对各种集合元素进行遍历
- hasNext()方法检测集合中是否还有下一个元素
- next()方法返回集合中的下一个元素
public static void main(String[] args) {
//将英文单词添加到HashSet中
Set set = new HashSet();
//向集合中添加元素
set.add("bule");
set.add("yellow");
set.add("red");
set.add("black");
set.add("white");
set.add("green");
set.add("gold");
//显示集合内容
System.out.println("集合元素为:");
Iterator it = set.iterator();//将set中的元素放到迭代器中
//遍历迭代器,并输出元素
while (it.hasNext()){
System.out.print(it.next()+" ");
}
System.out.println();
System.out.println("==================");
//在集合中插入新的单词
//set.add("hrll");
set.add("red");//插入重复元素
it = set.iterator();
System.out.println("插入重复元素后集合元素为:");
//遍历迭代器,并输出元素
while (it.hasNext()){
System.out.print(it.next()+" ");
}
}
输出结果
集合元素为:
red gold bule green white yellow black
==================
插入重复元素后集合元素为:
red gold bule green white yellow black
Process finished with exit code 0
set无法插入重复数据,但程序不会报错,仍能正常运行。
public class Cat {
private String name;
private int month;
private String species;
public Cat(String name, int month, String species) {
this.name = name;
this.month = month;
this.species = species;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
@Override
public String toString() {
return "Cat{" +
"姓名:" + name +
", 月份:" + month +
", 品种:" + species +
'}';
}
}
输出结果
Cat{姓名:饭饭, 月份:4, 品种:橘猫}
Cat{姓名:花花, 月份:12, 品种:英短}
Process finished with exit code 0
如果插入相同的数据
//添加一个与花花属性一样的对象
Cat huahua1 = new Cat("花花",12,"英短");
set.add(huahua1);
System.out.println("==============");
System.out.println("添加重复的数据后");
it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
此时的输出结果
Cat{姓名:饭饭, 月份:4, 品种:橘猫}
Cat{姓名:花花, 月份:12, 品种:英短}
==============
添加重复的数据后
Cat{姓名:花花, 月份:12, 品种:英短}
Cat{姓名:饭饭, 月份:4, 品种:橘猫}
Cat{姓名:花花, 月份:12, 品种:英短}
Process finished with exit code 0
但是HashSet中的元素无序并且不可以重复,此时需要重写Hashcod和equals方法
public class Cat {
private String name;
private int month;
private String species;
public Cat(String name, int month, String species) {
this.name = name;
this.month = month;
this.species = species;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
@Override
public String toString() {
return "Cat{" +
"姓名:" + name +
", 月份:" + month +
", 品种:" + species +
'}';
}
@Override
public boolean equals(Object o) {
//判断对象是否相等,相等则返回true,不用比较属性
if (this == o)
return true;
//判断O是否是Cat类的对象
if (o.getClass() == Cat.class) {
Cat cat = (Cat) o;
return cat.getName().equals(name)&&cat.getMonth()==month&&(cat.getSpecies().equals(species));
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(getName(), getMonth(), getSpecies());
}
输出结果
Cat{姓名:饭饭, 月份:4, 品种:橘猫}
Cat{姓名:花花, 月份:12, 品种:英短}
==============
添加重复的数据后
Cat{姓名:饭饭, 月份:4, 品种:橘猫}
Cat{姓名:花花, 月份:12, 品种:英短}
Process finished with exit code 0
查找对象
//重新插入一个猫对象
Cat huahua02 = new Cat("花花二代",2,"英短");
set.add(huahua02);
System.out.println("==============");
System.out.println("添加花花二代的数据后");
it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("==============");
//在集合中查找花花对象,并输出
if (set.contains(huahua)){
System.out.println("花花找到了!");
System.out.println(huahua);
System.out.println("==============");
}
else {
System.out.println("没找到花花");
System.out.println("==============");
}
//在集合中用名字查找花花
boolean flag = false;
Cat c = null;
it = set.iterator();
while (it.hasNext()){
c = (Cat)it.next();
if (c.getName().equals("花花")){
flag = true;
break;
}
}
if (flag){
System.out.println("花花找到了");
System.out.println(c);
System.out.println("==============");
}else {
System.out.println("花花没找到");
}
}
输出结果
Cat{姓名:饭饭, 月份:4, 品种:橘猫}
Cat{姓名:花花, 月份:12, 品种:英短}
==============
添加重复的数据后
Cat{姓名:饭饭, 月份:4, 品种:橘猫}
Cat{姓名:花花, 月份:12, 品种:英短}
==============
添加花花二代的数据后
Cat{姓名:饭饭, 月份:4, 品种:橘猫}
Cat{姓名:花花二代, 月份:2, 品种:英短}
Cat{姓名:花花, 月份:12, 品种:英短}
==============
花花找到了!
Cat{姓名:花花, 月份:12, 品种:英短}
==============
花花找到了
Cat{姓名:花花, 月份:12, 品种:英短}
==============
Process finished with exit code 0
删除花花二代信息
public class CatTest {
public static void main(String[] args) {
//定义猫对象
Cat huahua = new Cat("花花",12,"英短");
Cat fanfna = new Cat("饭饭",4,"橘猫");
//将猫对象放到HashSet()中
Set<Cat> set = new HashSet<Cat>();
set.add(huahua);
set.add(fanfna);
//显示猫的信息
Iterator<Cat> it = set.iterator();//迭代器
while(it.hasNext()){
System.out.println(it.next());
}
//添加一个与花花属性一样的对象
Cat huahua1 = new Cat("花花",12,"英短");
set.add(huahua1);
System.out.println("==============");
System.out.println("添加重复的数据后");
it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//重新插入一个猫对象
Cat huahua02 = new Cat("花花二代",2,"英短");
set.add(huahua02);
System.out.println("==============");
System.out.println("添加花花二代的数据后");
it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("==============");
//在集合中查找花花对象,并输出
if (set.contains(huahua)){
System.out.println("花花找到了!");
System.out.println(huahua);
System.out.println("==============");
}
else {
System.out.println("没找到花花");
System.out.println("==============");
}
//在集合中用名字查找花花
boolean flag = false;
Cat c = null;
it = set.iterator();
while (it.hasNext()){
c = (Cat)it.next();
if (c.getName().equals("花花")){
flag = true;
break;
}
}
if (flag){
System.out.println("花花找到了");
System.out.println(c);
System.out.println("==============");
}else {
System.out.println("花花没找到");
}
// 删除花花二代的信息并重新输出
for (Cat cat : set) {//把集合中的数据依次取出放到cat中
if ("花花二代".equals(cat.getName())) {
set.remove(cat);
break; }
}
System.out.println("==============");
System.out.println("删除花花二代后的数据");
for(Cat cat:set){
System.out.println(cat);
}
//删除集合中的所有宠物猫信息
System.out.println("==============");
boolean flag1=set.removeAll(set);
if(set.isEmpty()){
System.out.println("猫都不见了。。。");
}else{
System.out.println("猫还在。。。");
}
}
输出结果
Cat{姓名:饭饭, 月份:4, 品种:橘猫}
Cat{姓名:花花, 月份:12, 品种:英短}
==============
添加重复的数据后
Cat{姓名:饭饭, 月份:4, 品种:橘猫}
Cat{姓名:花花, 月份:12, 品种:英短}
==============
添加花花二代的数据后
Cat{姓名:饭饭, 月份:4, 品种:橘猫}
Cat{姓名:花花二代, 月份:2, 品种:英短}
Cat{姓名:花花, 月份:12, 品种:英短}
==============
花花找到了!
Cat{姓名:花花, 月份:12, 品种:英短}
==============
花花找到了
Cat{姓名:花花, 月份:12, 品种:英短}
==============
==============
删除花花二代后的数据
Cat{姓名:饭饭, 月份:4, 品种:橘猫}
Cat{姓名:花花, 月份:12, 品种:英短}
==============
猫都不见了。。。
Process finished with exit code 0
Map
- Map中的数据是以键值对(key-value)的形式存储的
- key-value以Entry类型的对象实例存在
- 可以通过key值快速地查找value
- 一个映射不能包含重复的键
- 每个键最多只能映射到一个值
- key值唯一
HashMap
- 基于哈希表的Map接口的实现
- 允许使用null值和null键
- key值不允许重复
- HashMap中的Entry对象是无序排列的
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Scanner;
import java.util.Map.Entry;
public class Maped {
public static void main(String[] args) {
Map<String,String> animal=new HashMap<String,String>();
System.out.println("请输入三组单词对应的注释,并存放到HashMap中");
Scanner console=new Scanner(System.in);
//添加数据
int i=0;
while(i<3){
System.out.println("请输入key值(单词):");
String key=console.next();
System.out.println("请输入value值(注释):");
String value=console.next();
animal.put(key, value);
i++;
}
//打印输出value的值(直接使用迭代器)
System.out.println("*****************************************");
System.out.println("使用迭代器输出所有的value:");
Iterator<String> it=animal.values().iterator();
while(it.hasNext()){
System.out.print(it.next()+" ");
}
System.out.println();
System.out.println("*****************************************");
//打印输出key和value的值
//通过entrySet方法
System.out.println("通过entrySet方法得到key-value:");
Set<Entry<String, String>> entrySet=animal.entrySet();
for(Entry<String, String> entry:entrySet){
System.out.print(entry.getKey()+"-");;
System.out.println(entry.getValue());
}
System.out.println();
System.out.println("*****************************************");
//通过单词找到注释并输出
//使用keySet方法
System.out.println("请输入要查找的单词:");
String strSearch=console.next();
//1.取得keySet
Set<String> keySet=animal.keySet();
//2.遍历keySet
for(String key:keySet){
if(strSearch.equals(key)){
System.out.println("找到了!"+"键值对为:"+key+"-"+animal.get(key));
break;
}
}
}
}
输出结果
请输入三组单词对应的注释,并存放到HashMap中
请输入key值(单词):
dog
请输入value值(注释):
狗
请输入key值(单词):
cat
请输入value值(注释):
猫
请输入key值(单词):
pig
请输入value值(注释):
猪
*****************************************
使用迭代器输出所有的value:
猫 狗 猪
*****************************************
通过entrySet方法得到key-value:
cat-猫
dog-狗
pig-猪
*****************************************
请输入要查找的单词:
pig
找到了!键值对为:pig-猪
Process finished with exit code 0
public class Goods {
private String id;//商品编号
private String name;//商品名称
private double price;//商品价格
//构造方法
public Goods(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
//getter和setter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "商品编号:" + id + ",商品名称:" + name + ",商品价格:" + price;
}
}
public class GoodTest {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
// 定义HashMap对象
Map<String, Goods> goodsMap = new HashMap<String, Goods>();
System.out.println("请输入三条商品信息:");
int i = 0;
while (i < 3) {
System.out.println("请输入第" + (i + 1) + "条商品信息:");
System.out.println("请输入商品编号:");
String goodsId = console.next();
// 判断商品编号id是否存在
if (goodsMap.containsKey(goodsId)) {
System.out.println("该商品编号已经存在!请重新输入!");
continue;
}
System.out.println("请输入商品名称:");
String goodsName = console.next();
System.out.println("请输入商品价格:");
double goodsPrice = 0;
try {
goodsPrice = console.nextDouble();
} catch (java.util.InputMismatchException e) {
System.out.println("商品价格的格式不正确,请输入数值型数据!");
console.next();
continue;
}
Goods goods = new Goods(goodsId, goodsName, goodsPrice);
// 将商品信息添加到HashMap中
goodsMap.put(goodsId, goods);
i++;
}
// 遍历Map,输出商品信息
System.out.println("商品的全部信息为:");
Iterator<Goods> itGoods = goodsMap.values().iterator();
while (itGoods.hasNext()) {
System.out.println(itGoods.next());
}
}
}
输出结果
请输入三条商品信息:
请输入第1条商品信息:
请输入商品编号:
01
请输入商品名称:
鼠标
请输入商品价格:
99.9
请输入第2条商品信息:
请输入商品编号:
01
该商品编号已经存在!请重新输入!
请输入第2条商品信息:
请输入商品编号:
02
请输入商品名称:
键盘
请输入商品价格:
199.9
请输入第3条商品信息:
请输入商品编号:
03
请输入商品名称:
主机
请输入商品价格:
九百
商品价格的格式不正确,请输入数值型数据!
请输入第3条商品信息:
请输入商品编号:
03
请输入商品名称:
主机
请输入商品价格:
999
商品的全部信息为:
商品编号:01,商品名称:鼠标,商品价格:99.9
商品编号:02,商品名称:键盘,商品价格:199.9
商品编号:03,商品名称:主机,商品价格:999.0
Process finished with exit code 0
LinkedList使用
1、 概述
- 与ArrayList一样,LinkedList也按照索引位置排序,但它的元素之间是双向链接的
- 适合快速地插入和删除元素
- LinkedList实现List和Queue两个接口
2、 构造方法
方法名 | 说明 |
---|---|
LinkedList() | 构造一个空列表 |
LinkedList(Collection<? extends E> c) | 构造一个包含指定 collection 中的元素的列表,这些元素按其 collection 的迭代器 |
返回的顺序排列 |
3、 常用方法
方法名 | 说明 |
---|---|
boolean add(E e) | 将指定元素添加到此列表的结尾 |
void add(int index, E element) | 在此列表中指定的位置插入指定的元素 |
boolean addAll(Collection<? extends E> c) | 添加指定 collection 中的所有元素到此列表的结尾 |
boolean addAll(int index, Collection<? extends E> c) | 将指定 collection 中的所有元素从指定位置开始插入此列表 |
void addFirst(E e) | 将指定元素插入此列表的开头 |
void addLast(E e) | 将指定元素添加到此列表的结尾 |
void clear() | 从此列表中移除所有元素 |
boolean contains(Object o) | 如果此列表包含指定元素,则返回 true |
E get(int index) | 返回此列表中指定位置处的元素 |
E getFirst() | 返回此列表的第一个元素 |
E getLast() | 返回此列表的最后一个元素 |
int indexOf(Object o) | 返回此列表中首次出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1 |
int lastIndexOf(Object o) | 返回此列表中最后出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1 |
E peek() | 获取但不移除此列表的头(第一个元素) |
E peekFirst() | 获取但不移除此列表的第一个元素;如果此列表为空,则返回 null |
E peekLast() | 获取但不移除此列表的最后一个元素;如果此列表为空,则返回 null |
E poll() | 获取并移除此列表的头(第一个元素) |
E pollFirst() | 获取并移除此列表的第一个元素;如果此列表为空,则返回 null |
E pollLast() | 获取并移除此列表的最后一个元素;如果此列表为空,则返回 null |
E pop() | 从此列表所表示的堆栈处弹出一个元素 |
void push(E e) | 将元素推入此列表所表示的堆栈 |
E remove() | 获取并移除此列表的头(第一个元素) |
E remove(int index) | 移除此列表中指定位置处的元素 |
boolean remove(Object o) | 从此列表中移除首次出现的指定元素(如果存在) |
E removeFirst() | 移除并返回此列表的第一个元素 |
E set(int index, E element) | 将此列表中指定位置的元素替换为指定的元素 |
int size() | 返回此列表的元素数 |
Object[] toArray() | 返回以适当顺序(从第一个元素到最后一个元素)包含此列表中所有元素的数组 |
4、 案例
(1) 案例一:使用LinkedList对字符串进行管理
import java.util.LinkedList;
public class LindedListDemo1 {
public static void main(String[] args) {
LinkedList<String> list=new LinkedList<String>();
//向链表添加数据
list.add("apple");
list.add("pear");
//将数据添加到链表的开始
list.addFirst("banana");
//将数据添加到链表的末尾
list.addLast("grape");
//在指定位置处添加数据,第一个参数为index值,从0开始
list.add(2, "orange");
//显示链表中的所有数据
System. out .println(list);
//判断列表中是否包含指定的元素,并输出相应的结果
boolean flag=list.contains("grape");
if(flag){
System. out .println("grape找到了!");
}else{
System. out .println("grape没找到!");
}
//返回index值为3的数据并输出
System. out .println("index值为3的数据为:"+list.get(3));
//返回第一个元素
System. out .println("第一个元素为:"+list.getFirst());
//返回最后一个元素
System. out .println("最后一个元素为:"+list.getLast());
}
}
运行结果为:
[banana,apple,orange,pear,grape]
grape找到了!
index值为3的数据为:pear
第一个元素为:banana
最后一个元素为:grape
(2)案例二:使用LinkedList对自定义类进行管理
Student类:
package com.linkedlist;
public class Student {
private String stuNum;
private String stuName;
private int age;
public Student(String stuNum,String stuName,int age){
this.stuNum=stuNum;
this.stuName=stuName;
this.age=age;
}
public String getStuNum() {
return stuNum;
}
public void setStuNum(String stuNum) {
this.stuNum = stuNum;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return " [学号:" + stuNum + ", 姓名:" + stuName + ", 年龄:"
+ age + "]";
}
}
LinkedListDemo2类:
import java.util.LinkedList;
public class LinkedListDemo2 {
public static void main(String[] args) {
LinkedList<Student> stuList=new LinkedList<Student>();
Student stu1=new Student("001","Mike",18);
Student stu2=new Student("002","Jack",20);
Student stu3=new Student("003","Lucy",19);
//将学生添加到链表,使用push完成
//LinkedList实现List接口的同时,也实现了Queue接口
//push和pop就是针对Queue进行添加和取出数据的操作的
stuList.push(stu1);
stuList.push(stu2);
stuList.push(stu3);
System. out .println("链表为:"+stuList);
//弹出一个元素,这里可以把链表看成一个容器,先加入到链表的数据
后弹出,
//依据的原则是先进后出
System. out .println("弹出的数据为:"+stuList.pop());
System. out .println("调用pop()方法后的链表为:\n"+stuList);
//peek()方法获取并不移除元素
System. out .println("调用peek()方法的数据为:"+stuList.peek());
System. out .println("调用peek()方法后的链表为:\n"+stuList);
//再次调用pop()方法,发现调用pop()方法后数据从链表中移除了,而
peek()方法不会
System. out .println("再次调用pop()方法"+stuList.pop());
System. out .println("再次调用pop()方法后的链表为:\n"+stuList);
//在链表中再重新添加元素
stuList.push(stu2);
stuList.push(stu3);
System. out .println("再次添加元素后的链表为:\n"+stuList);
//调用poll()方法
System. out .println("调用poll()方法输出元素"+stuList.poll());
//调用poll()方法将获取元素的同时从链表中删除了元素
System. out .println("调用poll()方法后的链表为:\n"+stuList);
}
}
运行结果为: