0% found this document useful (0 votes)
12 views7 pages

Generic Collections

Uploaded by

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

Generic Collections

Uploaded by

mshivanshmaurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name of the School: School of Computer Science and

Engineering
Course Code: E1UA307C Course Name:Java Programming

DEPARTMENT OF COMPUTER SCIENCE


& ENGINEERING
Subject Name: Java Programming

Topics Covered: - Generic Collections: Generic Classes


and Methods

1
Faculty Name: Jitender Tanwar Programe Name: B.Tech
Generic Collections: Generic Classes and Methods
In object-oriented programming (OOP), generics allow you to write flexible,
reusable, and type-safe code. Generic collections are collections that can
store objects of any type, but the type is defined at the time of creation.
This allows collections to work with different types of data without
compromising type safety or needing to cast types explicitly.

In languages Java, generics provide a mechanism to create classes,


interfaces, and methods that work with any data type, ensuring that type
safety is maintained during compilation.
// A generic class Box that can hold any type of object
Generic Classes
public class Box<T> {
A generic class is a
private T value;
class that can work In this example:
public Box(T value) { // Constructor • Box<T> is a generic class
with any type of data. where T is a placeholder for any
You define a generic this.value = value; } type.
• When creating an instance of
class by using a type public T getValue() { // Getter
Box, you specify the type you
parameter, which return value; } want (e.g., Box<Integer> or
Box<String>).
acts as a placeholder public void setValue(T value) { // Setter
for the actual type this.value = value; }
that will be used public static void main(String[] args) {
later. This allows for Box<Integer> intBox = new Box<>(10); // Create a Box that holds an Integer
creating more flexible System.out.println(intBox.getValue()); // Output: 10
and reusable Box<String> strBox = new Box<>("Hello, Generics!"); // Create a Box that holds a String
classes.
System.out.println(strBox.getValue()); // Output: Hello, Generics!
}}
// A generic method that returns the maximum of two values
Generic Methods
public class GenericMethods {
A generic method
// Generic method to find maximum of two values
allows a method to
public static <T extends Comparable<T>> T findMax(T a, T b) {
operate on objects
of any type. The if (a.compareTo(b) > 0) {
type parameters are return a;
specified before the } else { In this example:
return type in the return b; } } The method findMax is
generic and works with
method declaration. public static void main(String[] args) { any type that extends
// Find the maximum of two integers Comparable<T>. This
means the types passed
System.out.println(findMax(10, 20)); // Output: 20
to the method can be
// Find the maximum of two strings compared with each
other using the
System.out.println(findMax("Apple", "Orange")); //
compareTo method.The
Output: Orange
method is used with both
}} Integer and String types.
Benefits of Generics

1.Type Safety:
•Generics allow you to catch type errors at compile-time rather than at
runtime. Without generics, you might need to cast objects, which could
lead to ClassCastException errors at runtime.

2.Code Reusability:
•You can write more reusable code that works with different types without
duplicating logic.

3.Performance:
•Generics eliminate the need for casting and work with specific types,
leading to better performance compared to using raw types (non-generic).
import java.util.ArrayList;
public class GenericCollectionExample {
Example of a Generic public static void main(String[] args) {
Collection: // Create an ArrayList that holds Integer values
Java’s ArrayList The ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
ArrayList class in Java is numbers.add(20);
a part of the java.util numbers.add(30);
package and is a generic // Print the elements in the ArrayList
for (Integer number : numbers) {
collection that can store System.out.println(number); // Output: 10, 20, 30 }
elements of any type. // Create an ArrayList that holds String values
ArrayList<String> words = new ArrayList<>();
words.add("Apple");
words.add("Banana");
words.add("Cherry");
// Print the elements in the ArrayList
for (String word : words) {
System.out.println(word); // Output: Apple, Banana, Cherry
} }}
Thank you

You might also like