Friday, May 22, 2020

Kotlin vs Java continued

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