
- Groovy Tutorial
- Groovy - Home
- Groovy - Overview
- Groovy - Environment
- Groovy - Basic Syntax
- Groovy - Data Types
- Groovy - Variables
- Groovy - Optionals
- Groovy - Numbers
- Groovy - Strings
- Groovy - Ranges
- Groovy - Lists
- Groovy - Maps
- Groovy - Dates & Times
Groovy Operators
- Groovy - Operators
- Groovy - Arithmetic Operators
- Groovy - Assignment Operators
- Groovy - Relational Operators
- Groovy - Logical Operators
- Groovy - Bitwise Operators
- Groovy - Spaceship Operator
- Groovy - in Operator
- Groovy - Elvis Operator
- Groovy - Safe Navigation Operator
- Groovy Operator Precedence & Associativity
Control Statements
- Groovy - Decision Making
- Groovy - If Else Statement
- Groovy - Switch Statement
- Groovy - Loops
- Groovy - For Loop
- Groovy - For-in Loop
- Groovy - While Loop
- Groovy - Do While Loop
- Groovy - Break Statement
- Groovy - Continue Statement
Groovy File Handling
- Groovy - File I/O
- Java - Create a File
- Java - Write to File
- Java - Append to File
- Java - Read Files
- Java - Delete Files
- Java - File Properties
- Java - File Existence and Type
- Java - File Size
- Java - File Permissions
- Java - Directories
- Java - Listing Directories
- Java - Filtering Files/Directories
- Java - Deleting Directories
- Java - Renaming Files/Directories
Groovy Error & Exceptions
- Groovy - Exception Handling
- Groovy - try-catch Block
- Groovy - try-with-resources
- Groovy - Multi-catch Block
- Groovy - Nested try Block
- Groovy - Finally Block
- Groovy - throw Exception
- Groovy - Exception Propagation
- Groovy - Built-in Exceptions
- Groovy - Custom Exception
Groovy Multithreading
- groovy - Multithreading
- groovy - Thread Life Cycle
- groovy - Creating a Thread
- groovy - Starting a Thread
- groovy - Joining Threads
- groovy - Naming Thread
- groovy - Thread Scheduler
- groovy - Thread Pools
- groovy - Main Thread
- groovy - Thread Priority
- groovy - Daemon Threads
- groovy - Shutdown Hook
Groovy Synchronization
- groovy - Synchronization
- groovy - Block Synchronization
- groovy - Static Synchronization
- groovy - Inter-thread Communication
- groovy - Thread Deadlock
- groovy - Interrupting a Thread
- groovy - Thread Control
- groovy - Reentrant Monitor
- Groovy - Methods
- Groovy - Methods
- Groovy - Optional parenthesis
- Groovy - Named Arguments
- Groovy - Closures as Arguments
- Groovy - Method Overloading
- Groovy - Method Scope and Visibility
- Groovy - isCase Method
- Groovy - Implicit Return
- Groovy - Variable Arguments
- Groovy - Regular Expressions
- Groovy - Regular Expressions
- Groovy - Defining Regular Expressions
- Groovy - Matcher Object
- Groovy - Regex Tasks
- Groovy - XML
- Groovy - XML
- Groovy - Parsing XML
- Groovy - Creating XML
- Groovy - Modifying XML
- Groovy - Querying XML
- Groovy - Simplified Notation
- Groovy - Closure based Querying
- Groovy - Closure based Creation
- Groovy - JSON
- Groovy - JSON
- Groovy - Parsing JSON
- Groovy - Creating JSON using JsonOutput
- Groovy - Creating JSON using JsonBuilder
- Groovy - Modifying JSON
- Groovy - Error Handling
- Groovy - Handling JSON Arrays
- Groovy - JSON Array Operations
- Groovy - JSON Objects
- Groovy - JSON Object Operations
- Groovy - Generics
- Groovy - Generics
- Groovy - Declaring Generic Types
- Groovy - Bound Type Parameters
- Groovy - Wild Cards
- Groovy - Miscellaneous
- Groovy - Object Oriented
- Groovy - Closures
- Groovy - Annotations
- Groovy - JMX
- Groovy - DSLS
- Groovy - Database
- Groovy - Builders
- Groovy - Command Line
- Groovy - Unit Testing
- Groovy - Template Engines
- Groovy - Meta Object Programming
- Groovy Useful Resources
- Groovy - Quick Guide
- Groovy - Useful Resources
- Groovy - Discussion
Groovy - in operator
in operator provides an elegant and concise way to check membership or containment of an item in a Groovy data structures or ranges.
Checking Membership in Groovy Collections
We can check if a value is present in a collection like List or Set easily using in operator.
Example.groovy
def numbers = [1, 2, 3, 4, 5] def numberToSearch = 3 if (numberToSearch in numbers) { println "$numberToSearch is present in the list" }else { println "$numberToSearch is not present in the list" } def letters = ['a', 'b', 'c'] if ('f' in letters) { println "f is in the list" } else { println "f is not in the list" } def mySet = [10, 20, 30] as Set if (20 in mySet) { println "20 is present in the set" } else { println "20 is not present in the set" }
Output
When we run the above program, we will get the following result.
3 is present in the list f is not in the list 20 is present in the set
Checking Value in Groovy Ranges
We can check if a value lies between given range using in operator as shown below. in operator checks the value on left hand side in the given range on right hand size.
Example.groovy
def myRange = 1..10 def value = 7 if (value in myRange) { println "$value is within the range 1 to 10." }else{ println "$value is out of range." } if (15 in 1..5) { println "15 is in the range 1 to 5." } else { println "15 is out of rangd." } def charRange = 'a'..'f' if ('c' in charRange) { println "c is in the character range a to f" }else{ println "c is out of range" }
Output
When we run the above program, we will get the following result.
7 is within the range 1 to 10. 15 is out of rangd. c is in the character range a to f
Checking a key in a Map
We can check if a key is present in the Map or not.
Example.groovy
def map = ['name': 'Julie', 'age': 30, 'city': 'Delhi'] if ('age' in map) { println "The map contains a key 'age'" } else { println "The map is not having key 'age'" } if ('country' in map) { println "The map contains a key 'country'" } else { println "The map is not having key 'country'" }
Output
When we run the above program, we will get the following result.
7 is within the range 1 to 10. 15 is out of rangd. c is in the character range a to f
Advertisements