JAVA学习日记:集合框架(5)

本节课我学习的主要内容有:

1.简单队列
2.优先队列
3.双端队列

简单队列:

		先进先出。
 		之前学过的LinkedList就实现了队列的接口。
 		
 		方法:
 			以下方法都不会抛出异常。
 			offer();我认为是入队。
 			peek();检索队头。
 			poll();我认为是出队。
 
 		与上面对应的但是不同的方法:
 			以下方法都在一定条件下会抛出异常。
 			add();添加。
 			element();同样是检索。
 			remove();移除。

优先队列:

		通过PriorityQueue类来实现。
 			
 		这种优先队列中的元素的关系是有优先关系优先级的,同时元素之间
 		也是有可比较性或者比较能力的。
 
 		元素之间优先级别高的最先出队。

双端队列:

		实现Deque接口来表示,通常使用LinkedList类实现Deque接口。
 		双端队列可以在队列两端出对入队。

QueueTest类(测试各种队列):

package LessonForQueue;

import java.util.*;

class Game1
{
	private String price;

	public Game1(String price) 
	{
		super();
		this.price = price;
	}

	public String getPrice() 
	{
		return price;
	}
	
	@Override
	public String toString()
	{
		return this.getPrice();
	}
}

//class Game2Comparator implements Comparator<Game2>手写比较器
//{
//	@Override
//	public int compare(Game2 g1, Game2 g2) 
//	{
//		return g1.;
//	}
//}

class Game implements Comparable<Game>
{
	private String price;

	public Game(String price) 
	{
		super();
		this.price = price;
	}

	public String getPrice() 
	{
		return price;
	}
	
	@Override
	public String toString()
	{
		return this.getPrice();
	}

	@Override
	public int compareTo(Game g) //使元素具有可比较性
	{
		return this.getPrice().compareTo(g.getPrice());
	}
}

public class QueueTest 
{
	public static void main(String[] args) 
	{
		System.out.println("----------------简单队列	-----------------");
		//简单队列实现
		Queue<String> q1 = new LinkedList<String>();
		q1.add("a");
		q1.add("b");
		q1.add("c");
		q1.add("d");
		while (q1.peek() != null)//peek()方法没有peek到元素返回的是null,不会抛出异常。
		{
			System.out.println("此时删除的元素"+q1.peek());
			System.out.println("此时队列中的元素还有"+q1.toString());//调用的是Object类的toString方法/
			q1.poll();
			//poll方法我觉得可以叫做出队,检索队头并出队
		}
		
		System.out.println("----------------优先队列	-----------------");
		
		//优先队列
		Game g1 = new Game("a1");
		Game g2 = new Game("c3");
		Game g3 = new Game("b2");
		
		PriorityQueue<Game> q2 = new PriorityQueue<Game>();
		//也可以Queue<Game> q2 = new PriorityQueue<Game>();
		q2.offer(g1);
		q2.offer(g2);
		q2.offer(g3);
		//自然排序不可行。得实现Comparable接口使元素具有可比较性。
		System.out.println(q2.toString());//打印输出
		System.out.println("遍历但不出队");
		Iterator<Game> i1 = q2.iterator();
		while (i1.hasNext())
		{
			System.out.println("此时q2中的元素有:"+i1.next());//遍历但不出队
		}
		
		System.out.println("遍历但出队");
		while (q2.peek() != null)
		{
			System.out.println("此时q2中的元素有:"+q2.poll().getPrice());//遍历时会根据你的comparTo方法来排序输出
		}
		
		//用比较器
		Game1 g4 = new Game1("a1");
		Game1 g5 = new Game1("c3");
		Game1 g6 = new Game1("b2");
		PriorityQueue<Game1> q3 = new PriorityQueue<Game1>(Comparator.comparing(Game1::getPrice));//方法引用来实现比较器
		q3.offer(g4);
		q3.offer(g5);
		q3.offer(g6);
		//自然排序不可行。得实现Comparable接口使元素具有可比较性。
		System.out.println(q3.toString());//打印输出
		System.out.println("遍历但不出队");
		Iterator<Game1> i2 = q3.iterator();
		while (i2.hasNext())
		{
			System.out.println("此时q2中的元素有:"+i2.next());//遍历但不出队
		}
		
		System.out.println("----------------双端队列	-----------------");
		
		//双端队列没有优先队列的比较
		Deque<String> d1 = new LinkedList<String>();//用链表来初始化双端队列
		d1.offerFirst("a");
		d1.offerFirst("b");
		d1.offerLast("d");
		d1.offerLast("c");
		
		System.out.println(d1);
		while (d1.peekFirst() != null)
		{
			System.out.println("此时队列中从头出队"+d1.pollFirst());
		}
		
		d1.offerFirst("a");
		d1.offerFirst("b");
		d1.offerLast("d");
		d1.offerLast("c");
		
		while (d1.peekLast() != null)
		{
			System.out.println("此时队列中从尾出队"+d1.pollLast());
		}
		
	}
}

本篇部分文字来源于:
咕嘟咖啡杨海滨老师 — 《java编程语言高级特性》
在这里十分感谢老师能够给我带来学习的激情。

2020.11.2
本文章是本人学习笔记,不进行任何商用所以不支持转载请理解!也请别拿去商用!
如果觉得对你有帮助那么欢迎你随时来回顾!
只为记录本人学习历程。
毕
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值