I have seen many interesting topics about Kotlin on portal.kotlin-academy.com/#/ recently. Personally, I think it is very suitable for Kotlin lovers, interested partners can consult by themselves.
The interesting Kotlin series records their understanding of each question.
0x06: List minus List
fun main(args: Array<String>) {
val list = listOf(1, 2, 3)
print(list - 1)
print(list - listOf(1))
val ones = listOf(1, 1, 1)
print(ones - 1)
print(ones - listOf(1))
}
Copy the code
What is the result of the above code? Optional:
- [2, 3][2, 3][1, 1]
- [2, 3] [2, 3] [] [1, 1]
- [2, 3] [2, 3] [] [1, 1]
- [2, 3] [2, 3]
Think about it and write down the answer in your mind.
Analysis of the
Those familiar with Kotlin’s set-related operator logic should be able to quickly get the answer, which involves only – one operator and two overloaded implementations.
The first overload implementation:
The logic is to filter out the first element in the collection that is equal to the element passed in.
The second overload implementation:
The logic is to filter out all elements equal to any element in the second list.
The code is equivalent to the following:
fun main(args: Array<String>) {
val list = listOf(1, 2, 3)
print(list.minus(1))
print(list.minus(listOf(1)))
val ones = listOf(1, 1, 1)
print(ones.minus(1))
print(ones.minus(listOf(1)))
}
Copy the code
Look at the logic one by one
Val list = listOf(1, 2, 3) print(listOf(1)) print(listOf(1)) print(listOf(1)) Val ones = listOf(1, 1, 1) print(ones-1) Print (ones-listof (1)) print(ones-listof (1))Copy the code
So the correct answer is
[2, 3][1, 1][]
extension
The operator keyword is used in Kotlin to modify functions to indicate that the function overloads an operator or implements a convention. Use the operator keyword to modify the function and the function name can only be COMPONent1, Component2, component3… Time is to implement a contract, namely deconstruction.
Take a look at some of the List’s other operators (in, +) and destruct conventions.
fun main() {
val list = listOf(1, 2, 3)
println(2 in list)
println(4 in list)
println(list + 5)
println(list + listOf(1, 2, 3))
println(list + arrayOf(1, 2, 3))
println(list + sequenceOf(1, 2, 3))
val (v, w, x) = list
println(v)
println(w)
println(x)
println(list.component1())
println(list.component2())
println(list.component3())
println(list.component4())
println(list.component5())
}
Copy the code
The running results are as follows:
ComponentN supports up to 5 componentN support up to 5 componentN support up to 5 componentN support up to 5 componentN support up to 5 componentN support up to 5 componentN support up to 5
For operator and componentN with the way, for example:
class Location(val x: Int, val y: Int) {
operator fun component1() = x
operator fun component2() = y
}
fun main() {
val location = Location(520, 1314)
val (x, y) = location
println(x)
println(y)
}
Copy the code
Running results:
For operator overloading, here’s an example:
operator fun Location.minus(location: Location): Location { return Location(this.x - location.x, this.y - location.y) } operator fun Location.plus(location: Location): Location { return Location(this.x + location.x, this.y + location.y) } operator fun Location.contains(location: Location): Boolean { return this.x > location.x && this.y > location.y } class Location(val x: Int, val y: Int) { operator fun component1() = x operator fun component2() = y override operator fun equals(other: Any?) : Boolean = other is Location && this.x == other.x && this.y == other.y override fun hashCode(): Int { var result = x result = 31 * result + y return result } override fun toString(): String { return "($x, $y)" } } fun main() { val location = Location(520, 1314) val (x, y) = location println(x) println(y) val other = Location(1, 2) println(location - other) println(location + other) println(location == other) println(other in location) }Copy the code
The running results are as follows:
Note that the equals operator overload can only be implemented as a class member function, since this function is defined in the Any class.
Android Studio gives us friendly hints as we write, true to YYDS.
conclusion
- Operator overloading
- deconstruction