Section 1
Lesson 303.11
Generic and Java Collection Classes Lists, Maps, and Sets
Learning Objectives:
By the end of lesson, learners will be able to:
Describe Java Generics.
Demonstrate how to create the Generic method, class, and interface.
Section 1
Java Generics
Table of Contents
Topic 1: Overview of “Generics<T>.”
Topic 2: How to Use Generic.
Topic 2a: Generic Method.
Topic 2b: Generic Type Class.
Topic 2c: Generic Interface.
Topic 3: Bounded Types Parameter in Generic.
Topic 4: Generic Wildcard.
Topic 1: Overview of “Generics<T>”
The Java Generics allow us to create a single class, interface, and method that can be used with different types of data (objects). It allows programmers to create a method or a set of methods that will accept any data type.
Data types are to be defined as a generic type in design time, and the actual data type is determined at runtime.
In case of a bounded types, we can restrict the data type by using type parameter <T>. Note: Generics do not work with primitive types (int, float, char, etc).
In layman’s terms, Generics force type safety in Java language.
Topic 2: How to Use Generic
These are three common ways to use Generic:
Generic Method.
Generic Type Class.
Generic Type Interface.
Topic 2a: Generic Method
It is possible to create a generic method in generic class and in regular class. It is also possible to create a static generic method.
Syntax:
“datatype” is only a name. You are free to use “T,” “X,” “Z,” or other identifiers, but there are advisable naming conventions.
Example: Generic Method
In the above class, we have three generic methods: one generic static method and two generic regular methods. We are going to invoke them in myRunner class.
In the above class, the compiler can match the type parameter based on the value passed to the method.
Topic 2b: Generic Type Class
A class is generic if it declares one or more type variables. These type variables are known as the type parameters <> of the class.
A generic class is implemented exactly like a regular class. The only difference is that it contains a type parameter <> section.
There can be more than one type of parameter, separated by a comma. The classes, which accept one or more parameters, are known as parameterized classes or parameterized types.
“T” used inside the angle bracket <> indicates the type parameter.
To create objects of a generic class, we use the following syntax:
“T” is only a name. You are free to use “X” or “Z,” or any other identifier, but these are preferred and advisable naming conventions.
Example: Generic Type Class
Step 1: Create a Generics Class named GenericsClass
Step 2: Create a main class named MyRunner
Here, T used inside the angle bracket <> indicates the type parameter.
The getData() method returns exactly the type being held.
Notice that within our “GenericsClass” class definition, we use T as if it were a fully-defined type. We do not know exactly what type T is, but we do know that it is a reference type.
We also do not know exactly what type the field T is, but since T must be a reference type, we do know that the field T is an Object.
“T” is only a name. You are free to use “X” or “Z” or any other identifiers, but there are advisable naming conventions.
Topic 2c: Generic Interface
Generics also work with Interfaces.
An Interface is generic if it declares one or more type variables. These type variables are known as the type parameters <> of the Interface.
Syntax: Declare Generic Interface is as follows:
Syntax: Implementing a Java generic Interface:
For more information about Generic Interfaces, visit the Wiki document.
Example: Generic Interface
Step 1: Create a generic interface named Moveable with type parameter. The interface has a name and a type parameter T. We will declare three abstract methods in interface.
Step 2: Create a generic class named ElephantItem with type parameter. The class definition uses the same type parameters twice – one after the class name and another after the interface name it implements.
Step 3: Let create a main class named MyRunner:
Output:
Topic 3: Bounded Types Parameter in Generic
Bounded Types Parameter allow us to limit the data type that can be passed through type parameter by using the extends keyword. For example, for any particular generic class, method, or interface that should work only for numeric data types, you can specify using the “extends” keyword.
Syntax:
<T extends A>
Example:
The
<T extends Number>
means that the type parameter of GenericsClass must be a type, which is a subclass of the Number class.We can say that the type parameter is bounded.
Bounded Types Parameter in Generic (continued)
A generic type may be declared with multiple type parameters. In addition to simple type parameter names, type parameter declarations can also constrain the set of types allowed by using the extends keyword. Some examples follow:
Example - Bounded Types Parameter in Generic
Sometimes, we do not want to parameterize out classes or methods with just any type, but we want to put some restrictions on them. For instance, suppose we want a box that holds particular kinds of numbers such as a MathBox. What we want to say is that E must be a subtype of Number. Step 1: Create a generic class named MathBox. The class definition uses the type parameters with the keyword, extends, as shown below:
Step 2: Let’s create a main class named MyRunner as shown below:
The following is illegal because String is not a subclass of Number. You will get an Error.
Generic Type Parameter Names
The Generic Type Parameter naming convention helps us understand code easily. Having a naming convention is one of the best practices of Java programming language.
Unlike variable names, type parameters should be short and capitalized so that they will not be confused with variable or actual type names.
Here are some well-advised naming conventions:
E
K
N
V
T
Element (as in an element of a Collection). Key(asinakeyinaMap).
Number.
Value (as in Key/Value pair).
Type (everything else).
Note: These are only conventions – the compiler does not enforce them.
As a rule of thumb, you should use:
? extends T
if you only need "read" access ("input").? super T
if you need "write" access ("output").T
if you need both ("modify").
Topic 4: Generic Wildcard
The Wildcard argument is specified by the question mark (?), and it represents an “unknown type.” The extends and super keywords may be used in conjunction with the wildcard to provide upper and lower bounds on the types argument that may satisfy the type constraints.
A wildcard is important when creating a generic type that will operate on a class hierarchy.
Syntax: In general, to establish an upper bound for a wildcard, use the following type of Wildcard expression:
<? extends T>
// represents an upper bounded wildcard.Superclass is the name of the class that serves as the upper bound.
This is an inclusive clause.
You can specify a lower bound for a wildcard by adding a super clause to a wildcard declaration.
<? super T>
// represents a lower bounded wildcard.
Advantages of Java Generic
Code Reusability.
Compile-time Type Checking.
Used with Java Collections.
Hands-On Lab
Complete the GLAB - 303.11.1 - Generic Method and Class. You can find this lab under Assignment section.
Knowledge Check
What is a Generic Type Parameter?
How Does a Generic Method differ from a Generic Type?
What is a Bounded Types Parameter in Generic?
What is Generic Wildcard?
Summary
Java Generic is a language feature that allows for definition and use of generic types and methods. Java Generic is a powerful addition to the Java language because it makes the programmer's job easier and less prone to error. Generic enforces type correctness at compile time, and most importantly, enables implementing.
Learning Objectives:
By the end of lesson, learners will be able to:
Describe the Java Collections Interface.
Demonstrate how to give implementation of Collection<E> using classes.
Describe how to use the Java Collections Framework Generic Classes.
Describe the List Interface and implementation of list.
Understand the ArrayList<E> Class and Its methods
Demonstrate Iterating through ArrayList
Utilize the LinkedList<E> Class
Declare and Initialize Linkedlist<E>
Last updated