How to use collection in kotlin development

About kotlin development

Using kotlin to develop Android apps is becoming more and more popular among Java engineers. If you miss kotlin for some reason, we strongly recommend that you read this article.

For developers who are at the forefront of technology and like kotlin, this article is closely related to them. So let's take a look at how to use collections in kotlin.

Collections in kotlin are a framework based on Java collections. This article focuses on several features in the kotlin. Collections package.

data processing

Kotlin has a feature of extension function, which enables the kotlin standard library (stdlib) to support the methods of classes in JDK. For example, if you open open in the kotlin standard library_ Collection.kt file, you can find a method similar to the following:

So the code you write may look like this:

You should note that filter and dropwhile, like other operators, return a new case. This means that the originallist will not change.

In order to better understand what happens at the bottom of the code, let's open the source code and take a look at the listof () method:

Because the stream APIs of rxjava and Java 8 contain similar methods, the above code is very similar to rxjava and stream APIs. However, Android engineers can't use the stream API, so they use more rxjava data processing methods to solve this problem. Then, this operation is not completely correct because rxjava is an event processing library, not data processing. So you can now use kotlin to solve this problem without worrying about these problems.

Immutable set

If you feel unfamiliar with immutable objects, we suggest you read this document first and then take a look at it.

Kotlin's method of distinguishing mutable objects from immutable objects (lists, sets, maps, etc.) is different from other programming languages. When using the kotlin set, it is very useful to distinguish these two objects accurately to avoid unnecessary errors and bugs.

Kotlin allows you to create a collection instance of kotlin in a way similar to Java.

This is the simplest and neat way. The following is the best way to write:

I created a kotlin. Collections. List reference, and we also created an immutable collection. If you don't believe it, we can take a look at the source code:

You can see that there is no add () method or remove () method in the source code, and there are no other methods to change the collection. In this example, the instance itself is Java. Util. ArrayList. Let's use an example to explain why:

You'd better look at this example in the local source code:

How to understand: is Java's ArrayList the same as kotlin's list?

In fact, there is nothing strange here. Kotlin's collection inherits the Java list interface. We can see from the kotlin.collections.collection.kt file:

As mentioned earlier, this file contains all the collection extension methods. We can see that we can use almost all the methods in the Java collectionskt class in kotlin. Of course, we also need to import Java. Util. *.

Let's take a look at how we call the kotlin collection in Java code:

You can now clearly see how the kotlin collection uses the Java list. All extension functions can be accessed as static methods.

summary

Kotlin, the Android development language, is a very interesting language. It can help us write more concise and secure code. Except for the first time, kotlin is java compatible.

Well, the above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>