0% found this document useful (0 votes)
151 views

Advanced Programming Java-H-U2

Generics were added to Java to provide type safety and eliminate casts. Generics allow a class or method to accept instances of multiple types while ensuring type safety. This document discusses why generics are used, benefits like type safety and compile-time checking, and provides examples of generic classes, interfaces, and methods in Java. Collections in Java use generics to provide type-safe container classes like ArrayList that allow storing and retrieving element values without casting.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

Advanced Programming Java-H-U2

Generics were added to Java to provide type safety and eliminate casts. Generics allow a class or method to accept instances of multiple types while ensuring type safety. This document discusses why generics are used, benefits like type safety and compile-time checking, and provides examples of generic classes, interfaces, and methods in Java. Collections in Java use generics to provide type-safe container classes like ArrayList that allow storing and retrieving element values without casting.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

AMBO UNIVERSITY WOLISO CAMPUS

School of Technology and Informatics Department of Information Technology


Course Title: Advanced Programming in Java Course Code: ITec3058, ECTS: 5
Unit Two: Generics and Collections
2.1 Why Use Generics?
Due to the demand of safer code, generics were added to the Java programming language. Indeed, it adds
stability to the code, as potential bugs can be detected at compile time.
Here are the benefits of using generics:
1) Type-safety: We can hold only a single type of objects in generics. It doesn’t allow to store other objects.
Without Generics, we can store any type of objects.
2) Type casting is not required (Elimination of casts):
we don’t have to use casts when taking elements out of a collection:

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:

Using generics, the above code can be re-written as:

3) Stronger type checks at compile time (Compile-Time Checking):


Generics guarantee that there are no wrong types added to the collections. Stronger type checking makes the
code more readable:
It is checked at compile time so problem will not occur at runtime. The good programming strategy says it is
far better to handle the problem at compile time than runtime.

Advanced Programming in Java Lecture notes by: Jerusalem F. 1


This code tells us listNames is a list of Strings and only Strings. Objects of other types if added to the list
will cause compile error.
4) Enable writing generic algorithms:
With generics, we can generalize our code for reusability, as you see in the code of the List interface
and sort() method above.

Full example of Generics in Java


Here, we are using the ArrayList class, but you can use any collection class such as ArrayList, LinkedList,
HashSet, TreeSet, HashMap, Comparator etc.

Output

2.1.1 Generic Classes

Advanced Programming in Java Lecture notes by: Jerusalem F. 2


A class that can refer to any type is known as a generic class. Here, we are using the T type parameter to create
the generic class of specific type.
Let's see a simple example to create and use the generic class.
Creating a generic class:

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.

Advanced Programming in Java Lecture notes by: Jerusalem F. 3


Output

Reading Assignment
Generic Interfaces

2.1.3 Generic Methods


Generic Method
Like the generic class, we can create a generic method that can accept any type of arguments. Here, the scope
of arguments is limited to the method where it is declared. It allows static as well as non-static methods.
Let's see a simple example of java generic method to print array elements. We are using here E to denote the
element.

Output

Advanced Programming in Java Lecture notes by: Jerusalem F. 4


2.2 Collection API
Introduction
A collection is a container for other objects. To be useful, a collection must provide methods for accessing the
objects it contains including adding and removing objects. Collections vary in terms of the types of items they
contain and in the manner in which they organize their contents. strings and arrays are examples of linear
collections i.e., the items in a linear collection are ordered by position. Each item except the first item has a
unique predecessor, and each item except the last item has a unique successor. Linear collections are also said
to be lists.
E.g., grocery lists, stacks of dinner plates, customers waiting in a line at a bank are examples of linear
collections in the daily livelihood.
Map: also called unordered collection. As the name implies, items in unordered collection are in no particular
order. We cannot meaningfully speak of an item’s predecessor or successor. E.g., a bag of marbles
We can put marbles into and take marbles out of a bag, once in the bag the marbles are in no particular order.
A map is characterized by the fact that each item is associated with a unique identifying key at the time it is
added to the collection. Later, an item is retrieved from a map by specifying its key.
e.g., if the items are employees, the keys can be their Social Security Number (SSL). Most implementations of
maps have the rather astounding capability of being able to search for and retrieve items in constant time
independent of the number of items currently stored in the map. This contrasts sharply with arrays in which
the time required to search for an item depends on the array’s size.
Lists and maps are part of java.util, one of the Java’s standard packages.
Java Collections are predefined set of classes or data structures which can be used to store multiple items in a
single unit. Dynamically allocated data structures in Java (such as Hashtable, HashSet, HashMap, LinkedList,
Vector, Stack, ArrayList) are supported in a unified architecture called the Collection Framework, which
mandates the common behaviours of all the classes.

