Kotlin和不可变的收藏?

我正在学习Kotlin,看起来很可能我想在明年内把它当作我的主要语言。 然而,我一直在收到相互矛盾的研究,Kotlin是否有不可变的集合,我试图弄清楚是否需要使用Google Guava。

有人可以给我一些指导呢? 默认情况下使用Immutable集合吗? 什么操作符返回可变或不可变集合? 如果没有,是否有计划实施它们?

标准库中的Kotlin List是只读的:

 interface List<out E> : Collection<E> (source) 

元素的通用有序集合。 这个接口中的方法只支持对列表的只读访问。 通过MutableList接口支持读/写访问。

参数
E – 列表中包含的元素的类型。

如前所述,还有MutableList

 interface MutableList<E> : List<E>, MutableCollection<E> (source) 

支持添加和删除元素的通用有序元素集合。

参数
E – 列表中包含的元素的类型。

因此,Kotlin通过其接口强制执行只读行为,而不像像默认的Java实现那样在运行时抛出异常。

同样,还有一个MutableCollectionMutableIterableMutableIteratorMutableListIteratorMutableMapMutableSet ,请参阅stdlib文档。

这是令人困惑的,但有三种,而不是两种不变性:

  1. 可变 – 你应该改变集合(Kotlin的MutableList
  2. 只读 – 你不应该改变它(Kotlin的List ),但可能会(转换为可变,或从Java改变)
  3. 不变的 – 没有人能改变它(番石榴的不变的收藏)

所以万一(2) List只是一个接口,没有变异的方法,但你可以改变实例,如果你把它转换为MutableList

使用番石榴(案例(3)),你可以安全的从任何人改变收集,即使是一个铸造或从另一个线程。

Kotlin选择只读,以便直接使用Java集合,所以在使用Java集合时没有开销或转换。

正如您在其他答案中所看到的,Kotlin只读接口可以让您通过只读镜头查看集合。 但是这个集合可以通过转换或Java处理来绕过。 但是在合作的Kotlin代码中,很好,大多数的用法并不需要真正的不可变的集合,如果你的团队避免把集合转换为集合的可变形式,那么也许你不需要完全不可变的集合。

Kotlin系列允许同时发生拷贝突变和懒惰突变。 所以要回答你的问题的一部分,像filtermapflatmap ,运营商+ -所有创建副本时使用非懒惰集合。 在Sequence上使用时,它们将修改值作为访问时的集合,并继续为惰性(导致产生另一个Sequence )。 虽然对于一个Sequence ,调用toListtoSettoMap等任何东西都会导致最终的拷贝。 通过命名约定几乎任何开始与复制。

换句话说,大多数操作符会返回与您开始时相同的类型,如果该类型是“只读”,则会收到副本。 如果这种类型是懒惰的,那么你将懒洋洋地应用这个改变,直到你要求完整的收集。

有些人希望他们出于其他原因,如并行处理。 在这种情况下,最好看一下为此目的设计的高性能系列。 只在这些情况下使用它们,而不是在一般情况下。

在JVM世界中,很难避免与需要标准Java集合的库进行交互,并且对这些集合的转换会给不支持通用接口的库增加许多痛苦和开销。 Kotlin提供了良好的互操作性和缺乏转换性,只有合同保护。

所以,如果你不能避免想要不可变的集合,Kotlin可以轻松处理JVM空间中的任何东西:

此外,Kotlin团队正在为Kotlin本地开发不可变集合,可以在这里看到这个功能: https : //github.com/Kotlin/kotlinx.collections.immutable

还有许多其他的收集框架可以满足所有不同的需求和限制,Google是您找到它们的朋友。 Kotlin团队没有必要为标准库重新创建它们。 你有很多的选择,他们专注于不同的事情,如性能,记忆使用,不拳击,不变性等“选择是好的”…因此,一些其他: HPCC , HPCC – RT , FastUtil , Koloboke , 特罗夫和更多…

甚至有像Pure4J这样的努力,既然Kotlin现在支持Annotation处理,也许可以有类似理想的端口给Kotlin。

Kotlin 1.0将不会在标准库中拥有不可变的集合。 但是,它具有只读和可变接口 。 没有什么可以阻止你使用第三方不可变的收集库。

Kotlin的List接口中的方法“仅支持对列表的只读访问”,而其MutableList接口中的方法支持“添加和删除元素”。 但是,这两者都只是接口

Kotlin的List接口在编译时强制执行只读访问,而不是像java.util.Collections.unmodifiableList(java.util.List)那样将这些检查推迟到运行时(它返回指定列表的一个不可修改的视图…试图修改返回的列表…导致一个UnsupportedOperationException 。“ 它不强制不变性。

考虑下面的Kotlin代码:

 import com.google.common.collect.ImmutableList import kotlin.test.assertEquals import kotlin.test.assertFailsWith fun main(args: Array<String>) { val readOnlyList: List<Int> = arrayListOf(1, 2, 3) val mutableList: MutableList<Int> = readOnlyList as MutableList<Int> val immutableList: ImmutableList<Int> = ImmutableList.copyOf(readOnlyList) assertEquals(readOnlyList, mutableList) assertEquals(mutableList, immutableList) // readOnlyList.add(4) // Kotlin: Unresolved reference: add mutableList.add(4) assertFailsWith(UnsupportedOperationException::class) { immutableList.add(4) } assertEquals(readOnlyList, mutableList) assertEquals(mutableList, immutableList) } 

请注意, readOnlyList是一个List ,而add等方法无法解析(也不会编译), mutableList可以自然地进行变异,并且可以在编译时解析immutableList (来自Google Guava),但是会抛出异常运行。

除了最后一个导致Exception in thread "main" java.lang.AssertionError: Expected <[1, 2, 3, 4]>, actual <[1, 2, 3]>. 即我们成功突变了只读List

请注意,使用listOf(...)而不是arrayListOf(...)将返回一个有效的不可变列表,因为您无法将其转换为任何可变列表类型。 但是,为变量使用List接口并不妨碍将MutableList赋值给它( MutableList<E> extends List<E> )。

最后,请注意,Kotlin(以及Java)中的接口不能强制不变性,因为它“无法存储状态”(请参阅接口 )。 因此,如果你想要一个不可变的集合,你需要使用类似Google Guava提供的东西。


请参阅ImmutableCollectionsExplained·google / guava Wiki·GitHub

注: 这个答案在这里,因为代码是简单的和开放源代码,你可以使用这个想法使你的集合,你创建不可变的。 它不只是作为图书馆的广告。

在Klutter图书馆 ,新的Kotlin不可变的包装,使用Kotlin代表团包裹现有的Kotlin收集界面与保护层没有任何性能影响。 那么没有办法将集合,它的迭代器或其他集合转换成可以修改的东西。 他们变得不可变。

Klutter 1.20.0发布,它为现有的集合添加了不可变的保护器,基于@miensol的SO回答,提供了一个围绕集合的轻量级委托,可以防止任何修改,包括将其转换为可变类型,然后进行修改。 Klutter通过保护子集合(例如iterator,listIterator,entrySet等)更进一步。所有这些门都关闭了,并且使用Kotlin委托给大多数在性能上没有打击的方法。 只需调用myCollection.asReadonly()protect )或myCollection.toImmutable()复制然后保护 ),结果是相同的接口但保护。

下面是代码示例,通过基本上将接口委托给实际的类,同时覆盖突变方法以及返回的任何子集合被动态地包装起来。

 /** * Wraps a List with a lightweight delegating class that prevents casting back to mutable type */ open class ReadOnlyList <T>(protected val delegate: List<T>) : List<T> by delegate, ReadOnly, Serializable { companion object { @JvmField val serialVersionUID = 1L } override fun iterator(): Iterator<T> { return delegate.iterator().asReadOnly() } override fun listIterator(): ListIterator<T> { return delegate.listIterator().asReadOnly() } override fun listIterator(index: Int): ListIterator<T> { return delegate.listIterator(index).asReadOnly() } override fun subList(fromIndex: Int, toIndex: Int): List<T> { return delegate.subList(fromIndex, toIndex).asReadOnly() } override fun toString(): String { return "ReadOnly: ${super.toString()}" } override fun equals(other: Any?): Boolean { return delegate.equals(other) } override fun hashCode(): Int { return delegate.hashCode() } } 

除了助手扩展功能,使访问更容易:

 /** * Wraps the List with a lightweight delegating class that prevents casting back to mutable type, * specializing for the case of the RandomAccess marker interface being retained if it was there originally */ fun <T> List<T>.asReadOnly(): List<T> { return this.whenNotAlreadyReadOnly { when (it) { is RandomAccess -> ReadOnlyRandomAccessList(it) else -> ReadOnlyList(it) } } } /** * Copies the List and then wraps with a lightweight delegating class that prevents casting back to mutable type, * specializing for the case of the RandomAccess marker interface being retained if it was there originally */ @Suppress("UNCHECKED_CAST") fun <T> List<T>.toImmutable(): List<T> { val copy = when (this) { is RandomAccess -> ArrayList<T>(this) else -> this.toList() } return when (copy) { is RandomAccess -> ReadOnlyRandomAccessList(copy) else -> ReadOnlyList(copy) } } 

你可以看到这个想法,并推断从这个代码中创建缺少的类,它重复其他引用类型的模式。 或者在这里查看完整的代码:

https://github.com/kohesive/klutter/blob/master/core-jdk6/src/main/kotlin/uy/klutter/core/common/Immutable.kt

而且测试显示了一些允许修改之前的技巧,但现在不能,以及使用这些包装器的阻塞转换和调用。

https://github.com/kohesive/klutter/blob/master/core-jdk6/src/test/kotlin/uy/klutter/core/collections/TestImmutable.kt