class Person(val age: Int, val name: String){
override fun equals(other: Any?): Boolean { val other = (other as? Person)?: return false return other.age == age && other.name == name } override fun hashCode(): Int { return 1 + 7 * age + 13 * name.hashCode() }
}
And java Similarly, if it is added to a data structure such as hash, it must be rewritten equals and hascode Method, if equals If the method judgment is the same, it is considered to be the same object.
val persons = HashSet()
(0..5).forEach { persons += Person(20, "Benny") } println(persons.size) //Print out 1
**lambda expression** * kotlin Inside lambdas An expression is a**Anonymous function**So its type is actually the corresponding function type * java Medium lambdas The expression is actually**Interface type**A grammar of sugar, the two are different
//The function type is () - > unit
val func: () -> Unit = fun() { println("Hello") //The last line is the return value of the function } val lambda: () -> Unit = { println("Hello") }
//The function type is (Int) - > string val f1 = { p: Int -> println(p) "Hello" //The last line is the return value of the function } println(f1(1)) ``` kotlin in lambda In expression**Last line**The type of is the return type of the function **Infix expression** ![Insert picture description here](https://img-blog.csdnimg.cn/20201214213844664.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2x5YWJjMTIzNDU2,size_16,color_FFFFFF,t_70#pic_center) kotlin A strange way of writing in ``` val map = mutableMapOf( "Hello" to 2, "World" to 3 ) 2 to 3 2.to(3)
println("HelloWorld" rotate 5)
infix fun String.rotate(count: Int): String {
val index = count % length return this.substring(index) + this.substring(0, index)
}
The essence is the extension method of the class, preceded by `infix` Keywords may be used to achieve a more semantic way of writing
class Book
class Desk infix fun Book.on(desk: Desk){ } val book = Book() val desk = Desk() book on desk
**Higher order function** A higher-order function is simply a function whose parameters can be transferred to another function, which is common in`forEach`expression:
val intArray = IntArray(5){ it + 1 }
intArray.forEach { println(it) } intArray.forEach(::println) intArray.forEach { println("Hello $it") }
Example: time consuming to define a function printing method
fun cost(block: () -> Unit) {
val start = System.currentTimeMillis() block() println("${System.currentTimeMillis() - start}ms")
}
//The return value is a lambda expression and a high-order function
fun fibonacci(): () -> Long {
var first = 0L var second = 1L return { val next = first + second val current = first first = second second = next current }
}
Call:
cost {
val fibonacciNext = fibonacci() for (i in 0..10) { println(fibonacciNext()) } }
Obviously java You can't do this in. You must print before and after each method.. **Inline function** add to`inline`Keyword functions are marked as inline functions. The inline function is characterized by that the code will be directly inserted into the calling function. The decompilation result of the compiled code is the effect of directly inserting the code into the calling function. Inline functions are more efficient.
val intArray = IntArray(5){ it + 1 }
intArray.forEach { println(it) }
forEach stay kotlin The implementation in is an inline function:
public inline fun IntArray.forEach(action: (Int) -> Unit): Unit {
for (element in this) action(element)
}
Higher order functions can be used more efficiently with inline functions:
inline fun cost(block: () -> Unit) {
val start = System.currentTimeMillis() block() println(System.currentTimeMillis() - start) } cost { println("Hello") }
After compilation, it is equivalent to:
val start = System.currentTimeMillis()
println("Hello") println(System.currentTimeMillis() - start)
Return of inline function:
val ints = intArrayOf(1, 2, 3, 4)
ints.forEach { if(it == 3) return@forEach println("Hello $it") }
In fact, skipping 3 is not a return, which is equivalent to:
for (element in ints) {
if(element == 3) continue println("Hello $element") }
* nonLocalReturn Return the called method, which is directly returned below main method
inline fun nonLocalReturn(block: () -> Unit){
block()
}
fun main() {
nonLocalReturn { return }
}
* prohibit non-local-return,use`crossinline` keyword
public inline fun IntArray.forEach(crossinline action: (Int) -> Unit): Unit {
for (element in this) action(element)
}
about non-local-return In fact, it's still a little difficult to understand * Inline properties:
var pocket: Double = 0.0
var money: Double
inline get() = pocket inline set(value) { pocket = value }
![Insert picture description here](https://img-blog.csdnimg.cn/20201214221532580.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2x5YWJjMTIzNDU2,size_16,color_FFFFFF,t_70#pic_center) ![Insert picture description here](https://img-blog.csdnimg.cn/20201214221616169.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2x5YWJjMTIzNDU2,size_16,color_FFFFFF,t_70#pic_center)
fun main() {
val person = Person("benny", 20) person.let(::println) person.run(::println) val person2 = person.also { it.name = "hhh" //it is the current object } val person3 = person.apply { name = "xxx" //this is the current object } File("build.gradle").inputStream().reader().buffered().use { println(it.readLines()) }
}
The above methods still feel good `use` The method is more useful, IDE Click to open the source code to implement it:
@InlineOnly
@RequireKotlin("1.2", versionKind = RequireKotlinVersionKind.COMPILER_VERSION, message = "Requires newer compiler version to be inlined correctly.")
public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
Interview review route, sort out knowledge and improve reserves
How well you prepare your knowledge directly determines whether you can successfully pass one and two sides. Therefore, it is necessary to sort out your knowledge before the interview to see whether you need to improve your knowledge reserve.
With regard to knowledge sorting, I would like to share my review route during the interview: (I collected and sorted out the review materials of the following system from various leaders)
- Necessary skills for architects to build foundations
- Android advanced UI and FrameWork source code
- 360 ° performance tuning
- Interpretation of open source framework design ideas
- NDK module development
- Wechat applet
- Hybrid development and fluent
After sorting out the knowledge, it is necessary to check and fill in the gaps. Therefore, for these knowledge points, I have prepared a lot of e-books and notes on hand, which perfectly summarize each knowledge point:
"960 most complete Android Development Notes in the whole network"
379 pages of Android development interview
For half a year, we sorted out the most comprehensive analysis of Android interview questions on the market
It includes the questions asked in the interview of front-line Internet companies such as Tencent, Baidu, Xiaomi, Ali, LETV, meituan, 58, cheetah, 360, Sina and Sohu. Familiarity with the knowledge points listed in this article will greatly increase the probability of passing the first two rounds of technical interviews.
How to use it?
1. You can directly look through the required knowledge points through the directory index to find out omissions and fill vacancies.
2. The number of five pointed stars indicates the frequency of interview questions and represents the important recommendation index
507 page Android development related source code analysis
As long as programmers, whether Java or Android, don't read the source code and only look at the API documents, they will just stay superficial, which is unfavorable to the establishment and completion of our knowledge system and the improvement of practical technology.
1630934522040)]
After sorting out the knowledge, it is necessary to check and fill in the gaps. Therefore, for these knowledge points, I have prepared a lot of e-books and notes on hand, which perfectly summarize each knowledge point:
[external chain picture transferring... (img-GFBTm668-1630934522042)]
"960 most complete Android Development Notes in the whole network"
[external chain picture transferring... (img-2M6PWv6C-1630934522043)]
379 pages of Android development interview
For half a year, we sorted out the most comprehensive analysis of Android interview questions on the market
It includes the questions asked in the interview of front-line Internet companies such as Tencent, Baidu, Xiaomi, Ali, LETV, meituan, 58, cheetah, 360, Sina and Sohu. Familiarity with the knowledge points listed in this article will greatly increase the probability of passing the first two rounds of technical interviews.
How to use it?
1. You can directly look through the required knowledge points through the directory index to find out omissions and fill vacancies.
2. The number of five pointed stars indicates the frequency of interview questions and represents the important recommendation index
[external chain picture transferring... (img-Q1JLBsQw-1630934522045)]
507 page Android development related source code analysis
As long as programmers, whether Java or Android, don't read the source code and only look at the API documents, they will just stay superficial, which is unfavorable to the establishment and completion of our knowledge system and the improvement of practical technology.
What can really exercise your ability is to read the source code directly, not only limited to reading the source code of major systems, but also various excellent open source libraries.