Generics
Generics means parameterized types. The idea is to allow type
(Integer, String, … etc., and user-defined types) to be a parameter to
methods, classes, and interfaces. Using Generics, it is possible to
create classes that work with different data types. An entity such as
class, interface, or method that operates on a parameterized type is a
generic entity.
They were designed to extend Java's type system to allow "a type or
method to operate on objects of various types while providing
compile-time type safety"
NOTE:
You cannot create an instance of generic type because byte code has
no idea about it.
Upper bounded wildcards
CompareTo() method:
The compareTo() method (from the Comparable interface) is used to
compare objects based on their internal data in a meaningful way,
particularly for sorting or ordering purposes.
The compareTo() method is used when you need to compare objects
based on multiple criteria or need to sort them.
When a class implements Comparable, it must override the
compareTo() method to define how instances of that class should be
compared.
NOTE:
If result it 0 means both objects are equal.
If result is < 0 means other obj is bigger or else it is smaller.
Lambda functions:
lambda expressions (or lambda functions) were introduced in Java 8
as a way to provide a clear and concise syntax for writing anonymous
functions (i.e., functions that don’t have a name). They are primarily
used to implement functional interfaces—interfaces that have a
single abstract method (SAM). Lambda expressions help make the
code more readable and concise, especially when working with
collections, streams, and event handling.
Instead of implementing the interface in a separate class or
anonymous class, you can use a lambda expression to provide the
implementation of the single abstract method.
Syntax: (parameters) -> expression
NOTE:
No Type Declaration: You don’t need to explicitly declare the
types of the parameters in the lambda expression. Java can
infer them from the context.
Optional Parentheses: If there’s only one parameter, you can
omit the parentheses. For example: n -> n > 0.
Optional Curly Braces: If the body of the lambda expression
contains a single statement, you can omit the curly braces. For
example: x -> x * 2.
Return Keyword: If you use a single expression, you don’t need
to use the return keyword. It is implicitly returned. However, if
you have multiple statements, you need to use curly braces and
an explicit return.
Consumer:
In Java, Consumer is a functional interface in the [Link]
package. It represents an operation that accepts a single input
argument and returns no result. The Consumer interface is often
used in functional programming when you need to perform some
action on an object, like printing, modifying, or processing elements
in a collection.