![]() ![]() * true if any element in is found in the receiving collection.įun Collection. Lets start with one of the most common kinds of collections in Kotlin - a list. * elements the elements to look for in the receiving collection. Thankfully, theres a much better way Libby can create a collection. Calling people.any() is equivalent to people.size > 0 or people.isNotEmpty(). ![]() ![]() * Returns true if the receiving collection contains any of the specified elements. The any() method can be used to check if a List contains at least one element. Here's an example implementation of an extension function defined on the Collection class, the details of which are inspired by other answers in this thread: /** Let's consider an example: // Can be any list since its element type is only bounded by Any val anyList: List<> listOf(1, 2, 3) // Can be any object.The implementation detail is tucked away inside of that function and can be changed in a single place in future should you wish to change the implementation. All of the places where you need to check whether an Array or Collection contains any of the elements of another then remains nice and neat, as follows: arrayOf(1, 2, 3).containsAny(2, 3) For me, the best way to solve this is to define a containsAny(elements:) extension function on the Array and/or Collection class. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |