Unit-4 Generics and Collections (E-Next - In)
Unit-4 Generics and Collections (E-Next - In)
Q.1 What are Lambda Expressions? Explain syntax and use of Lambda expressions with a
suitable program.
Lambdas Expression:
Lambda expression is a new and important feature of Java which was included in
Java SE 8.
It provides a clear and concise way to represent one method interface using an
expression.
It is very useful in collection library. It helps to iterate, filter and extract data from
collection.
The Lambda expression is used to provide the implementation of an interface which
has functional interface. It saves a lot of code.
In case of lambda expression, we don't need to define the method again for
providing the implementation. Here, we just write the implementation code.
Java lambda expression is treated as a function, so compiler does not create .class
file.
Functional Interface:
Lambda expression provides implementation of functional interface. An interface
which has only one abstract method is called functional interface.
Java provides an anotation @FunctionalInterface, which is used to declare an
interface as functional interface.
{
System.out.println("Drawing "+width);
};
d2.draw();
}
}
Output:
Drawing 10
interface Drawable
{
public void draw();
}
class Test
{
public static void main (String[] args)
{
Gen < Integer> iob = new Gen<>(100); //instance of Integer type Gen Class.
int x = iob.getOb();
System.out.println(x);
Gen < String> sob = new Gen<>("Hello"); //instance of String type Gen Class.
String str = sob.getOb();
System.out.println(str);
}
}
Output:
100
Hello
In the above program, we first passed an Integer type parameter to the Generic class. Then,
we passed a String type parameter to the same Generic class. Hence, we reused the same
class for two different data types. Thus, Generics helps in code reusability with ease.
Generic Methods
You can also create generic methods that can be called with different types of
arguments. Based on the type of arguments passed to generic method, the compiler
handles each method.
The syntax for a generic method includes a type-parameter inside angle brackets,
and should appear before the method's return type.
Output:
java.lang.String = This is string
java.lang.Double = 99.O
Collection Framework:
Collections framework was not a part of original Java release.
Collections was added to J2SE 1.2. Prior to Java 2, Java provided adhoc classes such
as Dictionary, Vector, Stack and Properties to store and manipulate groups of
objects.
Framework in java means hierarchy of classes and interfaces.
Collections framework is contained in java.util package.
It provides many important classes and interfaces to collect and organize group of
alike objects.
Interface Description
Collection Enables you to work with groups of object; it is at the top of Collection hierarchy
Queue Extends Collection to handle special kind of list in which element are removed
only from the head.
Set Extends Collection to handle sets, which must contain unique element.