Translate

Tuesday 5 October 2021

Exercise Collections in Groovy Scripting groovy training telugu 22

Watch This Video

https://youtu.be/gp3HA0Wi9ZM 

----------------------------------------------------------------------------------------------------------------------------

[Exercise] Using Collections

1)Ranges

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

2)Lists

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

3)Maps

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


//Range

enum Days{
SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY
}
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 dayRange.to


//List

def days = ["SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"]
println days
println days.size()
days.pop()
println days
days<<"Sunday"
println days
println days[2]



//Map

def days = [1:"SUNDAY",2:"MONDAY",3:"TUESDAY",4:"WEDNESDAY",5:"THURSDAY",6:"FRIDAY",7:"SATURDAY"]
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.