Git Product home page Git Product logo

kotlin-important-functions-on-collections-'s Introduction

Kotlin-important-functions-on-Collections-

  1. How to create Map from array or list or set [associate()]

     val array: Array<String> = arrayOf("Android", "Kotlin", "IOS","Swift")
     val list = array.asList()
     val set = array.toSet()
     
     val mapFromArray = array.associateBy {
         it
     }
    
     val mapFromList = list.associate {
         Pair(it, it.length)
     }
    
     val mapFromSet = set.associate {
         Pair(it, it.length)
     }
    

  1. How to get index of last element in array or list [lastIndex]

In Java, we get last index array.length -1 in case Array and list.size() - 1 in case of List but in Kotlin we get lastIndex property with array and list.

    val array: Array<String> = arrayOf("Android", "Kotlin", "IOS","Swift")
    val lastIndexInArray = array.lastIndex
    
    val list = array.asList()
    val lastIndexInList = list.lastIndex

  1. How to check wheather all elements in Collection are satisfying condition . [all()]

this function return true when all elements in collection fullfilling mentioned condition

    val personAgeList = listOf<Int>(23,21,27,30,36,19)
    val isAllPersonAboveEighteen = personAgeList.all {age ->
        age > 18
    }

  1. How to check wheather any element in Collection is following condition. [any()] this function check, if that collection contains any element that fullfill mentioned condition

    val personList = listOf<Int>(25,21,28,13,6,29)
    val isAnyTeenager = personList.any{age ->
        age in 13..19
    }
    

note: this function is different from any() function that tells Collection contains any element or not.

  1. chunked function

     val personList = listOf<Int>(25,21,28,13,6,29,33,34,56,45)
     val list = personList.chunked(5) //list will have two elements. first element will be list of first 5 elements in personList. Second element will be list of next 5 elements.
    

kotlin-important-functions-on-collections-'s People

Contributors

sagarkisanavhad avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.