How can I use Groovy to add an item to the beginning of a list?
Groovy - add() Append the new value to the end of this List. This method has 2 different variants. boolean add(Object value) − Append the new value to the end of this List.
1 we can use the methods take() and drop() . With the take() method we get items from the beginning of the List. We pass the number of items we want as an argument to the method. To remove items from the beginning of the List we can use the drop() method.
Combine lists using the plus operator The plus operator will return a new list containing all the elements of the two lists while and the addAll method appends the elements of the second list to the end of the first one. Obviously, the output is the same as the one using addAll method.
list.add(0, myObject);
You can also read this for some other valuable examples: http://groovy.codehaus.org/JN1015-Collections
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With