Tuesday 5 October 2021

Exercise Collections in Groovy Scripting groovy training telugu 22

Watch This Video 


[Exercise] Using Collections


If you are new to Java or Groovy the idea of an Enum might be new. An Enum is a collection of constant values. We can use this collection of constants to create ranges. I want you to do some reading up on enum's and create an enum for days of the week. ex Sunday, Monday, etc...

Create a range from that enum
Print the size of the Range
Use the contains method to see if Wednesday is in that Range
Print the from element of this range
Print the to element of this range


Create a list days (Sun - Sat)
Print out the list
Print the size of the list
Remove Saturday from the list
Add Saturday back by appending it to the list
Print out the Wednesday using its index


Create a map of days of the week
1: Sunday, 2:Monday, etc...
Print out the map
Print out the class name of the map
Print the size of the map
Is there a method that would easily print out all of the days (values)? 
Without closures you may have to look at the Java API for LinkedHashMap


enum Days{
def dayRange=Days.SUNDAY..days.SATURDAY
//for in loop
for(day in dayRange){
println day

// using Closures

dayRange.each{day ->
println day

println dayRange.size()
println dayRange.contains(Days.WEDNESDAY)
println dayRange.from


println days
println days.size()
println days
println days
println days[2]


println days

println days.getClass().getName()
println days.size()
println days.values()

No comments:

Post a Comment

Note: only a member of this blog may post a comment.