Java Collection (Java - Util.class) : Content
Java Collection (Java - Util.class) : Content
class)
By Shubham Manjhi
Content :
\
ArrayDeque in Java
Array deques have no capacity restrictions and they grow as necessary to support usage.
They are not thread-safe which means that in the absence of external synchronization,
ArrayDeque does not support concurrent access by multiple threads.
Null elements are prohibited in the ArrayDeque.
ArrayDeque class is likely to be faster than Stack when used as a stack.
ArrayDeque class is likely to be faster than LinkedList when used as a queue
Declaration:
public class ArrayDeque Element
extends AbstractCollection
implements DequeElement, Cloneable, Serializable
Here, Element refers to the element which can refer to any class, such as Integer or String class.
Application :
Exceptions
include remove, removeFirstOccurrence, removeLastOccurrence, conta
ins, iterator.remove(), and the bulk operations, all of which run in linear
time.
DrawBack :
The iterators returned by this class's iterator method are fail-fast : If the deque is
modified at any time after the iterator is created, in any way except through the
iterator's own remove method, the iterator will generally throw
a ConcurrentModificationException. Thus, in the face of concurrent modification, the
iterator fails quickly and cleanly, rather than risking arbitrary, non -deterministic
behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally
speaking, impossible to make any hard guarantees in the presence of unsynchronized
concurrent modification. Fail-fast iterators throw ConcurrentModificationException on
a best-effort basis. Therefore, it would be wrong to write a program that depended on
this exception for its correctness: the fail-fast behavior of iterators should
be used only to detect bugs.
Java ArrayDeque Class: add() Method
Or
The add() method is used to insert an given element at the end of this deque.
public Boolean add(E e)
{
Add to last of queue;
}
Package: java.util
Java Platform: Java SE 8
Syntax: add(E e) .
Parameters: e = The element to add.
Return Value: true (as specified by Collection.add(E))
Return Value Type: Boolean
NullPointerException - if the specified element is nullThrows:
Example: Java ArrayDeque Class: add() Method
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
// Create an empty array deque with an initial capacity 5
Deque<Integer> deque = new ArrayDeque<Integer>(5);
The addFirst() method is used to insert an given element at the front of this
deque.
public Boolean addFirst(E e)
{
Add to First position of dequeue;
}
Package: java.util
Java Platform: Java SE 8
Syntax: addFirst(E e)
Parameters: e = The element to add.
Return Value Type: void for addFirst() and Boolean for offerFirst().
Throws: NullPointerException - if the specified element is null.
The addLast() method is used to insert a given element at the end of this
deque.
Syntax: addLast(E e)
Return Value Type: void for addLast() and Boolean for offerLast()
import java.util.ArrayDeque;
import java.util.Deque;
Output:
Number = 10,Number = 20,Number = 30,Number = 40,Number = 10,
Number = 20,Number = 30,Number = 40,Number = 50,Number = 10,
Number = 20,Number = 30,Number = 40,Number = 50,Number = 60
{
Clear the dequeu.
Package: java.util
Java Platform: Java SE 8
Syntax: clear()
Return Value Type: void
Output:
Number = 10
Number = 20
Number = 30
After clearing all the elements in the deque:
Java ArrayDeque Class: clone() Method
Package: java.util
Syntax:
clone()
Return Value:
Pictorial Presentation
Example: Java ArrayDeque Class: clone() Method
import java.util.ArrayDeque;
import java.util.Deque;
deque.add(10);
deque.add(20);
deque.add(30);
deque.add(40);
System.out.println("Original deque:");
System.out.println("Coloned deque:");
Copy
Output:
Original deque:
Number = 10
Number = 20
Number = 30
Number = 40
Coloned deque:
Number = 10
Number = 20
Number = 30
Number = 40
Java ArrayDeque Class: contains() Method
More formally, returns true if and only if this deque contains at least one
element e such that o.equals(e).
Package: java.util
Syntax:
contains(Object o)
Parameters:
Name Description
Return Value:
Pictorial Presentation
Example: Java ArrayDeque Class: contains() Method
import java.util.ArrayDeque;
import java.util.Deque;
StudentList.add("David");
StudentList.add("Tom");
StudentList.add("Rohit");
StudentList.add("Paul");
System.out.println(StudentList);
System.out.println(StudentList.contains("Tom"));
System.out.println(StudentList.contains("Sudhir"));
Copy
Output:
Students in the list are :
[David, Tom, Rohit, Paul]
Is list contains the student Tom?true
Is list contains the student Sudhir?false
Java ArrayDeque Class: descendingIterator()
Method
Package: java.util
Syntax:
descendingIterator()
Return Value:
Return Value Type: Iterator<E - the type of elements held in this collection>
Pictorial Presentation
Example: Java ArrayDeque Class: descendingIterator() Method
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
deque.add(10);
deque.add(20);
deque.add(30);
deque.add(40);
System.out.println(itr.next());
System.out.println(desc_itrr.next());
Copy
Output:
Print all the elements of the original deque:
10
20
30
40
Print all the elements of the original deque in reverse order:
40
30
20
10
Java ArrayDeque Class: element() or peek()
Method
public E element()
The element() method is used to retrieve the head of the queue represented
by this deque. This method differs from peek only in that it throws an
exception if this deque is empty.
Package: java.util
Syntax:
element()
Return Value:
Throws:
Pictorial Presentation
Example: Java ArrayDeque Class: element() Method
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
deque.add(10);
deque.add(20);
deque.add(30);
deque.add(40);
// Print all the elements of the original deque
System.out.println(n);
int e = deque.element();
Copy
Output:
Original elements of the deque:
10
20
30
40
Retrieved element: 10
Java ArrayDeque Class: getFirst() or peekFirst
Method
public E getFirst()
The getFirst() method is used to retrieve the first element of a given deque.
This method differs from peekFirst only in that it throws an exception if this
deque is empty.
Package: java.util
Syntax:
getFirst()
Return Value:
Throws:
Pictorial Presentation
Example: Java ArrayDeque Class: getFirst() Method
import java.util.ArrayDeque;
import java.util.Deque;
deque.add(10);
deque.add(20);
deque.add(30);
deque.add(40);
System.out.println(n);
int e = deque.getFirst();
Copy
Output:
Powered by
Original elements of the deque:
10
20
30
40
Retrieve the first element: 10
Java ArrayDeque Class: getLast() or peekLast()
Method
public E getLast()
The getLast() method is used to retrieve the last element of a given deque.
This method differs from peekLast only in that it throws an exception if this
deque is empty.
Package: java.util
Syntax:
getLast()
Return Value:
Throws:
Pictorial Presentation
Example: Java ArrayDeque Class: getLast() Method
import java.util.ArrayDeque;
import java.util.Deque;
deque.add(10);
deque.add(20);
deque.add(30);
deque.add(40);
System.out.println(n);
int e = deque.getLast();
Copy
Output:
Original elements of the deque:
10
20
30
40
Retrieve the last element: 40
Java ArrayDeque Class: isEmpty() Method
Package: java.util
Syntax:
isEmpty()
Return Value:
Pictorial Presentation:
Example: Java ArrayDeque Class: isEmpty() Method
import java.util.ArrayDeque;
import java.util.Deque;
if (deque.isEmpty())
else
Copy
Output:
The ArrayDeque is empty.
Java ArrayDeque Class: iterator() Method
Package: java.util
Syntax:
iterator()
Return Value:
Pictorial Presentation
Example: Java ArrayDeque Class: iterator() Method
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
deque.add(10);
deque.add(20);
deque.add(30);
deque.add(25);
System.out.println(n);
System.out.println(itr.next());
}
}
Copy
Output:
Original elements of the deque:
10
20
30
25
Print all the elements using iterator:
10
20
30
25
Java ArrayDeque Class: poll() Method
public E poll()
The poll() method is used to retrieve and remove the head of the queue
represented by a given deque (in other words, the first element of this deque).
Package: java.util
Syntax:
poll()
Return Value:
the head of the queue represented by this deque, or null if this deque is empty
Pictorial Presentation
Example: Java ArrayDeque Class: poll() Method
import java.util.ArrayDeque;
import java.util.Deque;
deque.add(200);
deque.add(150);
deque.add(95);
Copy
Output:
Powered by
Elements of the original deque:
Number = 100
Number = 200
Number = 150
Number = 95
Removed element: 100
Number = 200
Number = 150
Number = 95
Java ArrayDeque Class: pollFirst() or
removeFirst() or poll Method
public E pollFirst()
The pollFirst() method is used to retrieve and remove the first element of a
given deque.
Package: java.util
Syntax:
pollFirst()
Return Value:
Pictorial Presentation
Example: Java ArrayDeque Class: pollFirst() Method
import java.util.ArrayDeque;
import java.util.Deque;
deque.add(100);
deque.add(200);
deque.add(150);
deque.add(95);
Copy
Output:
Elements of the original deque:
Number = 100
Number = 200
Number = 150
Number = 95
Removed element: 100
Number = 200
Number = 150
Number = 95
Java ArrayDeque Class: pollLast() or removeLast()
Method
public E pollLast()
The pollLast() method is used to retrieve and remove the last element of a
given deque, or returns null if this deque is empty.
Package: java.util
Syntax:
pollLast()
Return Value:
Pictorial Presentation
Example: Java ArrayDeque Class: pollLast() Method
import java.util.ArrayDeque;
import java.util.Deque;
deque.add(100);
deque.add(200);
deque.add(150);
deque.add(95);
Copy
Output:
Elements of the original deque:
Number = 100
Number = 200
Number = 150
Number = 95
Removed element: 95
Number = 100
Number = 200
Number = 150
Java ArrayDeque Class: removeFirstOccurrence()
Method
Package: java.util
Syntax:
removeFirstOccurrence(Object o)
Parameters:
Name Description
Return Value:
Pictorial Presentation
Example: Java ArrayDeque Class: removeFirstOccurrence() Method
import java.util.ArrayDeque;
import java.util.Deque;
deque.add(100);
deque.add(200);
deque.add(150);
deque.add(95);
deque.add(200);
deque.removeFirstOccurrence(200);
System.out.println("New deque:");
Copy
Output:
Elements of the original deque:
Number = 100
Number = 200
Number = 150
Number = 95
Number = 200
New deque:
Number = 100
Number = 150
Number = 95
Number = 200
Java ArrayDeque Class: removeLastOccurrence()
Method
If the deque does not contain the element, it is unchanged. More formally,
removes the last element e such that o.equals(e) (if such an element exists).
Package: java.util
Syntax:
removeLastOccurrence(Object o)
Parameters:
Name Description
Return Value:
Pictorial Presentation
Example: Java ArrayDeque Class: removeLastOccurrence() Method
import java.util.ArrayDeque;
import java.util.Deque;
deque.add(100);
deque.add(200);
deque.add(150);
deque.add(95);
deque.add(200);
deque.removeLastOccurrence(200);
System.out.println("New deque:");
Copy
Output:
Elements of the original deque:
Number = 100
Number = 200
Number = 150
Number = 95
Number = 200
New deque:
Number = 100
Number = 200
Number = 150
Number = 95
The size() method is used to count the number of elements in a given deque.
Package: java.util
Syntax:
size()
Return Value:
Pictorial Presentation
Example: Java ArrayDeque Class: size() Method
import java.util.ArrayDeque;
import java.util.Deque;
deque.add(100);
deque.add(200);
deque.add(150);
deque.add(95);
deque.add(200);
Copy
Output:
Elements of the original deque:
Number = 100
Number = 200
Number = 150
Number = 95
Number = 200
Size of deque = 5
Commented [SM1]: import java.util.Arrays;
Java.util.Arrays Class
public class ArrayDemo {
Class methods public static void main(String[] args) {
// sorting array
Arrays.sort(byteArr);
1 static <T> List<T> asList(T... a)
// let us print all the elements
available in list
How to use it : List list1 = Arrays.asList(a); System.out.println("The sorted byte
array is:");
This method returns a fixed-size list backed by the specified array. // entering the value to be searched
byte searchVal = 35;
This method searches the specified array of bytes for the specified value System.out.println("The index of
element 35 is : " + retVal);
using the binary search algorithm. }
}
3 static int binarySearch(byte[] a, int fromIndex, int toIndex, byte
key)
Commented [SM2]: import java.util.Arrays;
This method searches a range of the specified array of bytes for the public class ArrayDemo {
public static void main(String[] args) {
specified value using the binary search algorithm.
// initializing unsorted byte array
byte byteArr[] = {10,20,15,22,35};
4 static int binarySearch(char[] a, char key)
// sorting array
This method searches the specified array of chars for the specified value Arrays.sort(byteArr);
using the binary search algorithm. // let us print all the elements
available in list
System.out.println("The sorted byte
5 static int binarySearch(char[] a, int fromIndex, int toIndex, char array is:");
key) for (byte number : byteArr) {
System.out.println("Number = " +
number);
This method searches a range of the specified array of chars for the }
specified value using the binary search algorithm. // entering the value to be searched
byte searchVal = 35;
This method searches a range of the specified array of doubles for the
specified value using the binary search algorithm.
This method searches the specified array of floats for the specified value
using the binary search algorithm.
This method searches a range of the specified array of floats for the
specified value using the binary search algorithm.
This method searches the specified array of ints for the specified value
using the binary search algorithm.
This method searches a range of the specified array of ints for the
specified value using the binary search algorithm.
This method searches a range of the specified array of longs for the
specified value using the binary search algorithm.
This method searches the specified array of longs for the specified value
using the binary search algorithm.
This method searches a range of the specified array for the specified
object using the binary search algorithm.
15 static int binarySearch(Object[] a, Object key)
This method searches the specified array for the specified object using
the binary search algorithm.
This method searches a range of the specified array of shorts for the
specified value using the binary search algorithm.
This method searches the specified array of shorts for the specified value
using the binary search algorithm.
This method searches a range of the specified array for the specified
object using the binary search algorithm.
This method searches the specified array for the specified object using
the binary search algorithm.
This method copies the specified array, truncating or padding with false
(if necessary) so the copy has the specified length.
This method copies the specified array, truncating or padding with zeros
(if necessary) so the copy has the specified length.
This method copies the specified array, truncating or padding with null
characters (if necessary) so the copy has the specified length.
23 static double[] copyOf(double[] original, int newLength)
This method copies the specified array, truncating or padding with zeros
(if necessary) so the copy has the specified length.
This method copies the specified array, truncating or padding with zeros
(if necessary) so the copy has the specified length.
This method copies the specified array, truncating or padding with zeros
(if necessary) so the copy has the specified length.
This method copies the specified array, truncating or padding with zeros
(if necessary) so the copy has the specified length.
This method copies the specified array, truncating or padding with zeros
(if necessary) so the copy has the specified length.
This method copies the specified array, truncating or padding with nulls
(if necessary) so the copy has the specified length.
This method copies the specified array, truncating or padding with nulls
(if necessary) so the copy has the specified length.
This method copies the specified range of the specified array into a new
array.
This method copies the specified range of the specified array into a new
array.
This method copies the specified range of the specified array into a new
array.
This method copies the specified range of the specified array into a new
array.
This method copies the specified range of the specified array into a new
array.
This method copies the specified range of the specified array into a new
array.
This method copies the specified range of the specified array into a new
array.
This method copies the specified range of the specified array into a new
array.
This method copies the specified range of the specified array into a new
array.
40 static boolean deepEquals(Object[] a1, Object[] a2)
This method returns true if the two specified arrays are deeply equal to
one another.
This method returns a hash code based on the "deep contents" of the
specified array.
This method returns true if the two specified arrays of booleans are equal
to one another.
This method returns true if the two specified arrays of bytes are equal to
one another.
This method returns true if the two specified arrays of chars are equal to
one another.
This method returns true if the two specified arrays of doubles are equal
to one another.
This method returns true if the two specified arrays of floats are equal to
one another.
This method returns true if the two specified arrays of longs are equal to
one another.
This method returns true if the two specified arrays of Objects are equal
to one another.
This method returns true if the two specified arrays of shorts are equal to
one another.
This method assigns the specified boolean value to each element of the
specified array of booleans.
This method assigns the specified boolean value to each element of the
specified range of the specified array of booleans.
This method assigns the specified byte value to each element of the
specified array of bytes.
This method assigns the specified byte value to each element of the
specified range of the specified array of bytes.
This method assigns the specified char value to each element of the
specified array of chars.
57 static void fill(char[] a, int fromIndex, int toIndex, char val)
This method assigns the specified char value to each element of the
specified range of the specified array of chars.
This method assigns the specified double value to each element of the
specified array of doubles.
This method assigns the specified double value to each element of the
specified range of the specified array of doubles.
This method assigns the specified float value to each element of the
specified array of floats.
This method assigns the specified float value to each element of the
specified range of the specified array of floats.
This method assigns the specified int value to each element of the
specified array of ints.
This method assigns the specified int value to each element of the
specified range of the specified array of ints.
This method assigns the specified long value to each element of the
specified range of the specified array of longs.
This method assigns the specified Object reference to each element of the
specified range of the specified array of Objects.
This method assigns the specified Object reference to each element of the
specified array of Objects.
This method assigns the specified short value to each element of the
specified range of the specified array of shorts.
This method assigns the specified short value to each element of the
specified array of shorts.
This method returns a hash code based on the contents of the specified
array.
This method returns a hash code based on the contents of the specified
array.
This method returns a hash code based on the contents of the specified
array.
This method returns a hash code based on the contents of the specified
array.
74 static int hashCode(float[] a)
This method returns a hash code based on the contents of the specified
array.
This method returns a hash code based on the contents of the specified
array.
This method returns a hash code based on the contents of the specified
array.
This method returns a hash code based on the contents of the specified
array.
This method returns a hash code based on the contents of the specified
array.
This method sorts the specified array of bytes into ascending numerical
order.
This method sorts the specified range of the specified array of bytes into
ascending numerical order.
This method sorts the specified array of chars into ascending numerical
order.
This method sorts the specified array of doubles into ascending numerical
order.
This method sorts the specified range of the specified array of doubles
into ascending numerical order.
This method sorts the specified array of floats into ascending numerical
order.
This method sorts the specified range of the specified array of floats into
ascending numerical order.
This method sorts the specified array of ints into ascending numerical
order.
This method sorts the specified range of the specified array of ints into
ascending numerical order.
This method sorts the specified array of longs into ascending numerical
order.
This method sorts the specified range of the specified array of longs into
ascending numerical order.
91 static void sort(Object[] a)
This method sorts the specified array of objects into ascending order,
according to the natural ordering of its elements.
This method sorts the specified range of the specified array of objects into
ascending order, according to the natural ordering of its elements.
This method sorts the specified array of shorts into ascending numerical
order.
This method sorts the specified range of the specified array of shorts into
ascending numerical order.
This method sorts the specified array of objects according to the order
induced by the specified comparator.
This method sorts the specified range of the specified array of objects
according to the order induced by the specified comparator.
1
java.util.Collections.addAll() Commented [SM3]: import java.util.*;
Description : This method adds all of the specified public class CollectionsDemo {
public static void main(String args[]) {
System.out.println("Initial
collection value: "+arrlist);
Return Value
}
Let us compile and run the above program, this
will produce the following result.
The method call returns 'true' if the collection changed as a result of the
Initial collection value: [A, B, C]
call Final collection value: [A, B, C, 1, 2, 3]
Exception
UnsupportedOperationException − This is thrown if c does not support the
add method.
3
java.util.Collections.binarySearch() Commented [SM5]: import java.util.*;
System.out.println("'QUALITY' is
list − This is the list to be searched. available at index: "+index);
}
key − This is the key to be searched for. }
Return Value : The method call returns the index of the
search key, if it is contained in the list.
Exception : ClassCastException − This is thrown if the list
contains elements that are not mutually
comparable.
4
java.util.Collections.binarySearch()
Description : This method searches the specified list for the specified
object using the binary search algorithm in specific range.
c − This is the comparator by which the list is ordered. A null value indicates
that the elements' natural ordering should be used.
Return Value
The method call returns the index of the search key, if it is contained in
the list.
Exception
ClassCastException − This is thrown if the list contains elements that
are not mutually comparable using the specified comparator.
5 static <E> Collection<E> checkedCollection(Collection<E> c, Class<E>
type)
This method returns a dynamically typesafe view of the specified sorted map.
This method returns a dynamically typesafe view of the specified sorted set.
11 static <T> void copy(List<? super T> dest, List<? extends T> src)
This method copies all of the elements from one list into another.
This method returns true if the two specified collections have no elements in
common.
This method replaces all of the elements of the specified list with the specified
element.
This method returns the number of elements in the specified collection equal to the
specified object.
This method returns the starting position of the first occurrence of the specified
target list within the specified source list, or -1 if there is no such occurrence.
This method returns the starting position of the last occurrence of the specified target
list within the specified source list, or -1 if there is no such occurrence.
This method returns an array list containing the elements returned by the specified
enumeration in the order they are returned by the enumeration.
22 static <T extends Object & Comparable<? super T> >T max(Collection<?
extends T> coll)
This method returns the maximum element of the given collection, according to the
natural ordering of its elements.
This method returns the maximum element of the given collection, according to the
order induced by the specified comparator.
This method Returns the minimum element of the given collection, according to the
natural ordering of its elements.
This method returns the minimum element of the given collection, according to the
order induced by the specified comparator.
This method returns an immutable list consisting of n copies of the specified object.
This method replaces all occurrences of one specified value in a list with another.
This method reverses the order of the elements in the specified list.
This method returns a comparator that imposes the reverse of the natural ordering
on a collection of objects that implement the Comparable interface.
This method rotates the elements in the specified list by the specified distance.
This method randomly permutes the specified list using a default source of
randomness.
This method randomly permute the specified list using the specified source of
randomness.
This method returns an immutable set containing only the specified object.
This method returns an immutable list containing only the specified object.
This method returns an immutable map, mapping only the specified key to the
specified value.
This method sorts the specified list into ascending order, according to the natural
ordering of its elements.
This method sorts the specified list according to the order induced by the specified
comparator.
This method swaps the elements at the specified positions in the specified list.
41 static <T> Collection<T> synchronizedCollection(Collection<T> c)
This method returns a synchronized (thread-safe) list backed by the specified list.
This method returns a synchronized (thread-safe) map backed by the specified map.
This method returns a synchronized (thread-safe) set backed by the specified set.
This method returns a synchronized (thread-safe) sorted set backed by the specified
sorted set.