简体   繁体   中英

How do you program to interface in Kotlin?

In Java we can have code like this:

List<String> a = new ArrayList<>();

How can we do that in Kotlin?

You can see basic usage of the list and set types below:

val numbers: MutableList<Int> = mutableListOf(1, 2, 3)
val readOnlyView: List<Int> = numbers
println(numbers)        // prints "[1, 2, 3]"
numbers.add(4)
println(readOnlyView)   // prints "[1, 2, 3, 4]"
readOnlyView.clear()    // -> does not compile

val strings = hashSetOf("a", "b", "c", "c")
assert(strings.size == 3)

For more information check this page: Kotlin

In Kotlin programming to the interface is often times not needed, since it has type inference. You can simply write:

val someList = ArrayList<String>()
someList.add("Hello")

and if you ever change the type of the list you don't have to change the delclaration:

val someList = MyCustomListWichCanOnlyHoldStrings()
someList.add("Hello")

If you really want you can still specify the interface manually like this:

val someList: List<String> = ArrayList<String>()
someList.add("Hello")

Edit:

I just want to elaborate a little more, why you program to the interface in Java.

When you write all your code like this:

public ArrayList<String> giveMeSomeList() {
    ArrayList<String> someList = new ArrayList<>();
    someList.add("some");
    return someList;
}

And you want to change the list type to LinkedList you need to change the code in at least three places above.

When you program against the interface like this:

public List<String> giveMeSomeList() {
    List<String> someList = new ArrayList<>();
    someList.add("some");
    return someList;
}

you only need to chage the code in the initialization and the rest of the code can be left as is.


In Kotlin we don't have this problem since the type gets inferred. When you don't write the type you don't have to change it if the type changes.

I interpreted this question two different ways, so I'm gonna write this answer containing both:

  1. Using Lists in Kotlin
val mutableList = mutableListOf(/*items*/)//Similar to ArrayList
var immutableList = listOf(/*items*/)//You can't add or remove 

They can be initialized without any items, but it requires the diamond operator to specify the type.

You should use the Kotlin initialization methods when creating lists though. ArrayList has arrayListOf<Type>(items) The code you had in the question would be:

val list: List<String> = mutableListOf()

or

val List: List<String> = listOf()

There's also arrayListOf (as I mentioned earlier) and, not directly a list, but setOf .

  1. Declaring a superclass type (ie List) but initializing a child-class

When you do:

val /*var*/ someField = SomeClass()

in Kotlin, someField gets automatic type inference, which here is SomeClass. When you do mutableListOf<SomeClass> , the inferred type is MutableList<SomeClass> . So, if you want to specify a specific child, you need to manually write the type:

val /*var*/ someList: List<SomeClass> = mutableListOf()

Just like in Java, just with different syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM