Wednesday, May 20, 2020

Kotlin vs Java continued...

Collections are provided in the Kotlin standard library via extension functions.  

Collections can be retrieved with 

 - slice 

 - take and drop 

 - chunked and 

 - windowed

There are functions available to retrieve single elements from collections such as from lists and sets.

Lists are ordered collections where every element has a position.

Set is not an ordered collection but Kotlin set does leverage some order.

LinkedHashSet uses the order of insertion

SortedSet uses the natural sorting order

or some sets may have unknown order.

An element is retrieved from a collection by position with the help of elementAt with the starting element accessed at position 0 and the ending element accessed at position size-1

There are also useful aliases for retrieving the first and last element of the collection or for safe variations of elementsAt

These aliases support predicates so that the elements are qualified before being retrieved as first or last. They throw exception if no match occurs.

Random access of the elements are also supported with random() function.

The existence of one or more elements in a collection is done with contains() or containsAll()



No comments:

Post a Comment