List & Set Functions
List functions
range(numeric start, numeric end, (Optional) numeric step)
- Creates a list of numbers from start to end (both inclusive) with the given step
- If the step is not provided
- Default value of 1 is used if start <= end
- Else default value of -1 is used
def List<int> range1 = range(1, 5); // [1,2,3,4,5]
def List<real> range2 = range(2.0, 5.7); // [2.0, 3.0, 4.0, 5.0]
def List<int> range3 = range(6, 2); // [6,5,4,3,2]
def List<real> range4 = range(5.3, 1.2); // [5.3, 4.3, 3.3, 2.3, 1.3]
def List<int> range5 = range(1, 12, 3); // [1, 4, 7, 10]
If you are providing step, make sure that step is positive if end >= start and vice-versa
clearList(List listValue)
- Empties the given list
def List<int> list = [1,3,4,6];
list.clearList(); // []
clearList(list); // []
def List<string> list = ["apple", "banana", "cherry"];
list.clearList(); // []
clearList(list); // []
reversed(List listValue)
- Returns the reverse of the list
def List<int> list = [1,3,4,6];
def List<int> reversedList = list.reversed(); // [6,4,3,1]
def List<int> reversedList2 = reversed(list); // [6,4,3,1]
def List<string> list = ["apple", "banana", "cherry"];
def List<string> reversedList = list.reversed(); // ["cherry", "banana", "apple"]
def List<string> reversedList2 = reversed(list); // ["cherry", "banana", "apple"]
shuffled(List listValue)
- Returns a random permutation of the list
def List<int> list = [1,3,4,6];
def List<int> shuffledList = list.shuffled(); // [4,1,6,3]
def List<int> shuffledList2 = shuffled(list); // [4,1,6,3]
def List<string> list = ["apple", "banana", "cherry"];
def List<string> shuffledList = list.shuffled(); // ["cherry", "apple", "banana"]
def List<string> shuffledList2 = shuffled(list); // ["cherry", "apple", "banana"]
take(List listValue, int n)
- Returns a list with maximum n items taken from the start
def List<int> list1 = [1,3,4,6,2];
def List<int> list2 = list1.take(3); // [1,3,4]
def List<int> list3 = list1.take(6); // [1,3,4,6,2]
def List<int> list4 = take(list1, 3); // [1,3,4]
def List<int> list5 = take(list1, 6); // [1,3,4,6,2]
def List<string> list1 = ["apple", "banana", "cherry"];
def List<string> list2 = list1.take(2); // ["apple", "banana"]
def List<string> list3 = list1.take(4); // ["apple", "banana", "cherry"]
def List<string> list4 = take(list1, 2); // ["apple", "banana"]
def List<string> list5 = take(list1, 4); // ["apple", "banana", "cherry"]
takeLast(List listValue, int number)
- Returns a list with maximum n items taken from the end
def List<int> list1 = [1,3,4,6,2];
def List<int> list2 = list1.takeLast(3); // [4,6,2]
def List<int> list3 = list1.takeLast(6); // [1,3,4,6,2]
def List<int> list4 = takeLast(list1, 3); // [4,6,2]
def List<int> list5 = takeLast(list1, 6); // [1,3,4,6,2]
def List<string> list1 = ["apple", "banana", "cherry"];
def List<string> list2 = list1.takeLast(2); // ["banana", "cherry"]
def List<string> list3 = list1.takeLast(4); // ["apple", "banana", "cherry"]
def List<string> list4 = takeLast(list1, 2); // ["banana", "cherry"]
def List<string> list5 = takeLast(list1, 4); // ["apple", "banana", "cherry"]
drop(List listValue)
- Returns a list with max n items removed from the start
def List<int> list1 = [1,3,4,6,2];
def List<int> list2 = list1.drop(3); // [6,2]
def List<int> list3 = list1.drop(6); // []
def List<int> list4 = drop(list1, 3); // [6,2]
def List<int> list5 = drop(list1, 6); // []
def List<string> list1 = ["apple", "banana", "cherry"];
def List<string> list2 = list1.drop(2); // ["cherry"]
def List<string> list3 = list1.drop(4); // []
def List<string> list4 = drop(list1, 2); // ["cherry"]
def List<string> list5 = drop(list1, 4); // []
dropLast(List listValue)
- Returns a list with max n items removed from the end
def List<int> list1 = [1,3,4,6,2];
def List<int> list2 = list1.dropLast(3); // [1,3]
def List<int> list3 = list1.dropLast(6); // []
def List<int> list4 = dropLast(list1, 3); // [1,3]
def List<int> list5 = dropLast(list1, 6); // []
def List<string> list1 = ["apple", "banana", "cherry"];
def List<string> list2 = list1.dropLast(2); // ["apple"]
def List<string> list3 = list1.dropLast(4); // []
def List<string> list4 = dropLast(list1, 2); // ["apple"]
def List<string> list5 = dropLast(list1, 4); // []
slice(List listValue, List<int> positions)
- Returns a list from listValue with items at positions
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def List<int> positions = [3,1,4];
def List<string> listAtPositions = list.slice(positions); // ["grape", "apple", "melon"]
def List<string> listAtPositions2 = slice(list, positions); // ["grape", "apple", "melon"]
def List<real> list = [12.344,4554.224,-12,-782.24];
def List<int> positions = [2,1,4];
def List<real> listAtPositions = list.slice(positions); // [4554.224, 12.344, -782.24]
def List<real> listAtPositions2 = slice(list, positions); // [4554.224, 12.344, -782.24]
Make sure that the positions given are in range otherwise you'll get an error
subList(List listValue, int startPosition, int endPosition)
- Returns a sublist from listValue with items at positions from startPosition to endPosition (both inclusive)
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def List<string> subList = list.subList(2, 4); // ["banana", "grape", "melon"]
def List<string> subList2 = subList(list, 2, 4); // ["banana", "grape", "melon"]
def List<real> list = [12.344,4554.224,-12,-782.24];
def List<real> subList = list.subList(2, 4); // [4554.224, -12, -782.24]
def List<real> subList2 = subList(list, 2, 4); // [4554.224, -12, -782.24]
Make sure that startPosition and endPosition are valid positions and endPosition >= startPosition otherwise you'll get an error
chunked(List listValue, int size)
- Partition the list into chunks of length = size provided. Length of last subList will be less than size if list size is not divisible by size provided
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def List<List<string>> chunkedList1 = list.chunked(2); // [["apple", "banana"], ["grape", "melon"], ["cherry"]]
def List<List<string>> chunkedList2 = chunked(list, 2); // [["apple", "banana"], ["grape", "melon"], ["cherry"]]
def List<List<string>> chunkedList3 = list.chunked(3); // [["apple", "banana", "grape"], ["melon", "cherry"]]
def List<List<string>> chunkedList4 = chunked(list, 3); // [["apple", "banana", "grape"], ["melon", "cherry"]]
def List<List<string>> chunkedList5 = list.chunked(6); // [["apple", "banana", "grape", "melon", "cherry"]]
def List<List<string>> chunkedList6 = chunked(list, 6); // [["apple", "banana", "grape", "melon", "cherry"]]
def List<int> list = [1,2,3,4,5,6,7,8,9,10];
def List<List<int>> chunkedList1 = list.chunked(2); // [[1,2],[3,4],[5,6],[7,8],[9,10]]
def List<List<int>> chunkedList2 = chunked(list, 2); // [[1,2],[3,4],[5,6],[7,8],[9,10]]
def List<List<int>> chunkedList3 = list.chunked(3); // [[1,2,3],[4,5,6],[7,8,9],[10]]
def List<List<int>> chunkedList4 = chunked(list, 3); // [[1,2,3],[4,5,6],[7,8,9],[10]]
def List<List<int>> chunkedList5 = list.chunked(6); // [[1,2,3,4,5,6],[7,8,9,10]]
def List<List<int>> chunkedList6 = chunked(list, 6); // [[1,2,3,4,5,6],[7,8,9,10]]
windowed(List listValue, int size)
- Transforms a list into sliding window of given size
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def List<List<string>> chunkedList1 = list.windowed(2); // [["apple","banana"],["banana","grape"],["grape","melon"],["melon","cherry"]]
def List<List<string>> chunkedList2 = list.windowed(3); // [["apple","banana","grape"],["banana","grape","melon"],["grape","melon","cherry"]]
def List<List<string>> chunkedList3 = list.windowed(6); // [["apple","banana","grape","melon","cherry"]]
def List<List<string>> chunkedList4 = windowed(list, 2); // [["apple","banana"],["banana","grape"],["grape","melon"],["melon","cherry"]]
def List<List<string>> chunkedList5 = windowed(list, 3); // [["apple","banana","grape"],["banana","grape","melon"],["grape","melon","cherry"]]
def List<List<string>> chunkedList6 = windowed(list, 6); // [["apple","banana","grape","melon","cherry"]]
def List<int> list = [1,2,3,4,5,6,7,8,9,10];
def List<List<int>> chunkedList1 = list.windowed(2); // [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9],[9,10]]
def List<List<int>> chunkedList2 = windowed(list, 2); // [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9],[9,10]]
def List<List<int>> chunkedList3 = list.windowed(3); // [[1,2,3],[2,3,4],[3,4,5],[4,5,6],[5,6,7],[6,7,8],[7,8,9],[8,9,10]]
def List<List<int>> chunkedList4 = windowed(list, 3); // [[1,2,3],[2,3,4],[3,4,5],[4,5,6],[5,6,7],[6,7,8],[7,8,9],[8,9,10]]
randomElement(List listValue)
- Returns a randomElement from the given list
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def string randomElement = list.randomElement(); // ["melon"]
def string randomElement2 = randomElement(list); // ["apple"]
def List<int> list = [1,2,3,4,5,6,7,8,9,10];
def int randomElement = list.randomElement(); // 5
def int randomElement2 = randomElement(list); // 1
insertElementAt(List listValue, ElementType element, int position)
- Adds the element to listValue at given postion
def List<string> list = ["apple", "banana", "grape"];
list.insertElementAt("melon", 2); // list = ["apple","melon" ,"banana", "grape"]
list.insertElementAt("cherry", 3); // list = [apple","melon" ,"cherry","banana", "grape"]
insertElementAt(list, "avocado", 1); // list = ["avocado", "apple","melon" ,"cherry","banana", "grape"]
def List<int> list = [1,2,3,4,5];
list.insertElementAt(6, 2); // list = [1,6,2,3,4,5]
list.insertElementAt(7, 3); // list = [1,6,7,2,3,4,5]
insertElementAt(list, 8, 1); // list = [8,1,6,7,2,3,4,5]
Make sure that the position given is in range otherwise you'll get an error
removeElementAt(List listValue, int position)
- Removes the element at position from listValue
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
list.removeElementAt(2); // list = ["apple", "grape", "melon", "cherry"]
list.removeElementAt(3); // list = ["apple", "grape", "cherry"]
removeElementAt(list, 2); // list = ["apple", "cherry"]
def List<int> list = [1,2,3,4,5];
list.removeElementAt(2); // list = [1,3,4,5]
list.removeElementAt(3); // list = [1,3,5]
removeElementAt(list, 2); // list = [1,5]
Make sure that the position given is in range otherwise you'll get an error
fillElements(List listValue, ElementType element, int start, int end)
- Replaces all the elements in listValue with element from start position to end position (both inclusive)
def List<string> list = ["apple", "banana", "apple", "melon", "banana"];
list.fillElements("guava", 2, 4); // list = ["apple", "guava", "guava", "guava", "banana"]
fillElements(list, "avocado", 1, 1); // list = ["apple", "guava", "guava", "guava", "banana"]
def List<int> list = [1,2,3,4,5];
list.fillElements(6, 2, 4); // list = [1,6,6,6,5]
fillElements(list, 7, 1, 1); // list = [7,6,6,6,5]
Make sure that the start and end positions are in range otherwise you'll get an error
setElementAt(List listValue, ElementType element, int position)
- Replaces the item at given position in listValue with element
def List<string> list = ["apple", "banana", "apple", "melon", "banana"];
list.setElementAt("guava", 2); // list = ["apple", "guava", "apple", "melon", "banana"]
list.setElementAt("avocado", 4); // list = ["apple", "guava", "apple", "avocado", "banana"]
setElementAt(list, "avocado", 1); // list = ["avocado", "guava", "apple", "avocado", "banana"]
def List<int> list = [1,2,3,4,5];
list.setElementAt(6, 2); // list = [1,6,3,4,5]
setElementAt(list, 7, 1); // list = [7,6,3,4,5]
Make sure that the position given is in range otherwise you'll get an error
sumOfList(List listValue)
- Returns the sum of the numbers in the list
def List<real> list = [12.344,4554.224,-12,-782.24];
def real value = list.sumOfList(); // 3772.328
def real value2 = sumOfList(list); // 3772.328
def List<int> list = [1,2,3,4,5];
def int value = list.sumOfList(); // 15
def int value2 = sumOfList(list); // 15
Make sure that the elements of the list are of numeric type, otherwise you'll get an error
averageOfList(List listValue)
- Returns the average of the numbers in the list
def List<real> list = [12.344,4554.224,-12,-782.24];
def real value = list.averageOfList(); // 943.082
def real value2 = averageOfList(list); // 943.082
def List<int> list = [1,2,3,4,5];
def real value = list.averageOfList(); // 3.0
def real value2 = averageOfList(list); // 3.0
Make sure that the elements of the list are of numeric type, otherwise you'll get an error
geometricMeanOfList(List listValue)
- Returns the geometric mean of the numbers in the list
def List<real> list = [12.344,4554.224,-12,-782.24];
def real value = list.geometricMeanOfList(); // 151.56464
def real value2 = geometricMeanOfList(list); // 151.56464
def List<int> list = [1,2,3,4,5];
def real value = list.geometricMeanOfList(); // 2.605171084697352
def real value2 = geometricMeanOfList(list); // 2.605171084697352
Make sure that the elements of the list are of numeric type, otherwise you'll get an error
harmonicMeanOfList(List listValue)
- Returns the harmonic mean of the numbers in the list
def List<real> list = [12.344,4554.224,-12,-782.24];
def real value = list.harmonicMeanOfList(); // -1183.040115747569
def real value2 = harmonicMeanOfList(list); // -1183.040115747569
def List<int> list = [1,2,3,4,5];
def real value = list.harmonicMeanOfList(); // 2.18978102189781
def real value2 = harmonicMeanOfList(list); // 2.18978102189781
Make sure that the elements of the list are of numeric type, otherwise you'll get an error
randomList(int n)
- Generates a list of random real numbers between 0 and 1
def List<real> list = randomList(3); // [0.745438338251014,0.9296330604651375,0.4517410208132736]
def List<real> list = randomList(5); // [0.745438338251014,0.9296330604651375,0.4517410208132736,0.234567890123456,0.876543210987654]
nthPercentileInList(List listValue, float n)
- Returns the nth percentile element from given listValue
def List<real> list = [12.344,4554.224,-12,-782.24];
def real value = list.nthPercentileInList(25); // -782.24
def real value2 = nthPercentileInList(list, 25); // -782.24
def List<real> list = [12.344,4554.224,-12,-782.24];
def real value = list.nthPercentileInList(50); // -12.0
def real value2 = nthPercentileInList(list, 50); // -12.0
Make sure that the elements of the list are of numeric type, otherwise you'll get an error
medianOfList(List listValue)
- Returns the median element from given listValue
def List<real> list = [12.344,4554.224,-12,-782.24];
def real value = list.medianOfList(); // -12.0
def real value2 = medianOfList(list); // -12.0
def List<int> list = [1,2,3,4,5];
def int value = list.medianOfList(); // 3
def int value2 = medianOfList(list); // 3
Make sure that the elements of the list are of numeric type, otherwise you'll get an error
getElementAt(List listValue, int position)
- Returns the element at given position in the given listValue
def List<real> list = [12.344,4554.224,-12,-782.24];
def real value = list.getElementAt(2); // 4554.224
def real value2 = getElementAt(list, 2); // 4554.224
def List<int> list = [1,2,3,4,5];
def int value = list.getElementAt(2); // 2
def int value2 = getElementAt(list, 2); // 2
Make sure that the position given is in range otherwise you'll get an error
takeWhile(List listValue, Lambda (ElementType element, (Optional) int position) => boolean)
- This functions returns a new list by taking elements from the start of the listValue till the first time false is returned
def List<string> list = ["apple","grape", "banana","melon", "cherry"];
def List<string> takenList = list.takeWhile((item, index)->{
return item.length() == 5;
}); // takenList = ["apple", "grape"] Stops taking elements when "banana" is encountered as "banana".length() == 5 is false
def List<string> takenList2 = takeWhile(list, (item, index)->{
return item.length() == 5;
}); // takenList = ["apple", "grape"] Stops taking elements when "banana" is encountered as "banana".length() == 5 is false
def List<int> numbers = [4, 2, 5, 1, 3];
def List<int> takenNumbers = numbers.takeWhile((item, index)->{
return item > 1;
}); // takenNumbers = [4, 2, 5]. Stops taking elements when 1 is encountered as 1 > 1 is false
def List<int> takenNumbers2 = takeWhile(numbers, (item, index)->{
return item > 1;
}); // takenNumbers = [4, 2, 5]. Stops taking elements when 1 is encountered as 1 > 1 is false
takeLastWhile(List listValue, Lambda (ElementType element, (Optional) int position) => boolean)
- This functions returns a new list by taking elements from the end of the listValue till the first time false is returned
def List<string> list = ["cherry","melon","banana","grape","apple"];
def List<string> takenList = list.takeLastWhile((item, index)->{
return item.length() == 5;
}); // takenList = ["grape", "apple"] Stops taking elements when "banana" is encountered as "banana".length() == 5 is false
def List<string> takenList2 = takeLastWhile(list, (item, index)->{
return item.length() == 5;
}); // takenList = ["grape", "apple"] Stops taking elements when "banana" is encountered as "banana".length() == 5 is false
def List<int> numbers = [3,1,5,2,4];
def List<int> takenNumbers = numbers.takeLastWhile((item, index)->{
return item > 1;
}); // takenNumbers = [5,2,4]. Stops taking elements when 1 is encountered as 1 > 1 is false
def List<int> takenNumbers2 = takeLastWhile(numbers, (item, index)->{
return item > 1;
}); // takenNumbers = [5,2,4]. Stops taking elements when 1 is encountered as 1 > 1 is false
dropWhile(List listValue, Lambda (ElementType element, (Optional) int position) => boolean)
- This functions returns a new list by dropping elements from the start of the listValue till the first time false is returned
def List<string> list = ["apple","grape", "banana","melon", "cherry"];
def List<string> droppedList = list.dropWhile((item, index)->{
return item.length() == 5;
}); // droppedList = ["banana","melon", "cherry"] Stops dropping elements when "banana" is encountered as "banana".length() == 5 is false
def List<string> droppedList2 = dropWhile(list, (item, index)->{
return item.length() == 5;
}); // droppedList = ["banana","melon", "cherry"] Stops dropping elements when "banana" is encountered as "banana".length() == 5 is false
def List<int> numbers = [4, 2, 5, 1, 3];
def List<int> droppedNumbers = numbers.dropWhile((item, index)->{
return item > 1;
}); // droppedNumbers = [1, 3]. Stops dropping elements when 1 is encountered as 1 > 1 is false
def List<int> droppedNumbers2 = dropWhile(numbers, (item, index)->{
return item > 1;
}); // droppedNumbers = [1, 3]. Stops dropping elements when 1 is encountered as 1 > 1 is false
dropLastWhile(List listValue, Lambda (ElementType element, (Optional) int position) => boolean)
- This functions returns a new list by dropping elements from the end of the listValue till the first time false is returned
def List<string> list = ["cherry","melon","banana","grape","apple"];
def List<string> droppedList = list.dropLastWhile((item, index)->{
return item.length() == 5;
}); // droppedList = ["cherry","melon","banana"] Stops dropping elements when "banana" is encountered as "banana".length() == 5 is false
def List<string> droppedList2 = dropLastWhile(list, (item, index)->{
return item.length() == 5;
}); // droppedList = ["cherry","melon","banana"] Stops dropping elements when "banana" is encountered as "banana".length() == 5 is false
def List<int> numbers = [3,1,5,2,4];
def List<int> droppedNumbers = numbers.dropLastWhile((item, index)->{
return item > 1;
}); // droppedNumbers = [3, 1]. Stops dropping elements when 1 is encountered as 1 > 1 is false
def List<int> droppedNumbers2 = dropLastWhile(numbers, (item, index)->{
return item > 1;
}); // droppedNumbers = [3, 1]. Stops dropping elements when 1 is encountered as 1 > 1 is false
indexOf(List listValue, Lambda (ElementType element, (Optional) int position) => boolean)
- This functions returns the position of the first item for which the lambda returns true
- Returns 0 if the lambda doesn't evaluate to true for any element
def List<string> list = ["apple","grape", "banana","melon", "cherry"];
def int listIndex = list.indexOf((item, index)->{
return item.length() == 6;
}); // listIndex = 3 as "banana" is the first element for which item.length() == 6 is true
def int listIndex2 = indexOf(list, (item, index)->{
return item.length() == 6;
}); // listIndex = 3 as "banana" is the first element for which item.length() == 6 is true
def List<int> numbers = [4, 2, 5, 1, 3];
def int numbersIndex = numbers.indexOf((item, index)->{
return item % 2 == 1;
}); // numbersIndex = 3 as 5 is the first element for which item % 2 == 1 is true
def int numbersIndex2 = indexOf(numbers, (item, index)->{
return item % 2 == 1;
}); // numbersIndex = 3 as 5 is the first element for which item % 2 == 1 is true
lastIndexOf(List listValue, Lambda (ElementType element, (Optional) int position) => boolean)
- This functions returns the position of the last item for which the lambda returns true
- Returns 0 if the lambda doesn't evaluate to true for any element
def List<string> list = ["apple","grape", "banana","melon", "cherry"];
def int listIndex = list.lastIndexOf((item, index)->{
return item.length() == 6;
}); // listIndex = 5 as "cherry" is the last element for which item.length() == 6 is true
def int listIndex2 = lastIndexOf(list, (item, index)->{
return item.length() == 6;
}); // listIndex = 5 as "cherry" is the last element for which item.length() == 6 is true
def List<int> numbers = [4, 2, 5, 1, 3];
def int numbersIndex = numbers.lastIndexOf((item, index)->{
return item % 2 == 1;
}); // numbersIndex = 5 as 3 is the last element for which item % 2 == 1 is true
def int numbersIndex2 = lastIndexOf(numbers, (item, index)->{
return item % 2 == 1;
}); // numbersIndex = 5 as 3 is the last element for which item % 2 == 1 is true
findFirst(List listValue, Lambda (ElementType element, (Optional) int position) => boolean)
- This functions returns the first item for which the lambda returns true
- Returns null if the lambda doesn't evaluate to true for any element
def List<string> list = ["apple","grape", "banana","melon", "cherry"];
def string listItem = list.findFirst((item, index)->{
return item.length() == 6;
}); // listItem = "banana" as "banana" is the first element for which item.length() == 6 is true
def string listItem2 = findFirst(list, (item, index)->{
return item.length() == 6;
}); // listItem = "banana" as "banana" is the first element for which item.length() == 6 is true
def List<int> numbers = [4, 2, 5, 1, 3];
def int numbersItem = numbers.findFirst((item, index)->{
return item % 2 == 1;
}); // numbersItem = 5 as 5 is the first element for which item % 2 == 1 is true
def int numbersItem2 = findFirst(numbers, (item, index)->{
return item % 2 == 1;
}); // numbersItem = 5 as 5 is the first element for which item % 2 == 1 is true
findLast(List listValue, Lambda (ElementType element, (Optional) int position) => boolean)
- This functions returns the last item for which the lambda returns true
- Returns null if the lambda doesn't evaluate to true for any element
def List<string> list = ["apple","grape", "banana","melon", "cherry"];
def string listItem = list.findLast((item, index)->{
return item.length() == 6;
}); // listItem = "cherry" as "cherry" is the last element for which item.length() == 6 is true
def string listItem2 = findLast(list, (item, index)->{
return item.length() == 6;
}); // listItem = "cherry" as "cherry" is the last element for which item.length() == 6 is true
def List<int> numbers = [4, 2, 5, 1, 3];
def int numbersItem = numbers.findLast((item, index)->{
return item % 2 == 1;
}); // numbersItem = 3 as 3 is the last element for which item % 2 == 1 is true
def int numbersItem2 = findLast(numbers, (item, index)->{
return item % 2 == 1;
}); // numbersItem = 3 as 3 is the last element for which item % 2 == 1 is true
zip(List listValue1, List listValue2)
- Returns a Map by mapping from an element in listValue1 to its corresponding element in listValue2
- If the two lists are of different sizes, then the size of the returned Map is the size of the smaller list
def List<string> list = ["apple","grape", "banana","melon", "cherry"];
def List<int> numbers = [4, 2, 5, 1, 3];
def Map <string, int> value = list.zip(numbers); // "apple" -> 4,"grape" -> 2, "banana" -> 5,"melon" -> 1, "cherry" -> 3
def Map <string, int> value2 = zip(list, numbers); // "apple" -> 4,"grape" -> 2, "banana" -> 5,"melon" -> 1, "cherry" -> 3
def List<string> list = ["apple","grape", "banana","melon", "cherry"];
def List<int> numbers = [4, 2, 5, 1, 3];
def Map <int, string> value = numbers.zip(list); // 4 -> "apple",2 -> "grape", 5 -> "banana",1 -> "melon", 3 -> "cherry"
def Map <int, string> value2 = zip(numbers, list); // 4 -> "apple",2 -> "grape", 5 -> "banana",1 -> "melon", 3 -> "cherry"
unzip(Map mapValue)
- This is the opposite of zip. It takes a Map and returns a List of two Lists
- First List contains all the keys of the map
- Second List contains all the values of the map
- Note that, in the map type of the key should be equal to the type of the value, otherwise you'll get an error
def Map<string, string> fruitColorMap= mapOf("apple" to "red", "banana" to "yellow", "orange" to "orange");
def List<List<string>> value = fruitColorMap.unzip(); // [["apple","banana","orange"],["red","yellow","orange"]]
def List<List<string>> value2 = unzip(fruitColorMap); // [["apple","banana","orange"],["red","yellow","orange"]]
def Map<int, int> numberToTheirSquares= mapOf(1 to 1, 2 to 4, 3 to 9, 4 to 16, 5 to 25);
def List<List<int>> value = numberToTheirSquares.unzip(); // [[1,2,3,4,5],[1,4,9,16,25]]
def List<List<int>> value2 = unzip(numberToTheirSquares); // [[1,2,3,4,5],[1,4,9,16,25]]
sort(List listValue)
- Returns a sorted list from the given listValue
def List<string> list = ["banana", "apple", "grape", "melon", "cherry"];
def List<string> sortedList = list.sort(); // ["apple", "banana", "cherry", "grape", "melon"]
def List<string> sortedList2 = sort(list); // ["apple", "banana", "cherry", "grape", "melon"]
def List<int> numbers = [4, 2, 5, 1, 3];
def List<int> sortedNumbers = numbers.sort(); // [1,2,3,4,5]
def List<int> sortedNumbers2 = sort(numbers); // [1,2,3,4,5]
sort can only be called on List of type int, real, string, Date, Time and DateTime
sortBy(List listValue, Lambda (ElementType element, (Optional) int position) => Type)
- Returns a sorted list from the given listValue by comparing the corresponding value returned from the lambda
def List<string> list = ["banana", "apple", "grape", "melon", "cherry"];
def List<string> sortedList = list.sortBy((item, index)->{
return item.substring(item.length(), item.length()); // returns the last character of the string
}); // ["banana","apple", "grape", "melon", "cherry" ]
def List<string> sortedList2 = sortBy(list, (item, index)->{
return item.substring(item.length(), item.length()); // returns the last character of the string
}); // ["banana","apple", "grape", "melon", "cherry" ]
def List<int> numbers = [4, -2, 5, -1, 3];
def List<int> sortedNumbers = numbers.sortBy((item, index)->{
return item * item; // returns the square of the number
}); // [-1, -2, 3, 4, 5]
def List<int> sortedNumbers2 = sortBy(numbers, (item, index)->{
return item * item; // returns the square of the number
}); // [-1, -2, 3, 4, 5]
distinct(List listValue)
- Returns all the distinct elements from the listValue
def List<string> list = ["apple", "banana", "apple", "melon", "banana", "tomato"];
def List<string> distinctElements = list.distinct(); // distinctElements = ["melon", "tomato"]
def List<string> distinctElements2 = distinct(list); // distinctElements = ["melon", "tomato"]
def List<int> numbers = [4, 2, 5, 1, 3, 2, 4, 5];
def List<int> distinctNumbers = numbers.distinct(); // distinctNumbers = [1, 3]
def List<int> distinctNumbers2 = distinct(numbers); // distinctNumbers = [1, 3]
duplicated(List listValue)
- Returns all the elements that are present more than once in the listValue
def List<string> list = ["apple", "banana", "apple", "melon", "banana", "tomato"];
def List<string> duplicatedElements = list.duplicated(); // duplicatedElements = ["apple", "banana"]
def List<string> duplicatedElements2 = duplicated(list); // duplicatedElements = ["apple", "banana"]
def List<int> numbers = [4, 2, 5, 1, 3, 2, 4, 5];
def List<int> duplicatedNumbers = numbers.duplicated(); // duplicatedNumbers = [2, 4, 5]
def List<int> duplicatedNumbers2 = duplicated(numbers); // duplicatedNumbers = [2, 4, 5]
unique(List listValue)
- Returns all the unique elements from the listValue
def List<string> list = ["apple", "banana", "apple", "melon", "banana", "tomato"];
def List<string> uniqueElements = list.unique(); // uniqueElements = ["apple", "banana", "melon", "tomato"]
def List<string> uniqueElements2 = unique(list); // uniqueElements = ["apple", "banana", "melon", "tomato"]
def List<int> numbers = [4, 2, 5, 1, 3, 2, 4, 5];
def List<int> uniqueNumbers = numbers.unique(); // uniqueNumbers = [4, 2, 5, 1, 3]
def List<int> uniqueNumbers2 = unique(numbers); // uniqueNumbers = [4, 2, 5, 1, 3]
uniqueBy(List listValue, string field)
- Returns all the unique elements from the listValue by comparing the filed
def class Employee {
def string name;
def string dept;
}
Employee john = {
"name": "John Doe",
"dept": "HR",
"salary": 45000.0
};
Employee jane = {
"name": "Jane Doe",
"dept": "Engineering",
"salary": 71000.0
};
Employee chris = {
"name": "Chris Lee",
"dept": "Engineering",
"salary": 30000.0
};
def List<Employee> employees = [john, jane, chris];
def List<Employee> uniqueElements = employees.uniqueBy("dept");
def List<Employee> uniqueElements2 = uniqueBy(employees, "dept");
/*
[
{
"name": "John Doe",
"dept": "HR",
"salary": 45000.0
},
{
"name": "Chris Lee",
"dept": "Engineering",
"salary": 30000.0
}
]
*/
def class Employee {
def string name;
def string dept;
}
Employee employee1 = {
"name": "John",
"dept": "HR",
"salary": 45000.0
};
Employee employee2 = {
"name": "John",
"dept": "Engineering",
"salary": 71000.0
};
Employee employee3 = {
"name": "Jane",
"dept": "Engineering",
"salary": 30000.0
};
def List<Employee> employees = [employee1, employee2, employee3];
def List<Employee> uniqueElements = employees.uniqueBy("name");
def List<Employee> uniqueElements2 = uniqueBy(employees, "name");
/*
[
{
"name": "John",
"dept": "HR",
"salary": 45000.0
},
{
"name": "Jane",
"dept": "Engineering",
"salary": 30000.0
}
]
*/
This function does not preserve ordering
toSet(List listValue)
- Converts a given
ListtoSet
def List<string> list = ["apple", "banana", "apple", "melon", "banana", "tomato"];
def Set<string> uniqueElements = list.toSet(); // ["apple", "banana", "melon", "tomato"]
def Set<string> uniqueElements2 = toSet(list); // ["apple", "banana", "melon", "tomato"]
def List<int> numbers = [1, 7,1,4,7,2,3,5];
def Set<int> uniqueNumbers = numbers.toSet(); // [1,7,4,2,3,5]
def Set<int> uniqueNumbers2 = toSet(numbers); // [1,7,4,2,3,5]
equalsIgnoreOrder(List listValue1, List listValue2)
- Returns true if two lists are equal ignoring their order
- You can use the
notvariant of this function as detailed here
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def boolean value1 = list.equalsIgnoreOrder(["banana", "cherry", "apple","melon", "grape"]); // true
def boolean value2 = equalsIgnoreOrder(list, ["banana", "cherry", "apple","melon", "grape"]); // true
def boolean value3 = list.equalsIgnoreOrder(["banana", "cherry", "apple","melon", "guava"]); // false
def boolean value4 = equalsIgnoreOrder(list, ["banana", "cherry", "apple","melon", "guava"]); // false
def List<int> numbers1 = [1, 2, 3, 4, 5];
def List<int> numbers2 = [5, 4, 3, 2, 1];
def boolean value1 = numbers1.equalsIgnoreOrder(numbers2); // true
def boolean value2 = equalsIgnoreOrder(numbers1, numbers2); // true
hasElementBeforeElement(List value, Item item1, Item item2)
- Returns true if item1 is present in the list before item2
- You can use the
notvariant of this function as detailed here
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def boolean value1 = list.hasElementBeforeElement("apple", "banana"); // true
def boolean value2 = hasElementBeforeElement(list, "apple", "banana"); // true
def boolean value2 = list.hasElementBeforeElement("banana", "apple"); // false
def boolean value4 = hasElementBeforeElement(list, "banana", "apple"); // false
def List<int> numbers = [1, 2, 3, 4, 5];
def boolean value1 = numbers.hasElementBeforeElement(2, 3); // true
def boolean value2 = hasElementBeforeElement(numbers, 2, 3); // true
def boolean value2 = numbers.hasElementBeforeElement(3, 2); // false
def boolean value4 = hasElementBeforeElement(numbers, 3, 2); // false
hasElementAtAnyOfIndices(List value, Item item, List<int> indices)
- Returns true if item is present in the list at any of the given indices
- You can use the
notvariant of this function as detailed here
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def boolean value1 = list.hasElementAtAnyOfIndices("apple", [1,3,5]); // true
def boolean value2 = hasElementAtAnyOfIndices(list, "apple", [1,3,5]); // true
def boolean value2 = list.hasElementAtAnyOfIndices("banana", [1,3,5]); // false
def boolean value4 = hasElementAtAnyOfIndices(list, "banana", [1,3,5]); // false
def List<int> numbers = [1, 2, 3, 4, 5];
def boolean value1 = numbers.hasElementAtAnyOfIndices(2, [1,3,5]); // false
def boolean value2 = hasElementAtAnyOfIndices(numbers, 2, [1,3,5]); // false
def boolean value2 = numbers.hasElementAtAnyOfIndices(3, [1,3,5]); // true
def boolean value4 = hasElementAtAnyOfIndices(numbers, 3, [1,3,5]); // true
hasSubList(List list1, List list2)
- Returns true if list2 is present as a contiguous ordered sublist in list1
- You can use the
notvariant of this function as detailed here
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def boolean value1 = list.hasSubList(["banana", "grape"]); // true
def boolean value2 = hasSubList(list, ["banana", "grape"]); // true
def boolean value2 = list.hasSubList(["banana", "melon"]); // false
def boolean value4 = hasSubList(list, ["banana", "melon"]); // false
def List<int> numbers = [1, 2, 3, 4, 5];
def boolean value1 = numbers.hasSubList([2, 3]); // true
def boolean value2 = hasSubList(numbers, [2, 3]); // true
def boolean value2 = numbers.hasSubList([1, 4]); // false
def boolean value4 = hasSubList(numbers, [1, 4]); // false
Set functions
isSubset(Set lhsSet, Set rhsSet)
- Used to check if
lhsSetis a subset ofrhsSet - You can use the
notvariant of this function as detailed here
def Set<string> set1 = createSetFrom("apple", "banana", "apple", "melon", "banana", "tomato");
def Set<string> set2 = createSetFrom("apple", "melon", "banana", "tomato", "tomato", "orange");
def boolean isSubset = set1.isSubset(set2); // true
def boolean isSubset2 = isSubset(set1, set2); // true
def boolean isSubset3 = set2.isSubset(set1); // false
def boolean isSubset4 = isSubset(set2, set1); // false
def Set<int> set1 = createSetFrom(1, 2, 3, 4, 5);
def Set<int> set2 = createSetFrom(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
def boolean isSubset = set1.isSubset(set2); // true
def boolean isSubset2 = isSubset(set1, set2); // true
def boolean isSubset3 = set2.isSubset(set1); // false
def boolean isSubset4 = isSubset(set2, set1); // false
isSuperset(Set lhsSet, Set rhsSet)
- Used to check if
lhsSetis a superset ofrhsSet - You can use the
notvariant of this function as detailed here
def Set<string> set1 = createSetFrom("apple", "banana", "apple", "melon", "banana", "tomato");
def Set<string> set2 = createSetFrom("apple", "melon", "banana", "tomato", "tomato", "orange");
def boolean isSuperset = set1.isSuperset(set2); // false
def boolean isSuperset2 = isSuperset(set1, set2); // false
def boolean isSuperset3 = set2.isSuperset(set1); // true
def boolean isSuperset4 = isSuperset(set2, set1); // true
def Set<int> set1 = createSetFrom(1, 2, 3, 4, 5);
def Set<int> set2 = createSetFrom(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
def boolean isSuperset = set1.isSuperset(set2); // false
def boolean isSuperset2 = isSuperset(set1, set2); // false
def boolean isSuperset3 = set2.isSuperset(set1); // true
def boolean isSuperset4 = isSuperset(set2, set1); // true
isPureSubset(Set lhsSet, Set rhsSet)
- Used to check if
lhsSetis a pure subset ofrhsSet - You can use the
notvariant of this function as detailed here
def Set<string> set1 = createSetFrom("apple", "banana", "apple", "melon", "banana", "tomato");
def Set<string> set2 = createSetFrom("apple", "melon", "banana", "tomato", "tomato", "orange");
def boolean isPureSubset = set1.isPureSubset(set2); // true
def boolean isPureSubset2 = isPureSubset(set1, set2); // true
def boolean isPureSubset3 = set2.isPureSubset(set1); // false
def boolean isPureSubset4 = isPureSubset(set2, set1); // false
def Set<int> set1 = createSetFrom(1, 2, 3, 4, 5);
def Set<int> set2 = createSetFrom(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
def boolean isPureSubset = set1.isPureSubset(set2); // true
def boolean isPureSubset2 = isPureSubset(set1, set2); // true
def boolean isPureSubset3 = set2.isPureSubset(set1); // false
def boolean isPureSubset4 = isPureSubset(set2, set1); // false
isPureSuperset(Set lhsSet, Set rhsSet)
- Used to check if
lhsSetis a pure superset ofrhsSet - You can use the
notvariant of this function as detailed here
def Set<string> set1 = createSetFrom("apple", "banana", "apple", "melon", "banana", "tomato");
def Set<string> set2 = createSetFrom("apple", "melon", "banana", "tomato", "tomato", "orange");
def boolean isPureSuperset = set1.isPureSuperset(set2); // false
def boolean isPureSuperset2 = isPureSuperset(set1, set2); // false
def boolean isPureSuperset3 = set2.isPureSuperset(set1); // true
def boolean isPureSuperset4 = isPureSuperset(set2, set1); // true
def Set<int> set1 = createSetFrom(1, 2, 3, 4, 5);
def Set<int> set2 = createSetFrom(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
def boolean isPureSuperset = set1.isPureSuperset(set2); // false
def boolean isPureSuperset2 = isPureSuperset(set1, set2); // false
def boolean isPureSuperset3 = set2.isPureSuperset(set1); // true
def boolean isPureSuperset4 = isPureSuperset(set2, set1); // true
union(Set lhsSet, Set rhsSet)
- Used to create union of
lhsSetandrhsSet
def Set<string> set1 = createSetFrom("apple", "banana", "apple", "melon", "banana", "tomato");
def Set<string> set2 = createSetFrom("apple", "melon", "tomato", "tomato", "orange");
def Set<string> result1 = set1.union(set2); // ["apple", "banana", "melon", "tomato", "orange"]
def Set<string> result2 = union(set1, set2); // ["apple", "banana", "melon", "tomato", "orange"]
def Set<int> set1 = createSetFrom(1, 2, 3, 4, 5);
def Set<int> set2 = createSetFrom(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
def Set<int> result1 = set1.union(set2); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def Set<int> result2 = union(set1, set2); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
intersection(Set lhsSet, Set rhsSet)
- Used to create intersection of
lhsSetandrhsSet
def Set<string> set1 = createSetFrom("apple", "banana", "apple", "melon", "banana", "tomato");
def Set<string> set2 = createSetFrom("apple", "melon", "tomato", "tomato", "orange");
def Set<string> result1 = set1.intersection(set2); // ["apple", "melon", "tomato"]
def Set<string> result2 = intersection(set1, set2); // ["apple", "melon", "tomato"]
def Set<int> set1 = createSetFrom(1, 2, 3, 4, 5);
def Set<int> set2 = createSetFrom(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
def Set<int> result1 = set1.intersection(set2); // [1, 2, 3, 4, 5]
def Set<int> result2 = intersection(set1, set2); // [1, 2, 3, 4, 5]
difference(Set lhsSet, Set rhsSet)
- Used to get items present in
lhsSetbut NOT inrhsSet
def Set<string> set1 = createSetFrom("apple", "banana", "apple", "melon", "banana", "tomato");
def Set<string> set2 = createSetFrom("apple", "melon", "tomato", "tomato", "orange");
def Set<string> result1 = set1.difference(set2); // ["banana"]
def Set<string> result2 = difference(set1, set2); // ["banana"]
def Set<int> set1 = createSetFrom(1, 2, 3, 4, 5);
def Set<int> set2 = createSetFrom(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
def Set<int> result1 = set1.difference(set2); // [1, 2, 3, 4, 5]
def Set<int> result2 = difference(set1, set2); // [1, 2, 3, 4, 5]
symmetricDifference(Set lhsSet, Set rhsSet)
- Used to get items present in
lhsSetor inrhsSetbut not in both the sets
def Set<string> set1 = createSetFrom("apple", "banana", "apple", "melon", "banana", "tomato");
def Set<string> set2 = createSetFrom("apple", "melon", "tomato", "tomato", "orange");
def Set<string> result1 = set1.symmetricDifference(set2); // ["banana", "orange"]
def Set<string> result2 = symmetricDifference(set1, set2); // ["banana", "orange"]
def Set<int> set1 = createSetFrom(1, 2, 3, 4, 5);
def Set<int> set2 = createSetFrom(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
def Set<int> result1 = set1.symmetricDifference(set2); // [6, 7, 8, 9, 10]
def Set<int> result2 = symmetricDifference(set1, set2); // [6, 7, 8, 9, 10]
clearSet(Set set)
- Used to clear out a set
def Set<string> set1 = createSetFrom("apple", "banana", "apple", "melon", "banana", "tomato");
set1.clearSet(); // []
clearSet(set1); // []
def Set<int> set1 = createSetFrom(1, 2, 3, 4, 5);
set1.clearSet(); // []
clearSet(set1); // []
createSetFrom(item... items)
- Used to create a new
Setfrom the given items (Any number of items >= 1 can be specified) - All items need to be of the same type as
Setis a homogenous collection of items
def Set<string> fruits = createSetFrom("apple", "banana", "grape","apple", "banana") // ["apple", "banana", "grape"];
def Set<string> favouriteNumbers = createSetFrom(7, 1, 2, 2, 4, 7) // [7, 1, 2, 4];
findOne(Set set, Lambda (ElementType element, (Optional) int position) => boolean)
- This functions returns an item for which the lambda returns true
- Returns null if the lambda doesn't true for any element
def Set<string> set = createSetFrom("apple","grape", "banana","melon", "cherry");
def string setItem = set.findOne((item)->{
return item.length() == 6;
}); // setItem = "banana" as "banana" is an element for which item.length() == 6 is true
def string setItem2 = findOne(set, (item)->{
return item.length() == 6;
}); // setItem2 = "banana" as "banana" is an element for which item.length() == 6 is true
def Set<int> numbers = createSetFrom(4, 2, 5, 1, 3);
def int numbersItem = numbers.findOne((item)->{
return item % 2 == 1;
}); // numbersItem = 5 as 5 is an element for which item % 2 == 1 is true
def int numbersItem2 = findOne(numbers, (item)->{
return item % 2 == 1;
}); // numbersItem2 = 5 as 5 is an element for which item % 2 == 1 is true
toList(Set setValue)
- Converts a given
SettoList
def Set<string> set = createSetFrom("apple", "banana", "melon", "tomato");
def List<string> list = set.toList(); // ["apple", "banana", "melon", "tomato"]
def List<string> list2 = toList(set); // ["apple", "banana", "melon", "tomato"]
def Set<int> set = createSetFrom(1, 2, 3, 4, 5);
def List<int> list = set.toList(); // [1, 2, 3, 4, 5]
def List<int> list2 = toList(set); // [1, 2, 3, 4, 5]
Functions on both Set and List
addElement((List | Set) value, ElementType element)
- If value is type of list, then this function appends the element to the end of the
List. Adds the element to the set if value is of type Set - Note that if the value is of type
Set, and the element is already present in the set, then the operation is ignored
def List<string> list = ["apple", "banana", "grape"];
list.addElement("melon"); // ["apple", "banana", "grape", "melon" ]
addElement(list, "cherry"); // ["apple", "banana", "grape", "melon", "cherry"]
def Set<string> set = createSetFrom("apple", "banana", "grape");
set.addElement("melon"); // ["apple", "banana", "grape", "melon" ]
addElement(set, "melon"); // ["apple", "banana", "grape", "melon"]
removeElements((List | Set) value, ElementType element)
- Removes all occurrences of element from
ListorSet
def List<string> list = ["apple", "banana", "apple", "melon", "banana"];
list.removeElements("melon"); // ["apple", "banana", "apple", "banana"]
removeElements(list, "apple"); // ["banana", "banana"]
def Set<string> set = createSetFrom("apple", "banana", "apple", "melon", "banana");
set.removeElements("melon"); // ["apple", "banana"]
removeElements(set, "apple"); // ["banana"]
randomSubset((List | Set) value, int n)
- Returns a random subset of maximum size n from the given
SetorList - If n > size of value, the returned subset has the same size as is the
SetorList
def List<real> list = [12.344,4554.224,-12,-782.24];
def Set<real> value1 = list.randomSubset(2); // [4554.224,-782.24]
def Set<real> value2 = randomSubset(list, 2); // [4554.224,-782.24]
def Set<string> set = createSetFrom("a","b","a","c","d");
def Set<string> value3 = set.randomSubset(2); // ["a", "c"]
def Set<string> value4 = randomSubset(set, 2); // ["c", "d"]
smallestElement((List | Set) value)
- Returns the smallest element in the given
SetorList
def List<int> list = [5, 6, 12, -7, -9];
def int result1 = list.smallestElement(); // -9
def int result2 = smallestElement(list); // -9
def Set<int> set = createSetFrom(5, 6, 12, -7, -9);
def int result3 = set.smallestElement(); // -9
def int result4 = smallestElement(set); // -9
Make sure that the elements of the list or set are of numeric type, otherwise you'll get an error
largestElement((List | Set) value)
- Returns the largest element in the given
SetorList
def List<int> list = [5, 6, 12, -7, -9];
def int result1 = list.largestElement(); // 12
def int result2 = largestElement(list); // 12
def Set<int> set = createSetFrom(5, 6, 12, -7, -9);
def int result3 = set.largestElement(); // 12
def int result4 = largestElement(set); // 12
Make sure that the elements of the list or set are of numeric type, otherwise you'll get an error
nthLargestElement((List | Set) value, int n)
- Returns the nth largest element in the given
SetorList
def List<int> list = [5, 6, 12, -7, -9];
def int result1 = list.nthLargestElement(2); // 6
def int result2 = nthLargestElement(list, 2); // 6
def Set<int> set = createSetFrom(5, 6, 12, -7, -9);
def int result3 = set.nthLargestElement(2); // 6
def int result4 = nthLargestElement(set, 2); // 6
Make sure that the elements of the list or set are of numeric type and n <= size of value, otherwise you'll get an error
nthSmallestElement((List | Set) value, int n)
- Returns the nth smallest element in the given
SetorList
def List<int> list = [5, 6, 12, -7, -9];
def int result1 = list.nthSmallestElement(4); // 6
def int result2 = nthSmallestElement(list, 4); // 6
def Set<int> set = createSetFrom(5, 6, 12, -7, -9);
def int result3 = set.nthSmallestElement(4); // 6
def int result4 = nthSmallestElement(set, 4); // 6
Make sure that the elements of the list or set are of numeric type and n <= size of value, otherwise you'll get an error
maxBy((List | Set) value, Lambda (ElementType element, (Optional) int position) => Type)
- Returns the largest element from the given
ListorSetby comparing the corresponding value returned from the lambda
def List<string> list = ["banana", "apple", "grape", "melon", "cherry"];
def string maxElementInList = list.maxBy((item, index)->{
return item.substring(item.length(), item.length()); // returns the last character of the string
}); // "cherry"
def string maxElementInList2 = maxBy(list, (item, index)->{
return item.substring(item.length(), item.length()); // returns the last character of the string
}); // "cherry"
def Set<string> set = createSetFrom("banana", "apple", "grape", "melon", "cherry");
def string maxElementInSet = set.maxBy((item)->{
return item.substring(item.length(), item.length()); // returns the last character of the string
}); // "cherry"
def string maxElementInSet2 = maxBy(set, (item)->{
return item.substring(item.length(), item.length()); // returns the last character of the string
}); // "cherry"
minBy((List | Set) value, Lambda (ElementType element, (Optional) int position) => Type)
- Returns the smallest element from the given
ListorSetby comparing the corresponding value returned from the lambda
def List<string> list = ["banana", "apple", "grape", "melon", "cherry"];
def string minElementInList = list.minBy((item)->{
return item.substring(item.length(), item.length()); // returns the last character of the string
}); // "banana"
def string minElementInList2 = minBy(list, (item)->{
return item.substring(item.length(), item.length()); // returns the last character of the string
}); // "banana"
def Set<string> set = createSetFrom("banana", "apple", "grape", "melon", "cherry");
def string minElementInSet = set.minBy((item)->{
return item.substring(item.length(), item.length()); // returns the last character of the string
}); // "banana"
def string minElementInSet2 = minBy(set, (item)->{
return item.substring(item.length(), item.length()); // returns the last character of the string
}); // "banana"
joinToString((List | Set) value, string separator)
- Returns a string after concatenating all the elements of the
ListorSetwith the given separator
def List<string> list = ["banana", "apple", "grape", "melon", "cherry"];
def string joinedList1 = list.joinToString("---"); // banana---apple---grape---melon---cherry
def string joinedList2 = joinToString(list, ", "); // banana, apple, grape, melon, cherry
def Set<string> set = createSetFrom("banana", "apple", "grape", "melon", "cherry");
def string joinedSet3 = set.joinToString("---"); // banana---apple---grape---melon---cherry
def string joinedSet4 = joinToString(set, ", "); // banana, apple, grape, melon, cherry
anyMatch((List | Set) value, Lambda (ElementType element, (Optional) int position) => boolean)
- Returns true if the lambda returns true for any element in the
ListorSet, otherwise returns false - You can use the
notvariant of this function as detailed here
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def boolean value1 = list.anyMatch((item, index)->{
return item.startsWith("a");
}); // true
def boolean value2 = anyMatch(list, (item, index)->{
return item.startsWith("d");
}); // false
def Set<string> set = createSetFrom("apple", "banana", "grape", "melon", "cherry");
def boolean value3 = set.anyMatch((item, index)->{
return item.startsWith("a");
}); // true
def boolean value4 = anyMatch(set, (item, index)->{
return item.startsWith("d");
}); // false
allMatch((List | Set) value, Lambda (ElementType element, (Optional) int position) => boolean)
- Returns true if the lambda returns true for all the elements in the
ListorSet, otherwise returns false - You can use the
notvariant of this function as detailed here
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def boolean value1 = list.allMatch((item, index)->{
return item.length() < 7;
}); // true
def boolean value2 = allMatch(list, (item, index)->{
return item.length() > 6;
}); // false
def Set<string> set = createSetFrom("apple", "banana", "grape", "melon", "cherry");
def boolean value3 = set.allMatch((item, index)->{
return item.length() < 7;
}); // true
def boolean value4 = allMatch(set, (item, index)->{
return item.length() > 6;
}); // false
noneMatch((List | Set) value, Lambda (ElementType element, (Optional) int position) => boolean)
- Returns true if the lambda returns true for none of the elements in the
ListorSet, otherwise returns false - You can use the
notvariant of this function as detailed here
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def boolean value1 = list.noneMatch((item, index)->{
return item.startsWith("a");
}); // false
def boolean value2 = noneMatch(list, (item, index)->{
return item.startsWith("d");
}); // true
def Set<string> set = createSetFrom("apple", "banana", "grape", "melon", "cherry");
def boolean value3 = set.noneMatch((item, index)->{
return item.startsWith("a");
}); // false
def boolean value4 = noneMatch(set, (item, index)->{
return item.startsWith("d");
}); // true
associateWith((List | Set) value, Lambda (ElementType element, (Optional) int position) => Type)
- Creates a Map by mapping from each element of the
ListorSetto the corresponsing value returned from the lambda
def List<string> list = ["apple","grape", "banana","melon", "cherry"];
def Map<string, number> fruitToLengthMap = list.associateWith((item, index)->{
return item.length();
}); // "apple" -> 5,"grape" -> 5, "banana" -> 6,"melon" -> 5, "cherry" -> 6
def List<int> numbers = [4, 2, 5, 1, 3];
def Map<int, boolean> numbersToParityMap = associateWith(numbers, (item, index)->{
return item % 2 == 0;
}); // 4 -> true, 2 -> true, 5 -> false, 1 -> false, 3 -> false
def Set<string> set = createSetFrom("apple","grape", "banana","melon", "cherry");
def Map<string, number> fruitToLengthMap = set.associateWith((item, index)->{
return item.length();
}); // "apple" -> 5,"grape" -> 5, "banana" -> 6,"melon" -> 5, "cherry" -> 6
def Set<int> numbers = createSetFrom(4, 2, 5, 1, 3);
def Map<int, boolean> numbersToParityMap = associateWith(numbers, (item, index)->{
return item % 2 == 0;
}); // 4 -> true, 2 -> true, 5 -> false, 1 -> false, 3 -> false
associateBy((List | Set) value, Lambda (ElementType element, (Optional) int position) => Type)
- Similar to
associateWithbut the key and value types are switched. Creates a Map by mapping to each element of theListorSetfrom the corresponsing value returned from the lambda
def List<string> list = ["apple","grape", "banana","melon", "cherry"];
def Map<string, string> firstLetterToFruitMap = list.associateBy((item, index)->{
return item.substring(1, 1);
}); // "a" -> "apple","g" -> "grape" ,"b" -> "banana","m" -> "melon" ,"c" -> "cherry"
def List<int> numbers = [4, 2, 5, 1, 3];
def Map<int, int> doubleToNumberMap = associateBy(numbers, (item, index)->{
return item * 2;
}); // 8 -> 4, 4 -> 2, 10 -> 5, 2 -> 1, 6 -> 3
def Set<string> set = createSetFrom("apple","grape", "banana","melon", "cherry");
def Map<string, string> firstLetterToFruitMap = set.associateBy((item, index)->{
return item.substring(1, 1);
}); // "a" -> "apple","g" -> "grape" ,"b" -> "banana","m" -> "melon" ,"c" -> "cherry"
def Set<int> numbers = createSetFrom(4, 2, 5, 1, 3);
def Map<int, int> doubleToNumberMap = associateBy(numbers, (item, index)->{
return item * 2;
}); // 8 -> 4, 4 -> 2, 10 -> 5, 2 -> 1, 6 -> 3
associate((List | Set) value, Lambda (ElementType element, (Optional) int position) => MapEntry)
- Generalized form of
associateWithandassociateBy. Creates a Map by looping over theListorSetand returning a corresponding MapEntry value
def List<string> list = ["apple","grape", "banana","melon", "cherry"];
def Map<string, int> firstLetterToLengthMap = list.associate((item, index)->{
return item.substring(1, 1) to item.length();
}); // "a" -> 5,"g" -> 5 ,"b" -> 6,"m" -> 5 ,"c" -> 6
def List<int> numbers = [4, 2, 5, 1, 3];
def Map<int, boolean> doubleToParityMap = associate(numbers, (item, index)->{
return item * 2 to (item % 2 == 0);
}); // 8 -> true, 4 -> true, 10 -> false, 2 -> false, 6 -> false
def Set<string> set = createSetFrom("apple","grape", "banana","melon", "cherry");
def Map<string, int> firstLetterToLengthMap = set.associate((item, index)->{
return item.substring(1, 1) to item.length();
}); // "a" -> 5,"g" -> 5 ,"b" -> 6,"m" -> 5 ,"c" -> 6
def Set<int> numbers = createSetFrom(4, 2, 5, 1, 3);
def Map<int, boolean> doubleToParityMap = associate(numbers, (item, index)->{
return item * 2 to (item % 2 == 0);
}); // 8 -> true, 4 -> true, 10 -> false, 2 -> false, 6 -> false
partition((List | Set) value, Lambda (ElementType element, (Optional) int position) => boolean)
- Divides a
Listinto two sublists or aSetinto two subsets. The firstListorSetcontains elements for which the lambda returns true, the secondListorSetcontains elements for which the lambda returns false
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def List<List<string>> partitionedList = list.partition((item, index)->{
return item.length() == 5;
}); // partitionedList = [["apple", "grape", "melon"],["banana", "cherry"]]
def List<int> numbers = [1...10];
def List<List<int>> partitionedNumbers = partition(numbers, (item, index)->{
return item % 2 == 1;
}); // partitionedNumbers = [[1,3,5,7,9], [2,4,6,8,10]]
def Set<string> set = createSetFrom("apple", "banana", "grape", "melon", "cherry");
def Set<Set<string>> partitionedSet = set.partition((item, index)->{
return item.length() == 5;
}); // partitionedSet = [["apple", "grape", "melon"],["banana", "cherry"]]
def Set<int> numbers = [1...10].toSet();
def Set<Set<int>> partitionedNumbers = partition(numbers, (item, index)->{
return item % 2 == 1;
}); // partitionedNumbers = [[1,3,5,7,9], [2,4,6,8,10]]
groupBy((List | Set) value, Lambda (ElementType element, (Optional) int position) => Type)
- Groups a
ListorSetaccording to the value returned by lambda
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def Map<int, List<string>> groupedList = list.groupBy((item, index)->{
return item.length();
}); // groupedList = 5 -> ["apple", "grape", "melon"], 6 -> ["banana", "cherry"]
def List<int> numbers = [1...10];
def Map<boolean, List<int>> groupedNumbers = groupBy(numbers, (item, index)->{
return item % 2 == 1;
}); // groupedNumbers = true -> [1,3,5,7,9], false -> [2,4,6,8,10]
def Set<string> set = createSetFrom("apple", "banana", "grape", "melon", "cherry");
def Map<int, Set<string>> groupedSet = set.groupBy((item, index)->{
return item.length();
}); // groupedSet = 5 -> ["apple", "grape", "melon"], 6 -> ["banana", "cherry"]
def Set<int> numbers = [1...10].toSet();
def Map<boolean, Set<int>> groupedNumbers = groupBy(numbers, (item, index)->{
return item % 2 == 1;
}); // groupedNumbers = true -> [1,3,5,7,9], false -> [2,4,6,8,10]
forEach((List | Set) value, Lambda (ElementType element, (Optional) int position) => void)
- This function is used to do something (given by lambda) for each element of the
ListorSet
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def List<string> fiveLetterFruits = [];
list.forEach((item, index)->{
if (item.length() == 5){
fiveLetterFruits.addElement(item);
}
});// fiveLetterFruits = ["apple", "grape", "melon"]
def List<string> fiveLetterFruits2 = [];
forEach(list, (item, index)->{
if (item.length() == 5){
fiveLetterFruits2.addElement(item);
}
});// fiveLetterFruits2 = ["apple", "grape", "melon"]
def Set<string> set = createSetFrom("apple", "banana", "grape", "melon", "cherry");
def Set<string> fiveLetterFruits = new Set<string>();
set.forEach((item, index)->{
if (item.length() == 5){
fiveLetterFruits.addElement(item);
}
});// fiveLetterFruits = ["apple", "grape", "melon"]
def Set<string> fiveLetterFruits2 = [];
forEach(set, (item, index)->{
if (item.length() == 5){
fiveLetterFruits2.addElement(item);
}
});// fiveLetterFruits2 = ["apple", "grape", "melon"]
filter((List | Set) value, Lambda (ElementType element, (Optional) int position) => boolean)
- This function returns a
ListorSetfor all the elements where the given lambda returns true
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def List<string> fiveLetterFruits = list.filter((item, index)->{
return item.length() == 5;
}); // fiveLetterFruits = ["apple", "grape", "melon"]
def List<int> numbers = [1...10];
def List<int> oddNumbers = filter(numbers, (item, index)->{
return item % 2 == 1;
}); // oddNumbers = [1,3,5,7,9]
def Set<string> set = createSetFrom("apple", "banana", "grape", "melon", "cherry");
def Set<string> fiveLetterFruits = set.filter((item, index)->{
return item.length() == 5;
}); // fiveLetterFruits = ["apple", "grape", "melon"]
def Set<int> numbers = [1...10].toSet();
def Set<int> oddNumbers = filter(numbers, (item, index)->{
return item % 2 == 1;
}); // oddNumbers = [1,3,5,7,9]
map((List | Set) value, Lambda (ElementType element, (Optional) int position) => Type)
- This function returns a new
ListorSetwith a different value (evaluated according to lambda) for each of its elements
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def List<int> sizeOfFruitsInList = list.map((item, index)->{
return item.length();
}); // sizeOfFruitsInList = [5,6,5,5,6]
def List<int> numbers = [1...5];
def List<int> squareOfNumbers = map(numbers, (item, index)->{
return item * item;
}); // squareOfNumbers = [1, 4, 9, 16, 25]
def Set<string> set = createSetFrom("apple", "banana", "grape", "melon", "cherry");
def Set<int> sizeOfFruitsInSet = set.map((item, index)->{
return item.length();
}); // sizeOfFruitsInSet = [5,6]
def Set<int> numbers = [1...5].toSet();
def Set<int> squareOfNumbers = map(numbers, (item, index)->{
return item * item;
}); // squareOfNumbers = [1, 4, 9, 16, 25]
flatMap((List<List> | Set<Set>) listValue, Lambda (ElementType element, (Optional) int position) => Type)
- This function first performs
mapand then flattens theListof (ListorSet) into aListor theSetof (ListorSet) into aSet
def List<List<string>> list = [["apple", "banana", "grape"], ["melon", "cherry"]];
def List<string> flatMappedList = list.flatMap((item, index)->{
return item.take(2);
}); // flatMappedList = ["apple","banana","melon","cherry"]
def List<List<int>> numbers = [[1...3], [4, 5]];
def List<int> flatMappedNumbers = flatMap(numbers, (item, index)->{
return item.reversed();
}); // flatMappedNumbers = [3,2,1,5,4]
def Set<Set<string>> set = [["apple", "banana", "grape"].toSet(), ["melon", "cherry"].toSet()].toSet();
def Set<string> flatMappedSet = set.flatMap((item, index)->{
return item.take(2);
}); // flatMappedSet = ["apple","banana","melon","cherry"]
def Set<Set<int>> numbers = [[1...3].toSet(), [4, 5].toSet()].toSet();
def Set<int> flatMappedNumbers = flatMap(numbers, (item, index)->{
return item.reversed();
}); // flatMappedNumbers = [3,2,1,5,4]
compactMap((List | Set) value, Lambda (ElementType element, (Optional) int position) => Type)
- This function first removes all the null values from the
ListorSetand then performsmapon it
def List<string> list = ["apple", null, "banana", "grape", null, "melon", "cherry"];
def List<int> sizeOfFruitsInList = list.compactMap((item, index)->{
return item.length();
}); // sizeOfFruitsInList = [5,6,5,5,6]
def List<int> numbers = [1, 2, 3, null, null, 4, null , 5];
def List<int> squareOfNumbers = compactMap(numbers, (item, index)->{
return item * item;
}); // squareOfNumbers = [1, 4, 9, 16, 25]
def Set<string> set = createSetFrom("apple", null, "banana", "grape", null, "melon", "cherry");
def Set<int> sizeOfFruitsInSet = set.compactMap((item, index)->{
return item.length();
}); // sizeOfFruitsInSet = [5,6,5,5,6]
def Set<int> numbers = createSetFrom(1, 2, 3, null, null, 4, null , 5);
def Set<int> squareOfNumbers = compactMap(numbers, (item, index)->{
return item * item;
}); // squareOfNumbers = [1, 4, 9, 16, 25]
reduce((List | Set) value, Lambda (ElementType element1, ElementType element2, (Optional) int position) => ElementType)
reducereduces aListorSetof values to a single value. See examples and explanations for details
def List<int> numbers = [1, 4, 3, 2];
def int reducedNumbers = numbers.reduce((accumulator, item, index)->{
return accumulator + item;
}); // reducedNumbers = 10
- In the above example,
- First Iteration => accumulator = 1 (First Item), item = 4 (Second Item). So the return value becomes accumulator + item = 5. This 5 becomes the next value for accumulator
- Second Iteration => accumulator = 5 (Value from previous iteration), item = 3 (Third Item). So the return value becomes accumulator + item = 8. This 8 becomes the next value for accumulator
- Third Iteration => accumulator = 8 (Value from previous iteration), item = 2 (Fourth Item). So the return value becomes accumulator + item = 10. This 10 becomes the next value for accumulator
- No more items left in the list. So the last value of accumulator (10) becomes the result
def List<int> numbers = [1, 4, 3, 2];
def int reducedNumbers = reduce(numbers, (accumulator, item, index)->{
return accumulator * item;
}); // reducedNumbers = 24
-
In the above example,
- First Iteration => accumulator = 1 (First Item), item = 4 (Second Item). So the return value becomes accumulator * item = 4. This 4 becomes the next value for accumulator
- Second Iteration => accumulator = 4 (Value from previous iteration), item = 3 (Third Item). So the return value becomes accumulator * item = 12. This 12 becomes the next value for accumulator
- Third Iteration => accumulator = 12 (Value from previous iteration), item = 2 (Fourth Item). So the return value becomes accumulator * item = 24. This 24 becomes the next value for accumulator
- No more items left in the list. So the last value of accumulator (24) becomes the result
-
Similar to the
Listexamples given above, we can have reduce forSettoo
def Set<int> numbers = createSetFrom(1, 4, 3, 2);
def int reducedNumbers = numbers.reduce((accumulator, item, index)->{
return accumulator + item;
}); // reducedNumbers = 10
def Set<int> numbers = createSetFrom(1, 4, 3, 2);
def int reducedNumbers = reduce(numbers, (accumulator, item, index)->{
return accumulator * item;
}); // reducedNumbers = 24
Make sure that the given list or set is not empty or else you'll get an error
fold((List | Set) value, Lambda (Type element1, ElementType element2, (Optional) int position) => Type, Type type)
foldis exactly likereduceexcept the initial value of accumulator is given. See examples and explanations for details
def List<int> numbers = [1, 4, 3, 2];
def int foldedNumbers = numbers.fold((accumulator, item, index)->{
return accumulator + item;
}, 5); // foldedNumbers = 15
- In the above example,
- First Iteration => accumulator = 5 (Initial value given), item = 1 (First Item). So the return value becomes accumulator + item = 6. This 6 becomes the next value for accumulator
- Second Iteration => accumulator = 6 (Value from previous iteration), item = 4 (Second Item). So the return value becomes accumulator + item = 10. This 10 becomes the next value for accumulator
- Third Iteration => accumulator = 10 (Value from previous iteration), item = 3 (Third Item). So the return value becomes accumulator + item = 13. This 13 becomes the next value for accumulator
- Fourth Iteration => accumulator = 13 (Value from previous iteration), item = 2 (Fourth Item). So the return value becomes accumulator + item = 15. This 15 becomes the next value for accumulator
- No more items left in the list. So the last value of accumulator (15) becomes the result
def List<int> numbers = [1, 4, 3, 2];
def int foldedNumbers = fold(numbers, (accumulator, item, index)->{
return accumulator * item;
}, 5); // foldedNumbers = 120
-
In the above example,
- First Iteration => accumulator = 5 (Initial value given), item = 1 (First Item). So the return value becomes accumulator * item = 5. This 5 becomes the next value for accumulator
- Second Iteration => accumulator = 5 (Value from previous iteration), item = 4 (Second Item). So the return value becomes accumulator * item = 20. This 20 becomes the next value for accumulator
- Third Iteration => accumulator = 20 (Value from previous iteration), item = 3 (Third Item). So the return value becomes accumulator * item = 60. This 60 becomes the next value for accumulator
- Fourth Iteration => accumulator = 60 (Value from previous iteration), item = 2 (Fourth Item). So the return value becomes accumulator * item = 120. This 120 becomes the next value for accumulator
- No more items left in the list. So the last value of accumulator (120) becomes the result
-
Similar to the
Listexamples given above, we can have fold forSettoo
def Set<int> numbers = createSetFrom(1, 4, 3, 2);
def int foldedNumbers = numbers.fold((accumulator, item, index)->{
return accumulator + item;
}, 5); // foldedNumbers = 15
def Set<int> numbers = createSetFrom(1, 4, 3, 2);
def int foldedNumbers = fold(numbers, (accumulator, item, index)->{
return accumulator * item;
}, 5); // foldedNumbers = 120
containsElement((List | Set) value, ElementType element)
- Returns true if the element is present in the given List or Set, otherwise returns false
- You can use the
notvariant of this function as detailed here
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def boolean value1 = list.containsElement("apple"); // true
def boolean value2 = list.containsElement("avocado"); // false
def boolean value3 = containsElement(list, "apple"); // true
def boolean value4 = containsElement(list, "avocado"); // false
def Set<string> set = createSetFrom("apple", "banana", "grape", "melon", "cherry");
def boolean value5 = set.containsElement("apple"); // true
def boolean value6 = set.containsElement("avocado"); // false
def boolean value7 = containsElement(set, "apple"); // true
def boolean value8 = containsElement(set, "avocado"); // false
containsAllElements((List | Set) lhs, (List | Set) rhs)
- Returns true if all the elements in rhs are present in lhs, otherwise returns false
- You can use the
notvariant of this function as detailed here
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def boolean value1 = list.containsAllElements(["apple", "banana"]); // true
def boolean value2 = containsAllElements(list, ["apple", "banana"]); // true
def boolean value2 = list.containsAllElements(["apple", "guava"]); // false
def boolean value4 = containsAllElements(list, ["apple", "guava"]); // false
def Set<string> set = createSetFrom("apple", "banana", "grape", "melon", "cherry");
def boolean value5 = set.containsAllElements(createSetFrom("apple", "banana")); // true
def boolean value6 = containsAllElements(set, createSetFrom("apple", "banana")); // true
def boolean value7 = set.containsAllElements(createSetFrom("apple", "guava")); // false
def boolean value8 = containsAllElements(set, createSetFrom("apple", "guava")); // false
containsAnyElement((List | Set) lhs, (List | Set) rhs)
- Returns true if any of the elements in rhs are present in lhs, otherwise returns false
- You can use the
notvariant of this function as detailed here
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def boolean value1 = list.containsAnyElement(["apple", "banana"]); // true
def boolean value2 = containsAnyElement(list, ["apple", "banana"]); // true
def boolean value2 = list.containsAnyElement(["strawberry", "guava"]); // false
def boolean value4 = containsAnyElement(list, ["strawberry", "guava"]); // false
def Set<string> set = createSetFrom("apple", "banana", "grape", "melon", "cherry");
def boolean value5 = set.containsAnyElement(createSetFrom("apple", "banana")); // true
def boolean value6 = containsAnyElement(set, createSetFrom("apple", "banana")); // true
def boolean value7 = set.containsAnyElement(createSetFrom("strawberry", "guava")); // false
def boolean value8 = containsAnyElement(set, createSetFrom("strawberry", "guava")); // false
randomElement((List | Set) item)
- Returns a randomElement from the given list or set
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def string randomElement1 = list.randomElement(); // ["melon"]
def string randomElement2 = randomElement(list); // ["banana"]
def Set<string> set = createSetFrom("apple", "banana", "grape", "melon", "cherry");
def string randomElement3 = set.randomElement(); // ["grape"]
def string randomElement4 = randomElement(set); // ["melon"]
retainAll((List | Set) item1, (List | Set) item2)
- Returns only the elements in item1 that are contained in item2
def List<string> list = ["apple", "banana", "grape", "melon", "cherry"];
def Set<string> set = createSetFrom("apple", "banana", "apple", "melon", "orange");
def List<string> retainedItems1 = list.retainAll(set); // ["apple", "banana", "melon"]
def List<string> retainedItems2 = retainAll(list, set); // ["apple", "banana", "melon"]
def Set<string> retainedItems3 = set.retainAll(list); // ["apple", "banana", "melon"]
def Set<string> retainedItems4 = retainAll(set, list); // ["apple", "banana", "melon"]