SlideShare a Scribd company logo
Copyright © 2021 Infogain Corporation. All Rights Reserved.
Collection in Java
Part-008
Objectives
◌ Collection interface
◌ List interface
◌ ArrayList
◌ Differences between ArrayList and Vector ?
◌ Getting synchronized version of ArrayList object
◌ LinkedList
Introduction to Java Collections
◌ If we want to represent a group of objects as single entity then we should go for collections.
◌ A collection is an object that groups multiple elements into a single unit. Collections are used to store, retrieve and
manipulate data.
◌ Collections framework in Java helps us in representing and manipulating collections.
◌ It is a collection of interfaces and classes which helps in storing and processing the data efficiently. Collections
Framework standardizes the way we store and access the data from collections and is a part of the java.util package.
Differences between Arrays and Collections ?
Arrays Collections
1) Arrays are fixed in size. 1) Collections are growable in nature.
2) Memory point of view arrays are not recommended
to use.
2) Memory point of view collections are highly
recommended to use.
3) Performance point of view arrays are
recommended to use.
3) Performance point of view collections are not
recommended to use.
4) Arrays can hold only homogeneous data type
elements.
4) Collections can hold both homogeneous and
heterogeneous elements.
5) There is no underlying data structure for arrays and
hence there is no readymade method support.
5) Every collection class is implemented based on
some standard data structure and hence readymade
method support is available.
6) Arrays can hold both primitives and object types. 6) Collections can hold only objects but not primitives.
Hierarchy of the Java Collections framework.
The Collections hierarchy
◌ The following list describes the various interfaces of the collection framework:
◌ Collection - the root of the collection hierarchy
◌ A collection represents a group of objects known as its elements.
◌ Some types of collections allow duplicate elements and others do not. Some are ordered and others are unordered.
◌ Set - a collection that cannot contain duplicate elements
◌ List - an ordered collection
◌ Lists can contain duplicate elements.
◌ A programmer can access elements by their index(position) and has control over where each element is inserted in the list.
◌ Queue and Deque (pronounced as "Deck") - collections used to hold multiple elements prior to processing
◌ Besides basic collection operations, a Queue and a Deque provides additional insertion, extraction, and inspection operations.
◌ Queue
◌ Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues.
◌ In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules.
◌ Deque
◌ Deques can be used both as FIFO (first-in-first-out) and LIFO (last-in-first-out).
◌ In a Deque, elements can be inserted, retrieved and removed from both ends.
Collection:
◌ If we want to represent a group of "individual objects" as a single entity then we should go for collection.
◌ In general we can consider collection as root interface of entire collection framework.
◌ Collection interface defines the most common methods which can be applicable for any collection object.
◌ There is no concrete class which implements Collection interface directly
List:
◌ It is the child interface of Collection.
◌ If we want to represent a group of individual objects as a single entity where "duplicates are allow and insertion
order must be preserved" then we should go for List interface
ArrayList Method Description
Add an item:
◌ The code below shows how to add food items to the ArrayList.
food.add("Noodles"); // Adding the elements
food.add("Burger");
food.add("Pasta");
food.add("Sandwich");
Modify an item:
◌ The code below shows how to modify a food item present in a specified index of the ArrayList.
food.set(1, "Pizza"); // Modifying the element at a specified index
Check for an element:
◌ The code below checks whether the food item is present in the ArrayList or not.
food.contains("Pasta"); // Checking whether the element is present or not
Remove an item:
◌ Let us remove pasta from the ArrayList. Pasta is present at the second index position of the ArrayList. The position of
the element to be removed should be specified in the method.
food.remove(2); // Removing the element from the second index position
ArrayList Demo:
import java.util.ArrayList;
import java.util.List;
class Tester {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<Integer>(); // Creating an ArrayList object
// Adding the elements to the list
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(6);
System.out.println("numbers list: " + numbers);
// Adding the number 15 at a particular index (index: 3) in the ArrayList
numbers.add(3, 15);
System.out.println("Observe the index position 3: " + numbers);
// Finding the size of the ArrayList
System.out.println("Size of the ArrayList: " + numbers.size());
// Retrieving the element at a specified index
System.out.println("The number present at the fifth index position is " + numbers.get(5));
// Modifying the element at a specified index (index: 2)
numbers.set(2, 200);
System.out.println("The number at the 2nd index position is changed from 3 to 200");
}
}
Introduction to Generics:
◌ Let us now look at the declaration of the Collection interface.
◌ You can notice <T> in the declaration which you have not used before.
◌ The <T> syntax tells you that the interface is generic.
public interface Collection<T> ... {
}
Generic classes:
◌ Until 1.4v a non-generic version of ArrayList class is declared as follows..
◌ add() method can take object as the argument and hence we can add any type of object to the ArrayList. Due to this
we are not getting type safety.
◌ The return type of get() method is object hence at the time of retrieval compulsory we should perform type casting.
class ArrayList
{
add(Object o);
Object get(int index);
}
Generic classes:
◌ But in 1.5v a generic version of ArrayList class is declared as follows.
◌ Based on our requirement T will be replaced with our provided type.
◌ For Example to hold only string type of objects we can create ArrayList object as follows.
ArrayList<String> l=new ArrayList<String>();
Generic classes:
◌ But in 1.5v a generic version of ArrayList class is declared as follows.
◌ Based on our requirement T will be replaced with our provided type.
◌ For Example to hold only string type of objects we can create ArrayList object as follows.
ArrayList<String> l=new ArrayList<String>();
Generic Types:
◌ Consider the following Container class.
public class Container {
private Object object;
public void set(Object object) {
this.object = object;
}
public Object get() {
return object;
}
}
o This non-generic Container class operates on objects of any type.
o Since its methods accept or return an Object, you can pass whatever you want but there is no way to
verify at compile time, how the class is being used. Someone may place an Integer in the container and
expect to get Integers out of it, while someone else may pass in a String resulting in a runtime error.
Generic version of the Container Class
◌ A generic class is defined with the following format:
class name<T1, T2, ..., Tn> { /* ... */ }
o The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type
parameters (also called type variables) T1, T2, ... and Tn.
o Container class can be updated with Generics as follows:
public class Container<T> {
// T stands for "Type"
private T t;
public void set(T t) {
this.t = t;
}
public T get() {
return t;
}
}
o As you can see, all occurrences of Objects are replaced by T.
o A type variable can be any non-primitive type you specify,
any class type, any interface type, any array type or even
another type variable
Generic version of the Container Class
◌ A generic class is defined with the following format:
class name<T1, T2, ..., Tn> { /* ... */ }
o The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type
parameters (also called type variables) T1, T2, ... and Tn.
o Container class can be updated with Generics as follows:
public class Container<T> {
// T stands for "Type"
private T t;
public void set(T t) {
this.t = t;
}
public T get() {
return t;
}
}
o As you can see, all occurrences of Objects are replaced by T.
o A type variable can be any non-primitive type you specify,
any class type, any interface type, any array type or even
another type variable
Generic Methods
◌ In addition to generic types, type parameters can also be used to define generic methods.
◌ Generic methods are methods that introduce their own type parameters.
◌ This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is
declared. You can create static and non-static generic methods and generic constructors.
◌ The syntax for a generic method includes a list of type parameters inside angle brackets which appears before the
method's return type.
public static <E> void method() {
}
Generic Types - Tryout
class Container<T> {
private T t;
public void set(T t) {
this.t = t;
}
public T get() {
return t;
}
}
class Tester {
public static void main(String[] args) {
Container<Integer> integerContainer = new Container<>();
integerContainer.set(1);
//integerContainer.set("Jeo");//Uncomment the code and check if String can be
passed to the set() method
System.out.println("Inside Integer Container : "+integerContainer.get());
Container<String> stringContainer = new Container<>();
//stringContainer.set(1); //Uncomment the code and check if Integer can be passed
to the set() method
stringContainer.set("Jeo");
System.out.println("Inside String Container : "+stringContainer.get());
}
}
Generic Method - Tryout
class GenericDemo{
//Generic Method
public static <E> void display(E[] arr) {
for (E element : arr) {
System.out.println(element);
}
}
public static void main(String[] args) {
String[] names= { "Luke", "Mia", "Mathew" };
display(names);
System.out.println();
Integer[] numbers = { 1, 2, 3, 4, 5 };
display(numbers);
}
}
ArrayList - Tryout 1
import java.util.ArrayList; // Importing the ArrayList class
import java.util.List;
class Tester {
public static void main(String[] args) {
List<String> food = new ArrayList<String>(); // Creating a list of String elements
food.add("Pizza"); // Adding elements
food.add("Burger");
food.add("Pasta");
food.add("Sandwich");
System.out.println("Food items: " + food);
}
}
ArrayList - Tryout 2
import java.util.ArrayList;
import java.util.List;
class Tester {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<Integer>(); // Creating an ArrayList object
// Adding the elements to the list
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(6);
System.out.println("numbers list: " + numbers);
// Adding the number 15 at a particular index (index: 3) in the ArrayList
numbers.add(3, 15);
System.out.println("Observe the index position 3: " + numbers);
// Finding the size of the ArrayList
System.out.println("Size of the ArrayList: " + numbers.size());
// Retrieving the element at a specified index
System.out.println("The number present at the fifth index position is " + numbers.get(5));
// Modifying the element at a specified index (index: 2)
numbers.set(2, 200);
System.out.println("The number at the 2nd index position is changed from 3 to 200");
}
}
Iterating through ArrayList using for-each - Tryout
import java.util.ArrayList;
import java.util.List;
class Student {
private int studentId;
private String studentName;
private boolean courseRegistered;
public Student(int studentId, String studentName, boolean courseRegistered) {
this.studentId = studentId;
this.studentName = studentName;
this.courseRegistered = courseRegistered;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public boolean getCourseRegistered() {
return courseRegistered;
}
public void setCourseRegistered(boolean courseRegistered) {
this.courseRegistered = courseRegistered;
}
}
class Tester {
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
students.add(new Student(1001, "Steve", true));
students.add(new Student(1002, "Rachel", false));
students.add(new Student(1003, "Monica", true));
students.add(new Student(1004, "David", true));
List<String> studentNames = new ArrayList<String>();
for (Student student : students) {
studentNames.add(student.getStudentName());
System.out.println("Student Id: " + student.getStudentId())
System.out.println("Student Name: " +
student.getStudentName());
System.out.println("Course Registered: " +
student.getCourseRegistered());
}
System.out.println("========================================
===");
System.out.println("Student Names: " + studentNames);
}
}

More Related Content

PDF
Collection framework (completenotes) zeeshan
PPTX
Generic Programming &amp; Collection
PPTX
Generic Programming &amp; Collection
PPT
Generics Collections
PPT
Generics collections
PPT
inner-classes-abstract-classevgdddfs.ppt
PPTX
Collections
DOCX
ArrayList.docx
Collection framework (completenotes) zeeshan
Generic Programming &amp; Collection
Generic Programming &amp; Collection
Generics Collections
Generics collections
inner-classes-abstract-classevgdddfs.ppt
Collections
ArrayList.docx

Similar to Collection in java to store multiple values.pptx (20)

PPTX
COLLECTIONS.pptx
PPTX
collection framework.pptx
PDF
Lecture 7- Iterator and for loop over arrays
PPTX
Array list(1)
PDF
ESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdf
PPTX
VTUOOPMCA5THMODULECollection OverV .pptx
PPTX
mca5thCollection OverViCollection O.pptx
PPTX
VTUOOPMCA5THMODULECollection OverVi.pptx
PPTX
VTUOOPMCA5THMODULEvCollection OverV.pptx
PPTX
Java New Programming Features
PPT
M251_Meeting 8 (SetsandMap Advanced Java).ppt
PPT
M251_Meeting 8 (Sets and Maps_Java_).ppt
PPT
3 functions and class
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
PPTX
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
PDF
Java Collections Tutorials
PPT
List in java
PPTX
DOC-20240812-WA0000 array string and.pptx
PPTX
Ch-2ppt.pptx
COLLECTIONS.pptx
collection framework.pptx
Lecture 7- Iterator and for loop over arrays
Array list(1)
ESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdf
VTUOOPMCA5THMODULECollection OverV .pptx
mca5thCollection OverViCollection O.pptx
VTUOOPMCA5THMODULECollection OverVi.pptx
VTUOOPMCA5THMODULEvCollection OverV.pptx
Java New Programming Features
M251_Meeting 8 (SetsandMap Advanced Java).ppt
M251_Meeting 8 (Sets and Maps_Java_).ppt
3 functions and class
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
Java Collections Tutorials
List in java
DOC-20240812-WA0000 array string and.pptx
Ch-2ppt.pptx
Ad

More from ASHUTOSH TRIVEDI (9)

PPT
agile-simple-guide-to-creating-project-burndown-chart-201025172824.ppt
PPTX
Docker Introduction How to create Image.pptx
PPTX
CircullarLinkedList notes for data sturcture
PPTX
Manual Testing Test Case To Learn Part-2.pptx
PPTX
Arrays are used in java to holding more.pptx
PDF
ServletConfig & ServletContext
PPTX
Identifier
PPTX
Type cast operator
PPTX
JAVA Literals
agile-simple-guide-to-creating-project-burndown-chart-201025172824.ppt
Docker Introduction How to create Image.pptx
CircullarLinkedList notes for data sturcture
Manual Testing Test Case To Learn Part-2.pptx
Arrays are used in java to holding more.pptx
ServletConfig & ServletContext
Identifier
Type cast operator
JAVA Literals
Ad

Recently uploaded (20)

PDF
Well-logging-methods_new................
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Construction Project Organization Group 2.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
composite construction of structures.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Digital Logic Computer Design lecture notes
Well-logging-methods_new................
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
OOP with Java - Java Introduction (Basics)
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
UNIT 4 Total Quality Management .pptx
R24 SURVEYING LAB MANUAL for civil enggi
Embodied AI: Ushering in the Next Era of Intelligent Systems
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Construction Project Organization Group 2.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
composite construction of structures.pdf
Internet of Things (IOT) - A guide to understanding
CYBER-CRIMES AND SECURITY A guide to understanding
Digital Logic Computer Design lecture notes

Collection in java to store multiple values.pptx

