0% found this document useful (0 votes)
64 views24 pages

Misc

Lambda expressions are anonymous functions that can be passed around and used without being bound to an identifier. They allow for more concise implementation of interfaces through expression syntax rather than full classes or methods. Regular expressions use special pattern matching characters to describe text sequences to search for. The java.util.regex package provides classes like Pattern and Matcher to define and search for regex patterns in strings.

Uploaded by

rudrakshi ghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views24 pages

Misc

Lambda expressions are anonymous functions that can be passed around and used without being bound to an identifier. They allow for more concise implementation of interfaces through expression syntax rather than full classes or methods. Regular expressions use special pattern matching characters to describe text sequences to search for. The java.util.regex package provides classes like Pattern and Matcher to define and search for regex patterns in strings.

Uploaded by

rudrakshi ghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Lambda Expression

A lambda expression is a short block of code which takes in


parameters and returns a value.

Lambda expressions are similar to methods, but they do not need a


name and they can be implemented right in the body of a method.

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.
Lambda Expression

Advantages:

It saves a lot of code. We don't need to define the method again for
providing the implementation. Here, we just write the implementation
code.

Purpose of using lambda expression:


To provide the implementation of Functional interface.
Less coding.
Lambda Expression

Lambda expressions can be implemented right in the body of a


method.

The lambda expression contains parameter and expression:


parameter -> expression;
(parameter1, parameter2) -> expression;
Lambda Expression
A program without Lambda Expression:

interface Drawable{
public void draw();
}
public class withoutLambdaDemo {
public static void main(String[] args) {
int width=10;
//without lambda, Drawable implementation using anonymous class
Drawable d=new Drawable(){
public void draw(){System.out.println("Drawing "+width);}
};
d.draw();
}
}
Lambda Expression
A program with Lambda Expression: Without Parameter

interface Drawable{
public void draw();
}
public class LambdaDemo {
public static void main(String[] args) {
int width=10;
//with lambda
Drawable d2=()->{ System.out.println("Drawing "+width); };
d2.draw();
}
}
Lambda Expression

Java Lambda Expression Example: Single Parameter


interface Info{
public String ShowName(String name);
}

public class LambdaExpression1{


public static void main(String[] args) {
// Lambda expression with single parameter.
Info s1=(name)->{ return "Hello, "+name; };

System.out.println(s1.ShowName("Harish"));
Lambda Expression

// You can omit function parentheses


Info s2= name ->{ return "Hello, "+name; };
System.out.println(s2.ShowName("Manish"));
}
}

Output:
Hello, Harish
Hello, Manish
Lambda Expression
Java Lambda Expression Example: Multiple Parameters:

interface Addition{
int add(int a,int b);
}

public class LambdaExpressionMultiple{


public static void main(String[] args) {

// Multiple parameters in lambda expression


Addition ad1=(a,b)->(a+b);
Lambda Expression

System.out.println(ad1.add(10,20));

// Multiple parameters with data type in lambda expression


Addition ad2=(int a,int b)->(a+b);
System.out.println(ad2.add(100,200));
}
}

Output:
30
300
Lambda Expression

Java Lambda Expression Example: with or without return


keyword:
You must use return keyword when lambda expression contains
multiple statements.

interface Addable{
int add(int a,int b);
}

public class LambdaExpressionReturn {


public static void main(String[] args) {
Lambda Expression

// Lambda expression without return keyword.


Addable ad1=(a,b)->(a+b);
System.out.println(ad1.add(10,20));

// Lambda expression with return keyword.


Addable ad2=(int a,int b)->{ return (a+b); };
int x = ad2.add(100,200));
}
}
Lambda Expression
Java Lambda Expression Example: Foreach Loop:
ArrayList is a part of
import java.util.*; collection framework and is
public class LambdaExpressionLoop{ present in java.util package.
public static void main(String[] args) { It uses a dynamic array for
storing the elements. It is
ArrayList<String> list= like an array, but there is no
new ArrayList<String>(); size limit. We can add or
list.add("ankit"); remove elements anytime.
So, it is much more flexible
list.add("mayank"); than the traditional array. It
list.add("irfan"); is found in the java.util
list.add("jai"); package. It is like the
Vector in C++.
Lambda Expression

list.forEach( (n)->System.out.println(n) );
}
}

Output:

ankit
mayank
irfan
jai
Regular Expression

A regular expression is a sequence of characters that forms a search pattern. When you
search for data in a text, you can use this search pattern to describe what you are
searching for.


A regular expression can be a single character, or a more complicated pattern.


Regular expressions can be used to perform all types of text search and text replace
operations.


Java does not have a built-in Regular Expression class, but we can import the
java.util.regex package to work with regular expressions.


The Java Regex or Regular Expression is an API to define a pattern for searching or
manipulating strings. It provides 1 interface and 3 classes
Regular Expression


java.util.regex package


The java.util.regex package provides following classes and interfaces for regular
expressions:

MatchResult interface

Matcher class - Used to search for the pattern

Pattern class - Defines a pattern (to be used in a search)

PatternSyntaxException class - Indicates syntax error in a regular expression pattern
Regular Expression

Matcher class:

It implements the MatchResult interface. It is a regex engine which is used to perform
match operations on a character sequence.


Regular Expression

Pattern class:

It is the compiled version of a regular expression. It is used to define a pattern for the regex
engine.


Regular Expression


// A Simple Java program to demonstrate working of String matching in Java


import java.util.regex.Matcher;

import java.util.regex.Pattern;


class Demo

{

public static void main(String args[])

{

// Create a pattern to be searched

Pattern p = Pattern.compile(“University", CASE_INSENSITIVE);


// Search above pattern in "Amity University and Calcutta University"

Matcher m = p.matcher("Amity University and Calcutta University");

Regular Expression


// Print starting and ending indexes of the pattern in text


while (m.find())

System.out.println("Pattern found from " + m.start() +

" to " + (m.end()-1));

}

}


Output:

Pattern found from 6 to 15

Pattern found from 30 to 39
Regular Expression


Flags:


Flags in the compile() method change how the search is performed. Here are a few of
them:


Pattern.CASE_INSENSITIVE - The case of letters will be ignored when performing a
search.


Pattern.LITERAL - Special characters in the pattern will not have any special meaning and
will be treated as ordinary characters when performing a search.


Pattern.UNICODE_CASE - Use it together with the CASE_INSENSITIVE flag to also
ignore the case of letters outside of the English alphabet
Regular Expression


Regular Expression Patterns:


The first parameter of the Pattern.compile() method is the pattern. It describes what is
being searched for.


Brackets are used to find a range of characters:


[abc] Find one character from the options between the brackets

[^abc] Find one character NOT between the brackets

[0-9] Find one character from the range 0 to 9
Regular Expression
Metacharacters:

Metacharacters are characters with a special meaning:


Regular Expression
Quantifiers:
Quantifiers define quantities
Regular Expression

// Create a pattern to be searched


Pattern p = Pattern.compile("ac*");

// Search above pattern in "accdjkyaccrta"


Matcher m = pattern.matcher("accdjkyaccrta");

Output:
Pattern found from 0 to 2
Pattern found from 7 to 9
Pattern found from 12 to 12

You might also like