Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Date before() method in Java with Examples
Next article icon

Pattern asPredicate() Method in Java with Examples

Last Updated : 12 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

asPredicate() method of a Pattern class used to creates a predicate object which can be used to match a string.Predicate is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. 

Syntax:

public Predicate asPredicate()

Parameters: This method accepts nothing as parameter. 

Return value: This method returns a predicate which can be used for matching on a string. 

Below programs illustrate the asPredicate() method: 

Program 1: 

Java




// Java program to demonstrate
// Pattern.asPredicate() method
 
import java.util.regex.*;
import java.util.function.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "ee";
 
        // create the string
        // in which you want to search
        String actualString
            = "aaeebbeecceeddee";
 
        // create a Pattern using REGEX
        Pattern pattern
            = Pattern.compile(REGEX);
 
        // get Predicate Object
        Predicate<String> predicate
            = pattern.asPredicate();
 
        // check whether predicate match
        // with actualString
        boolean value = predicate
                            .test(actualString);
 
        // print result
        System.out.println("value matched: "
                           + value);
    }
}
 
 
Output:
value matched: true

Program 2: 

Java




// Java program to demonstrate
// Pattern.asPredicate() method
 
import java.util.regex.*;
import java.util.function.*;
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "org";
 
        // create the string
        // in which you want to search
        String actualString
            = "welcome geeks";
 
        // create a Pattern using REGEX
        Pattern pattern
            = Pattern.compile(REGEX);
 
        // get Predicate Object
        Predicate<String> predicate
            = pattern.asPredicate();
 
        // check whether predicate match
        // with actualString
        boolean value
            = predicate.test(actualString);
 
        // print result
        System.out.println("value matched: "
                           + value);
    }
}
 
 
Output:
value matched: false

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#asPredicate()



Next Article
Date before() method in Java with Examples

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java - util package
  • Java-Functions
  • Java-Pattern
  • java-regular-expression
Practice Tags :
  • Java

Similar Reads

  • Pattern split(CharSequence) method in Java with Examples
    split(CharSequence) method of a Pattern class used to splits the given char sequence passed as parameter to method around matches of this pattern.This method can split charSequence into an array of String's, using the regular expression used to compile the pattern as a delimiter.so we can say that t
    2 min read
  • Pattern compile(String) method in Java with Examples
    The compile(String) method of the Pattern class in Java is used to create a pattern from the regular expression passed as parameter to method. Whenever you need to match a text against a regular expression pattern more than one time, create a Pattern instance using the Pattern.compile() method.Synta
    2 min read
  • Pattern flags() method in Java with Examples
    The flags() method of the Pattern class in Java is used to return the pattern's match flags. The Match flags are a bit mask that may include CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE, CANON_EQ, UNIX_LINES, LITERAL, UNICODE_CHARACTER_CLASS and COMMENTS Flags. Syntax: public int flags() Parame
    2 min read
  • Pattern matcher(CharSequence) method in Java with Examples
    The matcher(CharSequence) method of the Pattern class used to generate a matcher that will helpful to match the given input as parameter to method against this pattern. The Pattern.matcher() method is very helpful when we need to check a pattern against a text a single time, and the default settings
    2 min read
  • Date before() method in Java with Examples
    The before() method of Java Date class tests whether the date is before the specified date or not. Syntax: public boolean before(Date when) Parameters: The function accepts a single parameter when which specifies the date that has to be checked. Return Value: It returns a Boolean value. It will retu
    2 min read
  • Pattern split(CharSequence,int) method in Java with Examples
    split(CharSequence, int) method of a Pattern class used to splits the given char sequence passed as parameter to method around matches of this pattern.The array returned contains each substring of the input sequence created by this method. The substrings in the array are in the order in which they o
    3 min read
  • Scanner delimiter() method in Java with Examples
    The delimiter() method ofjava.util.Scanner class returns the Pattern this Scanner is currently using to match delimiters. Syntax: public Pattern delimiter() Return Value: The function returns the scanner's delimiting pattern. Below programs illustrate the above function: Program 1: // Java program t
    2 min read
  • Pattern compile(String,int) method in Java with Examples
    The compile(String, int) method of the Pattern class used to create a pattern from the regular expression with the help of flags where both expression and flags are passed as parameters to the method. The Pattern class contains a list of flags (int constants) that can be helpful to make the Pattern
    2 min read
  • Period equals() method in Java with Examples
    The equals() method of Period class in Java is used to check if two given periods are equal or not. The comparison is based on the type Period and each of the three years, months and date. To be equal, all of the three year, month and date must be individually equal. Syntax: public boolean equals(Pe
    2 min read
  • Pattern toString() Method in Java with Examples
    toString() method of a Pattern class used to return the string representation of this pattern. This return the regular expression from which this pattern was compiled. Syntax: public String toString() Parameters: This method accepts nothing as parameter. Return value: This method returns a string re
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences