Java.util.ArrayDeque Class in Java | Set 1
Last Updated :
22 Aug, 2022
java.util.ArrayDeque class describes an implementation of a resizable array structure which implements the Deque interface.
Array deques has not immutable and can grow as necessary. They are not thread-safe and hence concurrent access by multiple threads is not supported unless explicitly synchronized. Null elements are invalid in this structure. Most operations run in constant time, with speed decreasing linearly as size grows.
Declaration
public class ArrayDeque
extends AbstractCollection
implements Deque, Cloneable, Serializable
Methods of ArrayDeque Class :
1. add(Element e) : java.util.ArrayDeque.add(Element e) inserts particular element at the end of the deque.
Syntax :
public boolean add(Element e)
Parameter
e : element to add
Return
true : if element is inserted, else no
2. addFirst(Element e) : java.util.ArrayDeque.addFirst(Element e) inserts particular element at the start of the deque.
Syntax :
public boolean addFirst(Element e)
Parameter
e : element to add at the start
Return
true : if element is inserted, else no
3. addLast(Element e) : java.util.ArrayDeque.addLast(Element e) inserts particular element at the end of the deque. It is similar to add() method
Syntax :
public boolean addaddLast(Element e)
Parameter
e : element to add at end
Return
true : if element is inserted, else no
4. clear() : java.util.ArrayDeque.clear() removed all deque elements.
Syntax :
public void clear()
Parameter
-------
Return
-------
5. size() : java.util.ArrayDeque.size() returns the no. of elements in deque.
Syntax :
public int size()
Parameter
-----
Return
no. of elements in deque.
6. clone() : java.util.ArrayDeque.clone()copies the deque.
Syntax :
public ArrayDeque clone()
Parameter
--------
Return
copy of deque
7. contains(Obj) : java.util.ArrayDeque.contains(Obj) checks whether a deque contains the element or not
Syntax :
public boolean contains(Object obj)
Parameter
obj : object to be checked
Return
true, if element is +nt in the deque; else false
8. Iterator() : java.util.ArrayDeque.Iterator() returns an iterator over the deque.
Syntax :
public Iterator Iterator()
Parameter
----------
Return
iterator over the deque.
9. descendingIterator() : java.util.ArrayDeque.descendingIterator() returns a reverse order iterator over the deque
Syntax :
public Iterator descendingIterator()
Parameter
----------
Return
returns a reverse order iterator over the deque.
10. element() : java.util.ArrayDeque.element() returns element at the head of the deque
Syntax :
public E element()
Parameter
------
Return
head element of the deque
11. getFirst: java.util.ArrayDeque.getFirst()returns first element of the deque
Syntax :
public E getFirst()
Parameter
------
Return
head element of the deque
12. getLast: java.util.ArrayDeque.getLast()returns last element of the deque
Syntax :
public E getLast()
Parameter
------
Return
last element of the deque
13. isEmpty(): java.util.ArrayDeque.isEmpty() checks whether the deque is empty or not.
Syntax :
public boolean isEmpty()
Parameter
------
Return
true if the deque is empty; else false
14. toArray(): java.util.ArrayDeque.toArray() returns array having the elements of deque.
Syntax :
public Object[] toArray()
Parameter
------
Return
returns array having the elements of deque.
Java Program explaining ArrayDeque class methods :
Java
// Java code explaining the use of ArrayDeque class methods
// add(), addFirst(), addLast(), clear(), size(), contains()
// descendingIterator(), element(), getFirst(), isEmpty(),
// toArray, Iterator(), getLast()
import java.util.*;
public class NewClass
{
public static void main(String[] args)
{
// Initializing an deque
Deque<Integer> d = new ArrayDeque<Integer>(10);
// add() method to insert
d.add(2);
d.add(4);
d.add(6);
d.add(8);
d.add(2);
for (Integer element : d)
{
System.out.println("Element : " + element);
}
System.out.println("Using clear() ");
//clear() method
d.clear();
// addFirst() method to insert at start
d.addFirst(10);
d.addFirst(20);
// addLast() method to insert at end
d.addLast(24);
d.addLast(14);
System.out.println("Above elements are removed now \n");
//size() of ArrayDeque
System.out.println("Size of deque : " + d.size() + "\n");
for (Integer element : d)
{
System.out.println("Element : " + element);
}
// contains() method
System.out.println("\nIs 10 +nt in deque : " + d.contains(10));
// Iterator() :
System.out.println("\nElements of deque using Iterator :");
for(Iterator itr = d.iterator(); itr.hasNext();)
{
System.out.println(itr.next());
}
// descendingIterator() : to reverse the deque order
System.out.println("\nElements of deque in reverse order :");
for(Iterator dItr = d.descendingIterator(); dItr.hasNext();)
{
System.out.println(dItr.next());
}
// element() method : to get Head element
System.out.println("\nHead Element using element(): " + d.element());
// getFirst() method : to get Head element
System.out.println("Head Element using getFirst(): " + d.getFirst());
// getLast() method : to get last element
System.out.println("Last Element using getLast(): " + d.getLast());
// isEmpty() method :
System.out.println("\nChecks whether deque is empty : " + d.isEmpty());
//toArray() method :
Object[] arr = d.toArray();
System.out.println("\nArray Size : " + arr.length);
System.out.print("Array elements : ");
for(int i=0; i<arr.length ; i++)
System.out.print(" " + arr[i]);
}
}
Output :
Element : 2
Element : 4
Element : 6
Element : 8
Element : 2
Using clear()
Above elements are removed now
Size of deque : 4
Element : 20
Element : 10
Element : 24
Element : 14
Is 10 +nt in deque : true
Elements of deque using Iterator :
20
10
24
14
Elements of deque in reverse order :
14
24
10
20
Head Element using element(): 20
Head Element using getFirst(): 20
Last Element using getLast(): 14
Checks whether deque is empty : false
Array Size : 4
Array elements : 20 10 24 14
Java.util.ArrayDeque Class in Java | Set 2
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While
7 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read