Advanced Programming in Java Lecture notes by: Jerusalem F. 5


For many applications, the developers want to create and manage groups of related objects. There are two
options to group objects:
1) by creating arrays of objects, and
2) by creating collections of objects.
Collections provide a more flexible way to work with groups of objects. Unlike arrays, these group of objects
are powerful and can easily grow or shrink without having to redefine them. For some collections, you can
assign a key to any object that you put into the collection so that you can quickly retrieve the object by using
the key. The capacity planning and predefined algorithms for sort, search and manipulation make them easy to
use. The array, however, does not support so-called dynamic allocation - it has a fixed length which cannot be
changed once allocated. Moreover, array is a simple linear structure. Many applications may require more
complex data structure such as LinkedList, Stack, HashTable, Sets, or Trees.
The Java Collection Framework package (java.util) contains:
▪ Interfaces
▪ Implementations
▪ Algorithms
Interfaces
Java Collections Framework interfaces provides the abstract data type to represent collection. The data
structures derived from Collection interface are known as collections framework. The java.util.Collection is
the root interface of Collections Framework. It is on the top of Collections framework hierarchy. Two main
sub interfaces are List and Set. Collection interface methods are overridden by different classes in different
ways as per their requirement. Some collection classes do not accept duplicate values like HashSet and TreeSet.
The Collection interface comes with many methods so that each class derived can use well. It contains some
important methods such as size(), iterator(), add(), remove(), clear() that every Collection class must
implement. Map is the only interface that doesn't inherits from Collection interface but it's part of Collections
framework. All the collections framework interfaces are present in java.util package.
Implementations
Java Collections framework comes with many implementation classes for the interfaces. We can use them to
create different types of collections in java program. Most common implementations are ArrayList, HashMap
and HashSet. Some other important collection classes are LinkedList, TreeMap, TreeSet. These are the
concrete implementations of the collection interfaces. In essence, they are reusable data structures. These
classes solve most of our programming needs but if we need some special collection class, we can extend them
to create our custom collection class. Using an existing, common implementation makes your code shorter and
quicker to download. Also, using existing Core Java code core ensures that any improvements to the base code
will also improve the performance of your code. Java 1.5 came up with thread-safe collection classes that
Advanced Programming in Java Lecture notes by: Jerusalem F. 6
allowed to modify Collections while iterating over it, some of them are CopyOnWriteArrayList,
ConcurrentHashMap, CopyOnWriteArraySet. These classes are in java.util.concurrent package. All the
collection classes are present in java.util and java.util.concurrent package.
Algorithms
The Collection Framework also provides features to apply different algorithms on all or a few elements of a
collection, such as searching through a collection, sorting or shuffling elements of a collection, fetch a read
only view of a collection, and so forth. The great majority of the algorithms provided by the Java platform
operate on List instances, but a few of them operate on arbitrary Collection instances. The algorithms are said
to be polymorphic: that is, the same method can be used on many different implementations of the appropriate
collection interface. In essence, algorithms are reusable functionality.

Figure 2.1: Algorithm

Java Collections Framework


1) Java ArrayList class
What is an Array? An array is a container object that holds a fixed number of values of a single type or it is
an ordered collection of similar items where its items are referred in terms of their position. For example, you
are going to create an array for student marks. The Marks are stored as integer value so you can create an
integer array that holds all student marks. Suppose some students have marks from an examination but other
students have a grade. A grade is a string value like "A+", "A" etc. What do you do in this situation? You
should create two arrays, one for marks that is an integer array and another for grade that is a string array. Now
you have two arrays so you can't retrieve the result in students’ sequence because you have marks in the first
array then grades in the second array. So, when you have multiple types set then you can use an ArrayList.
ArrayList is one of the most flexible data structures from Java Collections. Arraylist is a class which
implements List interface. It is one of the widely used because of the functionality and flexibility it offers. It
is designed to hold heterogeneous collections of objects. The capacity of an ArrayList is the number of
elements the ArrayList can hold. As elements are added to an ArrayList, the capacity is dynamically increased
as required through reallocation. It can contain duplicate elements too. Elements in this collection can be
accessed using an integer index. Indexes in this collection are zero-based. It allows random access because
array works at the index basis. Moreover, it maintains insertion order.

