/* https://sapintegrationsuitecourse.com */ //Empty List def emptyList = [] //define an empty list assert emptyList.size() == 0 //empty list size is 0 assert emptyList.getClass() == java.util.ArrayList //list class is java.util.ArrayList //Access list by index, range def num_list = [50, 80, 99, 20] //define a list with 3 number assert num_list.size() == 4 assert num_list[1] == 80 //access the index 1 (2nd position) assert num_list[1,3] == [80, 20] //access the index 1 and 3 assert num_list[1..3] == [80, 99, 20] //access the range from index 1 to 3 //List can mix different kind of object def mixed_list = [10, 20, "aaa", "bbb", 30, "ccc", true, ["X", "Y"], null, []] assert mixed_list.size() == 10 assert mixed_list[1,2] == [20, "aaa"] assert mixed_list[6,7] == [true, ["X", "Y"]] assert mixed_list[7][1] == "Y" assert mixed_list[8] == null assert mixed_list[9] == [] //Add, Remove item from list def m_list = [] m_list = m_list + "a" //Add 'a' assert m_list == ["a"] m_list.add("b") //Add 'b' assert m_list == ["a", "b"] m_list += ["c", "d"] //Add 'c' and 'd' assert m_list == ["a", "b", "c", "d"] m_list << "e" << "f" //Add 'e' and 'f' assert m_list == ["a", "b", "c", "d", "e", "f"] m_list = m_list - ["a", "f"] //Remove 'a' and 'f' assert m_list == ["b", "c", "d", "e"] //Pop and Push item (Stack behavior) def n_list = ["Apple", "Orange", "Keyboard"] def n_value = n_list.pop() //Pop item to remove last item from list assert n_value == "Keyboard" assert n_list == ["Apple", "Orange"] n_list.push("Mango") //Push to add item to end of the list assert n_list == ["Apple", "Orange", "Mango"] //First, Last, Head, Tail def o_list = ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"] assert o_list.first() == "The" //Get the first item assert o_list.last() == "dog" //Get the last item assert o_list.head() == "The" //Take the head (first item) assert o_list.tail() == ["quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"] //Chop the head (first item), take the tail (all remaining items) //Take, TakeRight, Drop, DropRight assert o_list.take(3) == ["The", "quick", "brown"] //Take the first 3 items assert o_list.takeRight(3) == ["the", "lazy", "dog"] //Take right-most/last 3 items assert o_list.drop(3) == ["fox", "jumps", "over", "the", "lazy", "dog"] //Drop the first 3 items assert o_list.dropRight(3) == ["The", "quick", "brown", "fox", "jumps", "over"] //Drop the right-most/last 3 items //Contains, ContainsAll assert o_list.contains("dog") == true //check if list contains 'dog' assert o_list.containsAll("dog", "fox") == true //check if list contains 'dog' AND 'fox' assert o_list.containsAll("dog", "snake") == false //check if list contains 'dog' AND 'snake' //define a list with 7 strings def q_list = ["Life", "is", "like", "a", "box", "of", "chocolates"] //Loop or iterate each item q_list.each { println it.toString() } println '-------------------------------------' q_list.each {item -> println item.toString() } println '-------------------------------------' q_list.eachWithIndex{ item, idx -> println "$idx | $item" } println '-------------------------------------' //Find, FindAll def q_found_list = q_list.findAll {this_item -> this_item.length() <= 3 && this_item.contains("o") //Find all short word that length up to 3 AND contains 'o' } assert q_found_list == ["box", "of"] q_found_list = q_list.find {this_item -> this_item.length() <= 3 && this_item.contains("o") //Find first short word that length up to 3 AND contains 'o' } assert q_found_list == "box" //Join assert q_list.join("-") == "Life-is-like-a-box-of-chocolates" //Create unique list def p_list = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] assert p_list.unique(false) == [1, 2, 3, 4] //Return unique list but not modify original list assert p_list == [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] //original list still unmodified assert p_list.unique() == [1, 2, 3, 4] //Return unique list and original list modified become unique assert p_list == [1, 2, 3, 4] //original list modified become unique //Alternative way, add item into unique list def unique_list = [] p_list.each{item -> if(!unique_list.contains(item)){ unique_list.add(item) } } assert unique_list == [1, 2, 3, 4] //unique_list