![]() ![]() However, by casting the List to a MutableList, we are free to add new elements through its add method. By default, Kotlinâs List interface is read-only and doesnât allow us to add new elements to our list. Returns a set containing all elements that are contained by both of the list. In our above example, we create a new List and assign it to the list variable. Val resultC: Set = listOfItem2 union listOfItem1 Val resultB: Set = listOfItem1 union listOfItem2 Val resultA:List = (listOfItem1 + listOfItem2).distinct() Build an app that displays a scrollable list of affirmations with text and. Returns a set containing all distinct elements from both of the list. Gain a high-level introduction to more Kotlin concepts such as generics. They are just small example I will highly recommend for looking up in docs for detailed explanation!, Now then â¡ï¸ on the code â¡â¡ Union : I have added `List` operations that you would perform to get the similar results for comparison. Union, Intersect and Subtract are function present in Kotlin's standard collection api, they all return Set type and preserve the element iteration order of the first array, and since they all are infix they don't need (, ). TLDR : Snippet says it all â¡ Disclaimer : All the assets used as background belongs to respective companies I don't promote them as my own, they are just used to make code relatable and attractive. ![]() Small snippets on how you can perform union, intersection & subtraction on list! Featured on: 5.2 Creating Collections Problem You want to generate a list, set, or map.
0 Comments
Leave a Reply. |