Advanced Programming in Java Lecture notes by: Jerusalem F. 7


Figure 2.2: ArrayList

How to add an Items in an ArrayList?

How to print an ArrayList element?

Advanced Programming in Java Lecture notes by: Jerusalem F. 8


Output

How to find length/size of ArrayList in Java?


By using size() method of ArrayList class we can easily determine the size of the ArrayList. This method
returns the number of elements in an ArrayList Object.

Output

How to get specific ArrayList item?


Elements in ArrayList can be accessed using an integer index and ArrayList indexes start from zero. So
aList.get(1) return the second item from ArrayList.

Advanced Programming in Java Lecture notes by: Jerusalem F. 9


Output

How to get the first item of an ArrayList?


Since ArrayList indexes start from zero, aList.get(0) return the first item of ArrayList.

Advanced Programming in Java Lecture notes by: Jerusalem F. 10


How to get the last element of ArrayList?
The aList.size() return the total items in an ArrayList. Since ArrayList indexes start from zero, aList.size()-1
return the last item of ArrayList.

Output

How to remove all elements from Java ArrayList?


You can use two different methods to empty an arraylist in Java. They are ArrayList.clear() and
ArrayList.removeAll().
In case of removeAll, you should pass the same ArrayList as argument.

Advanced Programming in Java Lecture notes by: Jerusalem F. 11


Or

Output for both programs

Advanced Programming in Java Lecture notes by: Jerusalem F. 12


Difference between ArrayList.clear() and removeAll(collection)?
The methods clear() and removeAll(collection) serve two different purposes. The clear() method will go
through the underlying Array and set each entry to null while removeAll(collection) will go through the
ArrayList checking for collection and remove(Object) it if it exists. So it is confirm that clear() is much faster
since it doesn't have to deal with all those extra method calls. How to remove a specific item from ArrayList?
In general an object can be removed in two ways from an ArrayList (or generally any List), by index
(remove(int)) and by object (remove(Object)).
Remove by object (remove(Object)):

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.

Advanced Programming in Java Lecture notes by: Jerusalem F. 14


Output

How to sort an ArrayList in Java?


In ArrayList, elements are placed as they are inserted. But while coding, you often need them in some order.
in order to sort an ArrayList, we use sort() method of Collections class.

Advanced Programming in Java Lecture notes by: Jerusalem F. 15


Output

Sort an ArrayList in the reverse Order?

Advanced Programming in Java Lecture notes by: Jerusalem F. 16


Output

Search an item in Java ArrayList


You can check if a value exists in Java ArrayList using the following methods: ArrayList.contains(),
ArrayList.indexOf() and ArrayList.lastIndexOf()

Advanced Programming in Java Lecture notes by: Jerusalem F. 17


Output

Reading Assignment
HashMap, HashTable, TreeMap, Vector, HashSet, and TreeSet

Advanced Programming in Java Lecture notes by: Jerusalem F. 18


Summarized example for ArrayList in Java

Output

Advanced Programming in Java Lecture notes by: Jerusalem F. 19


LinkedList in Java
LinkedLists are among the simplest and most common data structures. Arrays and LinkedLists are similar
since they both store collections of data. An array allocates memory for all its elements lumped together as one
block of memory. In contrast, a LinkedList allocates space for each element separately in its own block of
memory called a Node. The main disadvantage of using arrays to store data is that arrays are static structures
and therefore cannot be easily extended or reduced to fit the data set. One disadvantage of a linked list over an
array is that it does not allow direct access to the individual elements. If you want to access a particular item
then you have to start at the head and follow the link until you get to that item.

figure 2.3: Java LinkedList


