当前位置:网站首页>Kotlin set operation summary
Kotlin set operation summary
2022-07-04 09:46:00 【There is a beauty like jade in the book】
1 aggregate API Summary
Iterable: Any implementation of this interface , Can traverse elements
MutableIterable: Inherit Iterable Interface . And provide us with the method of removal
Collection: A generic set of elements , This set is a read-only set , It provides us with size, Is it empty , Whether to include a certain method or a group of data .
MutableCollection: Inherit Collection,MutableIterable. And provides additional functions add,addAll,remove,clear Other methods
List: Inherit Collection. A normatively ordered read-only set . Because of order , therefore , We can use get(position) And so on
MutableList: Inherit List,MutableCollection. An orderly collection . And provide additional add/remove Element method
Set: Inherit Collection. An unordered set does not support repeated elements
MutableSet: Inherit Set,MutableCollection, An unordered collection and does not support repeating elements . however , Support add/remove Elements
Map: One key-value Read only collection of . also key only .
MutableMap: Inherit Map. Support put/remove Elements
2 Set operators ( Alphabetical order )
any
If at least one element matches the judgment condition , be return true
val list = listOf(1,2,3)
assertTrue { list.any{it >2} }
all
If all elements are consistent with the judgment conditions , be return true
val list = listOf(1,2,3)
assertTrue { list.all{it < 4} }
associate
Pass the specified conditions , hold list convert to map
val list = listOf(1, 2)
assertEquals(mutableMapOf(Pair("a1",1),Pair("a2",2)), list.associate({ it -> Pair("a"+it,it)}))
associateBy
Pass the specified conditions , hold list convert to map.2 Kind of , The first conversion map Of key; second map Of key-value All conversion
val list = listOf(1, 4, 2, 2)
assertEquals(hashMapOf("key1" to 1, "key4" to 4, "key2" to 2), list.associateBy { it -> "key" + it })
assertEquals(hashMapOf("key1" to "value1", "key4" to "value4", "key2" to "value2"), list.associateBy({ it -> "key" + it }, { it -> "value" + it }))
average
Find the average of the set ( The sum of elements / Element size ). Limited to (Byte,Short,Int,Long,Float,Double)
val list = listOf(1, 4, 2, 2)
assertEquals(2.25,list.average())
component1,…,component5
Returns the... Of the set n Elements , To return across the border ArrayIndexOutOfBoundsException
val list = listOf(1, 4, 2, 2)
assertEquals(1,list.component1())
assertEquals(4,list.component2())
contain
If the specified element can be found in the collection , be return true
val list = listOf(4,1,2,3,4)
assertTrue(list.contains(3))
containsAll
If you specify a set, all elements can be found in the target set , be return true
val list = listOf(4,1,2,3,4)
val subList = listOf(2,1)
assertTrue(list.containsAll(subList))
count
Return the number of elements that match the judgment condition
val list = listOf(1,2,3)
assertEquals(2,list.count { it>1 })
distinct
Returns an array containing only different elements
val list = listOf(1, 4, 2, 2)
assertEquals(listOf(1,4,2),list.distinct())
distinctBy
Returns the collection element after executing the specified condition , An array of different elements ( Original array elements )
val list = listOf(1, 4, 2, 2)
assertEquals(listOf(1,4),list.distinctBy { it%2 == 0})
drop
Return a list of all elements , But excluding the former n Elements
val list = listOf(1,2,3,4)
assertEquals(listOf(3,4),list.drop(2))
dropLast
Return a list of all elements , But not including the last n Elements
val list = listOf(1,2,3,4)
assertEquals(listOf(1,2),list.dropLast(2))
dropWhile
Return a list of all elements , But it does not contain elements that meet the judgment conditions
val list = listOf(4,1,2,3,4)
assertEquals(listOf(4,1,2,3,4),list.dropWhile{it <3})
val list = listOf(1,2,3,4)
assertEquals(listOf(3,4),list.dropWhile{it <3})
dropLastWhile
Return a list of all elements , But it does not contain elements that meet the conditions from the end of the list
//2 dissatisfaction
val list = listOf(1,2,3,4,2)
assertEquals(listOf(1,2,3,4,2),list.dropLastWhile{it >3})
//4,5 Satisfy
val list = listOf(1,2,3,4,5)
assertEquals(listOf(1,2,3),list.dropLastWhile{it >3})
elementAt
Returns the element of the specified index , If the index is out of bounds , Throw out ArrayIndexOutOfBoundsException
val list = listOf(1,2,3,4)
assertEquals(4,list.elementAt(3))
elementAtOrElse
Returns the element of the specified index , If the index is out of bounds , Returns the specified default value
val list = listOf(1,2,3,4)
assertEquals(18,list.elementAtOrElse(6,{it *3}))
elementAtOrElse
Returns the element of the specified index , If the index is out of bounds , Then return to null
val list = listOf(1,2,3,4)
assertEquals(null,list.elementAtOrNull(6))
filter
Filter out all qualified elements
val list = listOf(1,2,3,4)
assertEquals(listOf(2,3),list.filter{ it in 2..3 })
filterIndexed
Filter out all qualified elements ( The condition has one more index parameter )
val list = listOf(1, 4, 2, 2)
assertEquals(listOf(4),list.filterIndexed { index, it -> index>0 && it >2} )
filterNot
Filter out all unqualified elements
val list = listOf(1,2,3,4)
assertEquals(listOf(1,4),list.filterNot{ it in 2..3 })
filterNotNull
Filter out all that are not null The elements of
val list = listOf(1,2,3,null,4)
assertEquals(listOf(1,2,3,4),list.filterNotNull())
first
Returns the first element that satisfies the condition , If not, throw NoSuchElementException
val list = listOf(1,2,3,4)
assertEquals(2,list.first { it > 1 })
firstOrNull
Returns the first element that satisfies the condition , No, , be return Null
val list = listOf(1, 2, 3, 4)
assertEquals(null, list.firstOrNull { it > 5 })
find
Same as firstOrNull. Returns the first element that satisfies the condition , No, , be return Null
val list = listOf(1,2,3,4)
assertEquals(2,list.find { it > 1 })
findLast
Returns the last element that satisfies the condition , No, , be return Null
val list = listOf(1,2,3,4)
assertEquals(4,list.findLast { it > 1 })
flatMap
Go through all the elements , Create a collection for each element , Finally, put all the sets in one set .
val list = listOf(1, 2)
assertEquals(listOf(1,2,2,4),list.flatMap { it -> listOf(it,it*2) })
flatten
Traverse a single set , Contains all elements in a given nested set .
val list = listOf(listOf(1,2), listOf(4,2), listOf(3), listOf(4))
assertEquals(listOf(1,2,4,2,3,4),list.flatten())
fold
Will operate on the collection from the first to the last element
// Here is the multiplication operation
val list = listOf(1, 2, 3, 4)
assertEquals(48, list.fold(2) { total, next -> total * next })
foldRight
Follow fold Same operation , It's just to operate from the last to the next element
// Here is the multiplication operation
val list = listOf(1, 2, 3, 4)
assertEquals(48, list.foldRight(2) { total, next -> total * next })
get
Get the element where the index is located , No return ArrayIndexOutOfBoundsException
val list = listOf(1, 2, 4, 2, 3, 4)
assertEquals(4, list.get(2))
getOrElse
Get the element where the index is located , If not, return the default value
val list = listOf(1, 2, 4, 2, 3, 4)
assertEquals(10, list.getOrElse(8, { _ -> 10 }))
assertEquals(2, list.getOrElse(1, { _ -> 10 }))
getOrNull
Get the element where the index is located , Return on no nul
val list = listOf(1, 2, 4, 2, 3, 4)
assertEquals(null, list.getOrNull(8))
assertEquals(4, list.getOrNull(2))
groupBy
Returns a... Grouped by a given function map
val list = listOf(1, 2, 2, 4)
assertEquals(mapOf("error" to listOf(1), "right" to listOf(2, 2, 4)), list.groupBy { if (it % 2 == 0) "right" else "error" })
indexOf
Returns the first index position of the specified element , There is no return -
val list = listOf(1, 2, 2, 4)
assertEquals(1,list.indexOf(2))
indexOfFirst
Returns the index of the first element that meets the specified criteria , There is no return -1
val list = listOf(1, 2, 2, 4)
assertEquals(1, list.indexOfFirst { it % 2 == 0 })
indexOfLast
Returns the last index position that meets the specified conditions , There is no return -1
val list = listOf(1, 2, 2, 4)
assertEquals(3, list.indexOfLast { it % 2 == 0 })
last
Returns the last element that meets the conditions of the given function , If it doesn't exist, throw NoSuchElementException
val list = listOf(1, 2, 2, 4)
assertEquals(4, list.last { it % 2 == 0 })
lastIndexOf
Returns the first index position of the specified element , There is no return -1
val list = listOf(1, 2, 2, 4)
assertEquals(2, list.lastIndexOf(2) )
lastOrNull
Returns the last element that meets the conditions of the given function , There is no return null
val list = listOf(1, 2, 2, 4)
assertNull( list.lastOrNull{ it >5})
map
Returns an array in which each element is converted according to a given function condition
val list = listOf(1, 2, 2, 4)
assertEquals(listOf(2, 4, 4, 8), list.map{ it*2} )
mapIndexed
Same function map, Than map One more index
val list = listOf(1, 2, 2, 4)
assertEquals(listOf(0, 2, 4, 4), list.mapIndexed { index, it -> if (index % 2 == 0) index * it else it })
mapNotNull
Same as map. however , The element transformation does not contain Null
val list = listOf(1, 2,null, 2, 4)
assertEquals(listOf(2, 4, 4, 8), list.mapNotNull { it?.times(2) })
max
Returns the largest element of the set . There is no return null
val list = listOf(1, 2, 2, 4)
assertEquals(4, list.max())
val list = emptyList<Int>()
assertEquals(null, list.max())
maxBy
Returns the conversion according to the specified function , The original element of the maximum value generated ( What is returned is the original element ). If there is no element , Then return to null.
val list = listOf(1, 2, 2, 4)
assertEquals(1, list.maxBy { -it })
min
Returns the smallest element of the set , There is no return null
val list = listOf(1, 2, 2, 4)
assertEquals(1, list.min())
minBy
Returns the conversion according to the specified function , The original element of the minimum value generated ( What is returned is the original element ). If there is no element , Then return to null.
val list = listOf(1, 2, 2, 4)
assertEquals(4, list.minBy { -it })
none
If no element matches the specified function condition , Then return to true.
val list = mutableListOf(1, 2, 2, 4)
assertTrue(list.none { it > 4 })
orEmpty
If no element matches the specified function condition , Then return to true.
val list = mutableListOf(1, 2, 2, 4)
assertTrue(list.none { it > 4 })
partition
Divide a specified set into 2 individual . The first set is all that meet the conditions of the specified function , The second set is all sets that do not meet the specified conditions
val list = mutableListOf(1, 2, 2, 4)
assertEquals(Pair(listOf(2, 2, 4), listOf(1)), list.partition { it % 2 == 0 })
plus
Returns a collection containing the original collection and all elements in a given collection . You can also use + The operator
val list = listOf(1, 2, 2, 4)
val listTwo = listOf(5, 6)
assertEquals(listOf(1, 2, 2, 4, 5, 6), porkbun.com | parked domain(listTwo))
assertEquals(listOf(1, 2, 2, 4, 5, 6), list + listTwo)
reduce
And fold Function as . however , There is no initial value . Set the set from the first to the last , Operate according to the specified conditions
val list = listOf(1, 2, 2, 4)
assertEquals(-7, list.reduce { total, next -> total -next })
reduceRight
And reduce equally . however , The order is from the last to the first , Operate according to the specified conditions
val list = listOf(1, 2, 2, 4)
assertEquals(-1, list.reduceRight { next, total -> total - next })
reverse
Arrange the set in reverse order
val list = listOf(1, 2, 2, 4)
assertEquals(listOf(4,2,2,1), list.reversed())
single
Returns a single element that meets the conditions of the specified function , If there is no match or more than one , Throw an exception .
val list = listOf(1, 2, 2, 4)
assertEquals(4, list.single { it == 4 })
singleOrNull
Returns a single element that meets the conditions of the specified function , If there is no match or more than one , Then return to null
val list = listOf(1, 2, 2, 4)
assertEquals(null, list.singleOrNull { it == 2 })
sorted
Return the sorted list of all elements .
val list = listOf(1, 4, 2, 2)
assertEquals(listOf(1, 2, 2, 4), list.sorted())
sortBy
Return the sorted list of all elements . Order according to the specified function conditions
val list = listOf(1, 4, 2, 2)
assertEquals(listOf(4,2,2,1), list.sortedBy { -it })
sortDescending
Return the sorted list of all elements . In descending order
val list = listOf(1, 4, 2, 2)
assertEquals(listOf(4,2,2,1), list.sortedDescending())
sortedByDescending
Return the sorted list of all elements . Order in descending order of the specified function conditions
val list = listOf(1, 4, 2, 2)
assertEquals(listOf(1,2,2,4), list.sortedByDescending{ -it })
sum
Returns the sum of the element values in the set .
val list = listOf(1, 4, 2, 2)
assertEquals(9, list.sum())
sumBy
Returns the sum of the values generated by the elements in the set after conversion according to the specified function conditions .
val list = listOf(1, 4, 2, 2)
assertEquals(-9, list.sumBy{ -it })
slice
Return to one list It is specified in index The elements of .
val list = listOf(1, 4, 2, 2)
assertEquals(listOf(4,2,2), list.slice(1..3))
assertEquals(listOf(1,4), list.slice(listOf(0,1)))
take
Returns the... Starting with the first element n Elements .
val list = listOf(1, 4, 2, 2)
assertEquals(listOf(1,4), list.take(2))
takeLast
Returns the from the last element n Elements
val list = listOf(1, 4, 2, 2)
assertEquals(listOf(2,2), list.takeLast(2))
takeWhile
Returns the last element that conforms to the specified function bar Element of piece .( Encounter unqualified , Don't go down )
val list = listOf(1, 4, 2, 2)
assertEquals(listOf(2, 2), list.takeLastWhile { it < 3 })
zip
Return a list , The list consists of element pairs established by the same index elements in two sets . The length of this list is the length of the shortest set .
val list = listOf(1, 4, 2, 2)
assertEquals(listOf(Pair(1,10),Pair(4,20),Pair(2,30)), list.zip(listOf(10,20,30)))
边栏推荐
- Fabric of kubernetes CNI plug-in
- Regular expression (I)
- Development trend and market demand analysis report of high purity tin chloride in the world and China Ⓔ 2022 ~ 2027
- 华为联机对战如何提升玩家匹配成功几率
- 2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
- 2022-2028 research and trend analysis report on the global edible essence industry
- Daughter love in lunch box
- C # use ffmpeg for audio transcoding
- MATLAB小技巧(25)竞争神经网络与SOM神经网络
- Get the source code in the mask with the help of shims
猜你喜欢
How web pages interact with applets
xxl-job惊艳的设计,怎能叫人不爱
2022-2028 global special starch industry research and trend analysis report
Hands on deep learning (33) -- style transfer
Summary of reasons for web side automation test failure
C # use gdi+ to add text to the picture and make the text adaptive to the rectangular area
Normal vector point cloud rotation
C language pointer interview question - the second bullet
百度研发三面惨遭滑铁卢:面试官一套组合拳让我当场懵逼
MATLAB小技巧(25)竞争神经网络与SOM神经网络
随机推荐
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
【leetcode】540. A single element in an ordered array
【leetcode】29. Divide two numbers
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache
Lauchpad x | MODE
Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310
查看CSDN个人资源下载明细
Global and Chinese market of air fryer 2022-2028: Research Report on technology, participants, trends, market size and share
Pueue data migration from '0.4.0' to '0.5.0' versions
If you can quickly generate a dictionary from two lists
C # use gdi+ to add text with center rotation (arbitrary angle)
Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
Solution to null JSON after serialization in golang
Global and Chinese PCB function test scale analysis and development prospect planning report Ⓑ 2022 ~ 2027
C # use smtpclient The sendasync method fails to send mail, and always returns canceled
C语言指针面试题——第二弹
Golang Modules
Deadlock in channel
Basic data types in golang
2022-2028 research and trend analysis report on the global edible essence industry