  • 1. Copyright © 2021 Infogain Corporation. All Rights Reserved. Collection in Java Part-008
  • 2. Objectives ◌ Collection interface ◌ List interface ◌ ArrayList ◌ Differences between ArrayList and Vector ? ◌ Getting synchronized version of ArrayList object ◌ LinkedList
  • 3. Introduction to Java Collections ◌ If we want to represent a group of objects as single entity then we should go for collections. ◌ A collection is an object that groups multiple elements into a single unit. Collections are used to store, retrieve and manipulate data. ◌ Collections framework in Java helps us in representing and manipulating collections. ◌ It is a collection of interfaces and classes which helps in storing and processing the data efficiently. Collections Framework standardizes the way we store and access the data from collections and is a part of the java.util package.
  • 4. Differences between Arrays and Collections ? Arrays Collections 1) Arrays are fixed in size. 1) Collections are growable in nature. 2) Memory point of view arrays are not recommended to use. 2) Memory point of view collections are highly recommended to use. 3) Performance point of view arrays are recommended to use. 3) Performance point of view collections are not recommended to use. 4) Arrays can hold only homogeneous data type elements. 4) Collections can hold both homogeneous and heterogeneous elements. 5) There is no underlying data structure for arrays and hence there is no readymade method support. 5) Every collection class is implemented based on some standard data structure and hence readymade method support is available. 6) Arrays can hold both primitives and object types. 6) Collections can hold only objects but not primitives.
  • 5. Hierarchy of the Java Collections framework.
  • 6. The Collections hierarchy ◌ The following list describes the various interfaces of the collection framework: ◌ Collection - the root of the collection hierarchy ◌ A collection represents a group of objects known as its elements. ◌ Some types of collections allow duplicate elements and others do not. Some are ordered and others are unordered. ◌ Set - a collection that cannot contain duplicate elements ◌ List - an ordered collection ◌ Lists can contain duplicate elements. ◌ A programmer can access elements by their index(position) and has control over where each element is inserted in the list. ◌ Queue and Deque (pronounced as "Deck") - collections used to hold multiple elements prior to processing ◌ Besides basic collection operations, a Queue and a Deque provides additional insertion, extraction, and inspection operations. ◌ Queue ◌ Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues. ◌ In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. ◌ Deque ◌ Deques can be used both as FIFO (first-in-first-out) and LIFO (last-in-first-out). ◌ In a Deque, elements can be inserted, retrieved and removed from both ends.
  • 7. Collection: ◌ If we want to represent a group of "individual objects" as a single entity then we should go for collection. ◌ In general we can consider collection as root interface of entire collection framework. ◌ Collection interface defines the most common methods which can be applicable for any collection object. ◌ There is no concrete class which implements Collection interface directly
  • 8. List: ◌ It is the child interface of Collection. ◌ If we want to represent a group of individual objects as a single entity where "duplicates are allow and insertion order must be preserved" then we should go for List interface
  • 10. Add an item: ◌ The code below shows how to add food items to the ArrayList. food.add("Noodles"); // Adding the elements food.add("Burger"); food.add("Pasta"); food.add("Sandwich");
  • 11. Modify an item: ◌ The code below shows how to modify a food item present in a specified index of the ArrayList. food.set(1, "Pizza"); // Modifying the element at a specified index
  • 12. Check for an element: ◌ The code below checks whether the food item is present in the ArrayList or not. food.contains("Pasta"); // Checking whether the element is present or not
  • 13. Remove an item: ◌ Let us remove pasta from the ArrayList. Pasta is present at the second index position of the ArrayList. The position of the element to be removed should be specified in the method. food.remove(2); // Removing the element from the second index position
  • 14. ArrayList Demo: import java.util.ArrayList; import java.util.List; class Tester { public static void main(String[] args) { List<Integer> numbers = new ArrayList<Integer>(); // Creating an ArrayList object // Adding the elements to the list numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); numbers.add(6); System.out.println("numbers list: " + numbers); // Adding the number 15 at a particular index (index: 3) in the ArrayList numbers.add(3, 15); System.out.println("Observe the index position 3: " + numbers); // Finding the size of the ArrayList System.out.println("Size of the ArrayList: " + numbers.size()); // Retrieving the element at a specified index System.out.println("The number present at the fifth index position is " + numbers.get(5)); // Modifying the element at a specified index (index: 2) numbers.set(2, 200); System.out.println("The number at the 2nd index position is changed from 3 to 200"); } }
  • 15. Introduction to Generics: ◌ Let us now look at the declaration of the Collection interface. ◌ You can notice <T> in the declaration which you have not used before. ◌ The <T> syntax tells you that the interface is generic. public interface Collection<T> ... { }
  • 16. Generic classes: ◌ Until 1.4v a non-generic version of ArrayList class is declared as follows.. ◌ add() method can take object as the argument and hence we can add any type of object to the ArrayList. Due to this we are not getting type safety. ◌ The return type of get() method is object hence at the time of retrieval compulsory we should perform type casting. class ArrayList { add(Object o); Object get(int index); }
  • 17. Generic classes: ◌ But in 1.5v a generic version of ArrayList class is declared as follows. ◌ Based on our requirement T will be replaced with our provided type. ◌ For Example to hold only string type of objects we can create ArrayList object as follows. ArrayList<String> l=new ArrayList<String>();
  • 18. Generic classes: ◌ But in 1.5v a generic version of ArrayList class is declared as follows. ◌ Based on our requirement T will be replaced with our provided type. ◌ For Example to hold only string type of objects we can create ArrayList object as follows. ArrayList<String> l=new ArrayList<String>();
  • 19. Generic Types: ◌ Consider the following Container class. public class Container { private Object object; public void set(Object object) { this.object = object; } public Object get() { return object; } } o This non-generic Container class operates on objects of any type. o Since its methods accept or return an Object, you can pass whatever you want but there is no way to verify at compile time, how the class is being used. Someone may place an Integer in the container and expect to get Integers out of it, while someone else may pass in a String resulting in a runtime error.
  • 20. Generic version of the Container Class ◌ A generic class is defined with the following format: class name<T1, T2, ..., Tn> { /* ... */ } o The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, ... and Tn. o Container class can be updated with Generics as follows: public class Container<T> { // T stands for "Type" private T t; public void set(T t) { this.t = t; } public T get() { return t; } } o As you can see, all occurrences of Objects are replaced by T. o A type variable can be any non-primitive type you specify, any class type, any interface type, any array type or even another type variable
  • 21. Generic version of the Container Class ◌ A generic class is defined with the following format: class name<T1, T2, ..., Tn> { /* ... */ } o The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, ... and Tn. o Container class can be updated with Generics as follows: public class Container<T> { // T stands for "Type" private T t; public void set(T t) { this.t = t; } public T get() { return t; } } o As you can see, all occurrences of Objects are replaced by T. o A type variable can be any non-primitive type you specify, any class type, any interface type, any array type or even another type variable
  • 22. Generic Methods ◌ In addition to generic types, type parameters can also be used to define generic methods. ◌ Generic methods are methods that introduce their own type parameters. ◌ This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. You can create static and non-static generic methods and generic constructors. ◌ The syntax for a generic method includes a list of type parameters inside angle brackets which appears before the method's return type. public static <E> void method() { }
  • 23. Generic Types - Tryout class Container<T> { private T t; public void set(T t) { this.t = t; } public T get() { return t; } } class Tester { public static void main(String[] args) { Container<Integer> integerContainer = new Container<>(); integerContainer.set(1); //integerContainer.set("Jeo");//Uncomment the code and check if String can be passed to the set() method System.out.println("Inside Integer Container : "+integerContainer.get()); Container<String> stringContainer = new Container<>(); //stringContainer.set(1); //Uncomment the code and check if Integer can be passed to the set() method stringContainer.set("Jeo"); System.out.println("Inside String Container : "+stringContainer.get()); } }
  • 24. Generic Method - Tryout class GenericDemo{ //Generic Method public static <E> void display(E[] arr) { for (E element : arr) { System.out.println(element); } } public static void main(String[] args) { String[] names= { "Luke", "Mia", "Mathew" }; display(names); System.out.println(); Integer[] numbers = { 1, 2, 3, 4, 5 }; display(numbers); } }
  • 25. ArrayList - Tryout 1 import java.util.ArrayList; // Importing the ArrayList class import java.util.List; class Tester { public static void main(String[] args) { List<String> food = new ArrayList<String>(); // Creating a list of String elements food.add("Pizza"); // Adding elements food.add("Burger"); food.add("Pasta"); food.add("Sandwich"); System.out.println("Food items: " + food); } }
  • 26. ArrayList - Tryout 2 import java.util.ArrayList; import java.util.List; class Tester { public static void main(String[] args) { List<Integer> numbers = new ArrayList<Integer>(); // Creating an ArrayList object // Adding the elements to the list numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); numbers.add(6); System.out.println("numbers list: " + numbers); // Adding the number 15 at a particular index (index: 3) in the ArrayList numbers.add(3, 15); System.out.println("Observe the index position 3: " + numbers); // Finding the size of the ArrayList System.out.println("Size of the ArrayList: " + numbers.size()); // Retrieving the element at a specified index System.out.println("The number present at the fifth index position is " + numbers.get(5)); // Modifying the element at a specified index (index: 2) numbers.set(2, 200); System.out.println("The number at the 2nd index position is changed from 3 to 200"); } }
  • 27. Iterating through ArrayList using for-each - Tryout import java.util.ArrayList; import java.util.List; class Student { private int studentId; private String studentName; private boolean courseRegistered; public Student(int studentId, String studentName, boolean courseRegistered) { this.studentId = studentId; this.studentName = studentName; this.courseRegistered = courseRegistered; } public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public boolean getCourseRegistered() { return courseRegistered; } public void setCourseRegistered(boolean courseRegistered) { this.courseRegistered = courseRegistered; } } class Tester { public static void main(String[] args) { List<Student> students = new ArrayList<Student>(); students.add(new Student(1001, "Steve", true)); students.add(new Student(1002, "Rachel", false)); students.add(new Student(1003, "Monica", true)); students.add(new Student(1004, "David", true)); List<String> studentNames = new ArrayList<String>(); for (Student student : students) { studentNames.add(student.getStudentName()); System.out.println("Student Id: " + student.getStudentId()) System.out.println("Student Name: " + student.getStudentName()); System.out.println("Course Registered: " + student.getCourseRegistered()); } System.out.println("======================================== ==="); System.out.println("Student Names: " + studentNames); } }