Linked list is a data structure consisting of a group of nodes which together represent a sequence. It is dynamic
in nature which allocates the memory when required. That means, the number of nodes in a list is not fixed and
can grow and shrink on demand. Any application which has to deal with an unknown number of objects is a
must to use a linked list. Each node in a Linked list contains two fields: a "data" field to store whatever element
type the list holds for its client, and a "next" field which is a pointer used to link one node to the next node.
The last node in the list has its next field set to NULL to signify the end of the list.
There are three types of linked-list. They are:
▪ Single Linked List
▪ Doubly Linked List
▪ Circular Linked List
Single LinkedList: Single linked lists contain nodes which have a data part as well as a link part i.e., next,
which points to the next node in sequence of nodes. Item navigation in Single linked lists is forward only. The
operations we can perform on Single linked lists are insertion, deletion and traversal.
Doubly LinkedList: In a doubly linked list, each node contains two links the first link points to the previous
node and the next link points to the next node in the sequence. Unlike single linked-list, items can be navigated
forward and backward in a doubly linked list.
Circular LinkedList: In a circular linked list Last item contains link of the first element as next and the first
element has a link to the last element as previous.
LinkedList is a collection class that implements List, Deque and Queue Interfaces.
• Queue stores and removes its elements based on a first-in, first-out (FIFO) principle. As, LinkedList

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 an ArrayList to hold Integer objects.


2) LinkedList(Collection <? extends E>c)
This constructor creates a LinkedList initialized with the elements of Collection c, with a condition, that
Collection c holds similar type of objects as the type declared by the LinkedList.

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

Advanced Programming in Java Lecture notes by: Jerusalem F. 22


Reading Assignment
Differentiate between ArrayList and LinkedList?

2.2.1 Interfaces and Iterators


Collection is a group of objects, which are known as elements. It is the root interface in the collection
hierarchy. This interface is basically used to pass around the collections and manipulate them where the
maximum generality is desired.
Interface Abstract class
only contain abstract method contains both abstract and non-abstract classes
supports multiple inheritance doesn’t support multiple inheritance
by default, interface methods are public + abstract no restriction on abstract method modifiers
by default, interface variables are public + static final no need to declare variables as public + static final
interface keyword is used to declare interface abstract keyword is used to declare abstract class
2.2.2 Abstract, Concrete Classes
Abstract class Concrete class
is declared using abstract keyword. is not having abstract keyword during declaration
can inherit another class using <extends> keyword can inherit only an interface
cannot be directly instantized using <new >keyword can be directly instantized using <new >keyword
cannot be declared as final can be declared as final
abstract class may or mayn’t contain abstract methods cannot contain abstract methods
2.2.3 Wildcard Parameters in Java Generics
The ‘?’ (question mark) symbol represents the wildcard element. It means any type. If we write <? extends
Number>, it means any child class of Number, e.g., Integer, Float, and double. Now we can call the method of
Number class through any child class object.
We can use a wildcard as a type of a parameter, field, return type, or local variable. However, it is not allowed
to use a wildcard as a type argument for a generic method invocation, a generic class instance creation, or a
supertype.
Let's understand it by the example given below:
Advanced Programming in Java Lecture notes by: Jerusalem F. 23
Reading Assignment
Serialization with Generics

Output

Advanced Programming in Java Lecture notes by: Jerusalem F. 24


Chapter Summary
Collection: Root interface with basic methods like add(), remove(), contains(), isEmpty(), addAll(), ... etc.
Set: Doesn't allow duplicates. Example implementations of Set interface are HashSet (Hashing based) and
TreeSet (balanced BST based). Note that TreeSet implements SortedSet.
List: Can contain duplicates and elements are ordered. Example, implementations are LinkedList (linked list
based) and ArrayList (dynamic array based)
Queue: Typically order elements in FIFO order except exceptions like PriorityQueue.
Deque: Elements can be inserted and removed at both ends. Allows both LIFO and FIFO.
Map: Contains Key value pairs. Doesn't allow duplicates. Example, implementation are HashMap and
TreeMap.
TreeMap implements SortedMap.
The difference between Set and Map interface is that in Set we have only keys, whereas in Map, we have key,
value pairs.

Advanced Programming in Java Lecture notes by: Jerusalem F. 25

You might also like