Speaking of the "in" keyword is rarely used in the Java language because it is introduced later, i.e. the JDK5 enhanced for loop. The groovy language also retains this functionality so that we can write code similar to the following:
def list = [1,2,3]
for(i in list)
{
println i
}
The results of the operation are:
1
2
3
This enhanced for loop can also be used on loops on the map, such as:
def map = [a:"1",b:"2"]
for(i in map)
{
println "${i.key} : ${i.value}"
}
The results of the operation are:
a : 1
b : 2
In addition, the "in" keyword is also introduced into the groovy language to determine whether an object is an element of a collection class. Such as:
def list = [1,2,3]
println 1 in list
println 5 in list
The results of the operation are:
true
false
Others, such as set objects, can also be used in this way:
def set = [1,2,3,4,3,4] as Set
println 1 in set
println 5 in set
The results of the operation are:
true
false