Advanced Programming Java-H-U2
Advanced Programming Java-H-U2
There is no need to typecast the object. Before Generics, we need to type cast. This makes the code simpler
and more readable.
Below is the code snippet to iterate over a List collection without using generics:
Output
The T type indicates that it can refer to any type (like String, Integer, and Employee). The type you specify for
the class will be used to store and retrieve the data.
Using generic class:
Let's see the code to use the generic class.
Reading Assignment
Generic Interfaces
Output
Output
Output
Output
Or
Advanced Programming in Java Lecture notes by: Jerusalem F. 13
Output
Here we pass the argument as string object "Information Storage & Retrieval", so it will remove from the
collection.
Remove by index:
Here we pass the argument as index remove(1) " Information Storage & Retrieval ", so it will remove from the
collection.
Reading Assignment
HashMap, HashTable, TreeMap, Vector, HashSet, and TreeSet
Output
implements Deque interface, so LinkedList can be used to represent a first-in, first-out (FIFO) Queue.
• Deque is a double-ended queue i.e., first-in, first-out (FIFO) or last-in, first-out (LIFO) principle. As
LinkedList class implements Deque interface, so LinkedList can also be used to depict a Deque or
even a Stack (LIFO).
Advanced Programming in Java Lecture notes by: Jerusalem F. 20
Important features of LinkedList
• LinkedList allows storage of duplicate elements in it.
• LinkedList maintains an order in which elements are inserted in it.
• LinkedList can be used to depict a first-in, first-out (FIFO) or last-in, first-out (LIFO) storage.
Constructors of LinkedList:
1) LinkedList()
This constructor creates an empty LinkedList of a particular object type. Example
This constructor example creates a LinkedList arrL2 initialized with the elements of a LinkedList arrL1. Both
of the LinkedList hold Integer objects.
Some important methods of LinkedList class
Methods Description
boolean add(E e) Inserts the element e at the end of an LinkedList.
void addFirst(E e) Inserts the element e at the beginning of the LinkedList.
void addLast(E e) Inserts the element e at the end of LinkedList.
E removeFirst() Removes the first element of the LinkedList.
E removeLast(E e) Removes the last element of the LinkedList.
boolean offer(E e) Inserts the element e at the end of the LinkedList.
E peek() Returns the head(first) element of the LinkedList, without removing it.
E poll() Returns and removes the head(first) element of the LinkedList.
int size() Returns the total number of elements in an ArrayList.
void clear() Removes all of the elements from the ArrayList
E get(int index) Returns the element at the specified index in an ArrayList.
E remove(int index) Removes an element at a specified index in an ArrayList.
boolean remove(Object o) Removes the first occurrence of an Object from the ArrayList, if it is present.
Object[] toArray() Returns an Object array containing all the elements of an ArrayList.
Iterator iterator() Returns an Iterator to iterate over elements of an ArrayList.
Advanced Programming in Java Lecture notes by: Jerusalem F. 21
ListIterator listIterator() Returns a ListIterator to iterate over elements of an ArrayList.
Creating a LinkedList and displaying its features.
In this program, we have created a LinkedList to hold <String> objects and we will add/remove some elements
from this list and use it to initialize a new LinkedList.
Output
Output