Misc
Misc
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.
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
System.out.println(s1.ShowName("Harish"));
Lambda Expression
Output:
Hello, Harish
Hello, Manish
Lambda Expression
Java Lambda Expression Example: Multiple Parameters:
interface Addition{
int add(int a,int b);
}
System.out.println(ad1.add(10,20));
Output:
30
300
Lambda Expression
interface Addable{
int add(int a,int b);
}
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:
Output:
Pattern found from 0 to 2
Pattern found from 7 to 9
Pattern found from 12 to 12