Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is List in Kotlin a LinkedList?

The most popular lists in Java are ArrayList and LinkedList.

In Kotlin I see ArrayList and List. Is List actually a LinkedList? For example

val list = listof("1st", "2nd")
like image 854
androidmanifest Avatar asked Jun 26 '18 11:06

androidmanifest


People also ask

Is Kotlin list a linked list?

Kotlin Topics. LinkedList provides a doubly linked list data structure implementation. It implements both the List and Deque interface. It belongs to the Java collections framework.

Is LinkedList same as list?

Linked lists differ from lists in the way that they store elements in memory. While lists use a contiguous memory block to store references to their data, linked lists store references as part of their own elements.

What is lists in Kotlin?

A list is an ordered collection of items. In Kotlin, lists can be either mutable ( MutableList ) or read-only ( List ). For list creation, use the standard library functions listOf() for read-only lists and mutableListOf() for mutable lists.

Is linked list a list or queue?

Queue is a collection of one or more elements arranged in memory in a contiguous fashion. A linked list is a collection of one or more elements arranged in memory in a dis-contiguous fashion. Static Queue is always fixed size. List size is never fixed.


1 Answers

Just tried printing the type,

* for one element it's a SingletonList

val list = listOf("a")
println(list.javaClass)

output: class java.util.Collections$SingletonList

* for more than one element it's an ArrayList

val list = listOf("a", "b", "c")
println(list.javaClass)

output: class java.util.Arrays$ArrayList

like image 150
ir2pid Avatar answered Oct 11 '22 14:10

ir2pid