Saturday, May 23, 2020

Kotlin vs Java continued

Collections are provided in the Kotlin standard library via extension functions.  For example, groupBy takes a lambda function and returns a Map where each key is the lambda result and the corresponding value is the List of elements which represent the groups 

The groupBy can also be called with a second parameter 'valueTransform' where the keys from the first parameter 'keySelector' is mapped to the result of the transformation rather than the original elements.  

groupBy is different from groupingBy which returns an instance of 'Grouping' type. Here the operations are all applied in a lazy manner. Even the groups are formed just before the operation execution and supports the following operations: 

eachCount() which counts the elements in each group 

fold() and reduce() which operates on each group and returns results 

aggregate() which supports custom operations when fold and reduce are not enough     

Collections can be retrieved with 

 - slice 

 - take and drop 

 - chunked and 

 - windowed. 

slice() returns a list of collection when a set of indices are passed in. The positions of the elements to be retrieved can also be passed in via range descriptors. This is equivalent to subList 

  

Take and drop  are functions that return the elements themselves. 

  

take() and takeLast() work on the two ends of the list. If there are more number of elements specified than the current number, then they return the entire collection. 

  

drop() returns elements not matching the given predicate. 

  

chunked breaks the collection into parts of a given size, all parts are of equal size unless the remaining are smaller than the chunk size. chunks are short living lists that should be consumed right in that lambda. 

  

Windowed retrieves all possible ranges of the collection elements of a given size starting from each collection element. 

 

Lists order elements by position. Two lists cannot be considered similar if their elements are not in the same order. 

Order can be ‘natural’ if the elements in the collection implement the Comparable interface. The Comparable interface has a method called compareTo which has a major version and a minor version. This is the default order when none other is specified. 

Custom orders helps sort instances in any arbitrary way. Even if the objects cannot be compared, it is possible to override the compare method of the Comparator interface in this case. For example, 

companion object: Comparator<ReaderConfiguration> { 

         override fun compare(a: ReaderConfiguration, b: ReaderConfiguration): Int { 

                        if (a.numReaders == b.numReaders && 

                            a.numRetries == b.numRetries && 

                            a.retryMillis == b.retryMillis &&  

                            a.segment == b.segment && 

                            a.position == b.position ) return 0 

                        return -1 

        } 

    } 
Then there is reverse order by running the extension function reversed() on the collection. This creates a new collection. If we want the same collection reversed, the asReversed() method comes helpful. 

 

The random order of elements in a collection can be seen with the shuffled method.  

Aggregate function operations return a single vslue based on the collection content. For example, sum(), count(), average(), min(), and max() are widely used aggregate functions  

 

The Comparator described above can be used with aggregate functions to return the largest or the smallest functions 

The summation functions also take a function as a parameter.  

No comments:

Post a Comment