0% found this document useful (0 votes)
3 views10 pages

Queue&Generics

The document provides examples of using different types of queues and deques in Java, including Queue, Deque, PriorityQueue, and ArrayDeque, demonstrating their basic operations such as adding, removing, and checking elements. It also covers Java Generics with examples of a generic class and methods using unbounded, upper bounded, and lower bounded wildcards. Each section includes code snippets illustrating the functionality of these data structures and generics.

Uploaded by

Anjali Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Queue&Generics

The document provides examples of using different types of queues and deques in Java, including Queue, Deque, PriorityQueue, and ArrayDeque, demonstrating their basic operations such as adding, removing, and checking elements. It also covers Java Generics with examples of a generic class and methods using unbounded, upper bounded, and lower bounded wildcards. Each section includes code snippets illustrating the functionality of these data structures and generics.

Uploaded by

Anjali Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Queue/PriorityQueue

Deque/ArrayDeque

import [Link];
import [Link];

public class QueueExample {


public static void main(String[] args) {
// Creating a Queue using LinkedList
Queue<String> queue = new LinkedList<>();

// Adding elements to the queue


[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");

// Displaying the queue


[Link]("Queue: " + queue);

// Removing elements from the queue (FIFO)


[Link]("Removed: " + [Link]());
[Link]("Queue after removal: " + queue);

// Viewing the front element


[Link]("Front element: " + [Link]());

// Checking if queue is empty


[Link]("Is queue empty? " + [Link]());
}
}
------------------------------------------------------------------------------------
import [Link];
import [Link];

public class DequeExample {


public static void main(String[] args) {
// Creating a Deque using LinkedList
Deque<String> deque = new LinkedList<>();

// Adding elements to the front and back


[Link]("Apple"); // Add to front
[Link]("Banana"); // Add to back
[Link]("Cherry"); // Another way to add to front
[Link]("Date"); // Another way to add to back

// Displaying the deque


[Link]("Deque: " + deque);

// Removing elements from both ends


[Link]("Removed from front: " +
[Link]());
[Link]("Removed from back: " +
[Link]());

// Displaying the deque after removals


[Link]("Deque after removal: " + deque);

// Checking front and rear elements


[Link]("Front element: " + [Link]());
[Link]("Rear element: " + [Link]());

// Checking if deque is empty


[Link]("Is deque empty? " + [Link]());
}
}
------------------------------------------------------------------------------------
import [Link];

public class PriorityQueueExample {


public static void main(String[] args) {
// Creating a PriorityQueue of Integers (Min-Heap by default)
PriorityQueue<Integer> pq = new PriorityQueue<>();

// Adding elements
[Link](40);
[Link](10);
[Link](30);
[Link](20);

// Displaying the PriorityQueue (order may not be sorted)


[Link]("PriorityQueue: " + pq);

// Removing elements (Always removes the smallest element


first)
[Link]("Removed element: " + [Link]());
[Link]("PriorityQueue after removal: " + pq);

// Checking the front element


[Link]("Top element: " + [Link]());

// Checking if queue is empty


[Link]("Is PriorityQueue empty? " +
[Link]());
}
}
------------------------------------------------------------------------------------
import [Link];
import [Link];
public class ArrayDequeExample {
public static void main(String[] args) {
// Creating an ArrayDeque
Deque<String> deque = new ArrayDeque<>();

// Adding elements at both ends


[Link]("Apple"); // Add at the front
[Link]("Banana"); // Add at the rear
[Link]("Cherry"); // Another way to add at the front
[Link]("Date"); // Another way to add at the rear

// Displaying the deque


[Link]("Deque: " + deque);

// Removing elements from both ends


[Link]("Removed from front: " +
[Link]());
[Link]("Removed from back: " +
[Link]());

// Displaying the deque after removals


[Link]("Deque after removal: " + deque);

// Checking front and rear elements


[Link]("Front element: " + [Link]());
[Link]("Rear element: " + [Link]());

// Checking if deque is empty


[Link]("Is deque empty? " + [Link]());
}
}
------------------------------------------------------------------------------------
Java Generics
// Generic class with a type parameter T
class Box<T> {
private T value;

public void setValue(T value) {


[Link] = value;
}

public T getValue() {
return value;
}
}

public class GenericExample {


public static void main(String[] args) {
// Creating a Box for Integer
Box<Integer> intBox = new Box<>();
[Link](100);
[Link]("Integer Value: " + [Link]());

// Creating a Box for String


Box<String> strBox = new Box<>();
[Link]("Hello Generics");
[Link]("String Value: " + [Link]());
}
}
------------------------------------------------------------------------------------
Unbounded Wildcard (<?>)
Accepts Any Type
import [Link];
import [Link];

public class WildcardExample {


// Method with an unbounded wildcard
public static void printList(List<?> list) {
for (Object item : list) {
[Link](item + " ");
}
[Link]();
}
public static void main(String[] args) {
List<Integer> intList = new ArrayList<>();
[Link](10);
[Link](20);

List<String> strList = new ArrayList<>();


[Link]("Hello");
[Link]("World");

[Link]("Integer List: ");


printList(intList); // Works with Integer

[Link]("String List: ");


printList(strList); // Works with String
}
}
------------------------------------------------------------------------------------
Upper Bounded Wildcard (<? extends T>)
Restrict to a Type and its subclasses
import [Link];
import [Link];

public class UpperBoundWildcard {


// Method to sum numbers (Integer, Double, etc.)
public static double sumNumbers(List<? extends Number> list) {
double sum = 0;
for (Number num : list) {
sum += [Link]();
}
return sum;
}

public static void main(String[] args) {


List<Integer> intList = new ArrayList<>();
[Link](10);
[Link](20);

List<Double> doubleList = new ArrayList<>();


[Link](5.5);
[Link](2.5);

[Link]("Sum of Integers: " + sumNumbers(intList));


[Link]("Sum of Doubles: " +
sumNumbers(doubleList));
}
}
--------------------------------------------------------------------------------------------
Lower Bounded Wildcard (<? super T>)
Accepts a Type and Its Superclasses
import [Link];
import [Link];

public class LowerBoundWildcard {


// Method that adds numbers to a list of Integer or its
superclasses
public static void addNumbers(List<? super Integer> list) {
[Link](10);
[Link](20);
}

public static void main(String[] args) {


List<Number> numList = new ArrayList<>();
addNumbers(numList); // Works with Number (superclass of
Integer)
[Link]("Number List: " + numList);
}
}
------------------------------------------------------------------------------------

You might also like