Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Stack Queue Deque in the Java Collection Framework

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use Stack Queue Deque in the Java collection framework". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Stack Queue Deque in the Java collection framework.

Catalogue

1. Stack

1.1 introduction

1.2 Common methods

2. Queue

2.1 introduction

2.2 Common methods

3. Deque

3.1 introduction

3.2 Common methods

1. Stack1.1 introduction

The Stack stack, a subclass of Vector, implements a standard LIFO stack. At the bottom is an array.

The stack defines only the default constructor, which is used to create an empty stack. The stack not only includes all the methods defined by Vector, but also defines some of its own methods.

1.2 description of common methods E push (E item) push stack E pop () push stack E peek () check the top elements of the stack, do not delete boolean empty () to determine whether the stack is empty

Note: the following examples are all taken out of a separate code, and there is actually a logical relationship between the top and bottom.

Example 1: using Stack to construct a stack whose elements are shaped

Stack stack = new Stack ()

Example 2: stack pressing

Stack.push (1); stack.push (2); stack.push (3); / / result: [1,2,3]

Example 3: check the elements at the top of the stack without deleting them.

System.out.println (stack.peek ()); System.out.println (stack); / / results are: 3 and [1,2,3]

Example 4: out of stack

System.out.println (stack.pop ()); System.out.println (stack); / / results are: 3 and [1,2]

Example 5: determine whether the stack is empty

System.out.println (stack.empty ()) / / the result is: false2. Queue2.1 introduction

The Queue queue is a special linear table that only allows deletions at the front end of the table and inserts at the back end of the table.

The LinkedList class implements the Queue interface, so we can use LinkedList as a Queue.

2.2 Common methods describe boolean offer (E) queuing (returning special value when an error occurs) boolean add (E) queuing (error throwing exception) E poll () dequeuing (returning special value when error occurs) E remove () dequeuing (error throwing exception) E peek () getting queue leader element, not deleting (returning special value when error occurs) E element () getting queue leader element Do not delete (error throws exception) boolean isEmpty () to determine whether the queue is empty

Note: next, use LinkedList to demonstrate the use of queues, as long as you follow the principle of first-in, first-out. The following examples are all taken out separately from one piece of code, and there is actually a logical relationship between the top and bottom.

Example 1: create a queue with integer elements using LinkedList

LinkedList linkedList = new LinkedList ()

Example 2: queuing

LinkedList.offer (1); linkedList.offer (2); linkedList.offer (3)

Example 3: out of queue

System.out.println (linkedList.poll ()); / / result: 1

Example 4: get the first element of the queue and do not delete it

System.out.println (linkedList.peek ()); / / result: 2

Example 5: determine whether the queue is empty

System.out.println (linkedList.isEmpty ()) / / the result is: false3. Deque3.1 introduction

A double-ended queue is a queue that allows both sides to enter and dequeue. Elements can leave and join the team from the head of the team, or from the end of the team.

The LinkedList class implements the Deque interface, so we can use LinkedList as a Deque.

3.2 Common methods to describe boolean offerFirst (E e) enter the team from the head of the team boolean offerLast (E e) enter the team from the end of the team E pollFirst () from the head of the team E pollLast () from the end of the team E peekFirst () get the head of the team, do not delete E peekLast () to get the end of the queue element, do not delete

Note: next, use LinkedList to demonstrate the use of queues, as long as you follow the principle of first-in, first-out. The following examples are all taken out separately from one piece of code, and there is actually a logical relationship between the top and bottom.

Example 1: create a queue with integer elements using LinkedList

LinkedList linkedList = new LinkedList ()

Example 2: join the team from the head of the team

LinkedList.offerFirst (1); linkedList.offerFirst (2); linkedList.offerFirst (3); / / queue is: [3,2,1]

Example 3: join the team from the end of the line

LinkedList.offerLast (7); linkedList.offerLast (8); linkedList.offerLast (9); / / queue is: [3, 2, 1, 7, 8, 9]

Example 4: get out of line at the head of the line

System.out.println (linkedList.pollFirst ()); / / result: 3

Example 5: come out from the end of the line

System.out.println (linkedList.pollLast ()); / / result: 9

Example 6: get the line header element and do not delete it

System.out.println (linkedList.peekFirst ()); / / result: 2

Example 7: get the line header element and do not delete it

System.out.println (linkedList.peekLast ()); / / the result is: 8 so far, I believe you have a better understanding of "how to use Stack Queue Deque in the Java collection framework". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report