Kotlin Learning Quick Start (4) - a collection of use

List, Set, Map is a collection

  • List is an ordered collection, by index (reflecting the position of the integral elements) to access elements. Element can appear multiple times in the list. One example of a word list: a set of words, the order is important words and the word may be repeated.
  • Set is a collection of unique elements. It reflects the collection (set) of mathematical abstraction: a set of non-duplicate objects. Generally set the order of elements is not important. For example, a set of alphabet letters (set).
  • Map (or dictionary) is a set of key-value pairs. The key is unique, each key exactly mapped to a value. Value can be repeated. map is useful for storing the logical connection between the objects, e.g., employee ID and employee position.

Create Collection

Generally use standard library to quickly create collections, listOf ()、setOf ()、mutableListOf ()、mutableSetOf ()

Of course, you can also use a set of Java constructor

val numbers = listOf(0,1,2,3,4)

Empty set: emptyList (), emptySet () and emptyMap (), you need to specify the type of

val empty = emptyList<String>()

Take a portion of the collection

slice

val numbers = listOf(0,1,2,3,4)
//按顺序取
println(numbers.slice(1..3))//[1,2,3]
//按步长取
println(numbers.slice(0..4 step 2))//[0,2,4]
//取指定下标
println(numbers.slice(setOf(3, 4, 0)))//[3,4,0]

n elements taken take

  • take (n: Int) sequentially from the array elements take n
  • takeLast (n: Int) at the end of n elements taken
  • takeWhile {expresstion} been taken, stop condition is not satisfied
  • takeWhileLast {expresstion} taken from the back, stop condition is not satisfied
val numbers = listOf(0,1,2,3,4)
println(numbers.take(3))//[0,1,2]
println(numbers.takeLast(3))//[2,3,4]
val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.takeWhile { !it.startsWith("three") })//[one,two]
println(numbers.takeLastWhile { !it.startsWith("three") })//[four, five, six]

drop

  • drop (n: Int) discards array of n elements
  • dropLast (n: Int) at the end of an array of n elements discards
  • dropWhile {expresstion} has been lost, stop condition is not satisfied
  • dropWhileLast {expresstion} lost from the back, stop condition is not satisfied
val numbers = listOf(0,1,2,3,4)
println(numbers.drop(3))//[3,4]
println(numbers.dropLast(3))//[0,1]
val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.dropWhile { !it.startsWith("three") })//three, four, five, six
println(numbers.dropLastWhile { !it.startsWith("three") })//[one, two, three]

filter

  • elements having filter {experssion} satisfies conditions
  • filterNot {experssion} taking condition is not satisfied element
  • filterIndexed {experssion} If the condition requires the use of index values, then the use of this method
  • filterIsInstance <type> () whether the element is a type
val numbers = listOf("one", "two", "three", "four", "five", "six")
val longerThan3 = numbers.filter { it.length > 3 }
println(longerThan3)//[three,four,five]
val numbers = listOf("one", "two", "three", "four")
val filteredIdx = numbers.filterIndexed { index, s -> (index != 0) && (s.length < 5)  }
println(filteredIdx)//[two,four]
val numbers = listOf(null, 1, "two", 3.0, "four")
//这里只获得list中的String类型数据
println(numbers.filterIsInstance<String>())//[two,four]

partition group

val numbers = listOf(2,3,4,5,6)
//上一节类中说过的数据类自动解析,match是满足,rest则是不满足
val (match, rest) = numbers.partition { it%2==0}
println(match)
println(rest)

Inspection array meets conditions

  • any {expression} arbitrary array element satisfies a condition returns true
  • none {expression} is not a data element satisfies condition returns true
  • all {expression} all the elements in the array satisfies the condition, returns true
val numbers = listOf("one", "two", "three", "four")
println(numbers.any { it.endsWith("e") })
println(numbers.none { it.endsWith("a") })
println(numbers.all { it.endsWith("e") })

Take an element

  • [Index] list may also be used
  • get (index) usual method
  • elementAt (index) the new method
  • find {} to find a first element satisfies the condition
  • findLast {} to find the last element satisfies condition
  • random () to take a random

Polymerization operation

  • min () Minimum
  • max () maximum
  • average () average
  • sum () sums
  • count () Count

Sequence

  • sort () ascending
  • sortDescending () Descending

reference

Kotlin collection

Guess you like

Origin www.cnblogs.com/kexing/p/11264487.html
Recommended