专栏首页一个会写诗的程序员的博客Kotlin 开发者社区: Kotlin collections (集合类)

Kotlin 开发者社区: Kotlin collections (集合类)

Nicolas Fränkel is a Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Currently working for Exoscale. Also double as a teacher in universities and higher education schools, a trainer and triples as a book author.

Unfortunately, I must admit I have a hard time reading the documentation of Scala collections e.g.:

不幸的是,我必须承认我很难阅读Scala集合的文档,例如

trait LinearSeq [+A] extends Seq[A] with collection.LinearSeq[A] with GenericTraversableTemplate[A, LinearSeq] with LinearSeqLike[A, LinearSeq[A]]

Hence, I will only describe collections from the Kotlin side.

Iterator

At the root of Kotlin’s Collection API lies the Iterator interface, similar to Java’s. But the similitude stops after that. java.util.ListIterator features are broken down into different contracts:

  1. ListIterator to move the iterator index forward and backward
  2. MutableIterator to remove content from the iterator
  3. MutableListIterator inherits from the 2 interfaces above to mimic the entire contract of java.util.ListIterator

在Kotlin的Collection API的根源是Iterator接口,类似于Java。但在此之后,相似之处就停止了。java.util.ListIterator 功能分为不同的协议:

  1. ListIterator 向前和向后移动迭代器索引
  2. MutableIterator 从迭代器中删除内容
  3. MutableListIterator 从上面的2个接口继承来模仿整个合同 java.util.ListIterator

Collection, List and Set

The hierarchy of collections in Kotlin are very similar as in Java: Collection, Listand Set. (I won’t detail maps, but they follow the same design). The only, but huge, difference is that it’s divided between mutable and immutable types. Mutable types have methods to change their contents (e.g. add() andset()`), while immutable types don’t.

Of course, the hierarchy is a bit more detailed compared to Java, but that’s expected from a language that benefits from its parent’s experience.

Kotlin 集合类的层次结构和Java非常相似:CollectionListSet。(我不会详细说明地图,但它们遵循相同的设计)。唯一但又巨大的区别在于它分为可变类型和不可变类型。可变类型具有改变其内容的方法(例如 add() andset()`),而不可变类型则不具有。

当然,与Java相比,层次结构更加详细, 毕竟是基于Java基础之上的。

image

Implementations

IMHO, the important bit about Kotlin collections is not their hierarchy - though it’s important to understand about the difference between mutable and immutable.

As Java developers know, there’s no such things as out-of-the-box immutable collection types in Java. When an immutable collection is required, the mutable collection must be wrapped into an unmodifiable type via a call to the relevant Collections.unmodifiableXXX(). But unmodifiable types are not public, they are private in Collections: types returned are generic ones (List or Set interfaces). It means they implement all methods of the standard collections. Immutability comes from the mutable-related methods throwing exceptions: at compile time, there’s no way to differentiate between a mutable and an immutable collection.

On the opposite, Kotlin offers a clean separation between mutable and immutable types. It also provides dedicated functions to create objects of the relevant type:

相反,Kotlin提供了可变类型和不可变类型之间的清晰分离。它还提供专用函数来创建相关类型的对象:

image

As opposed to Scala, Kotlin doesn’t implement its own collection types, it reuses those from Java. That means that even when the compile-time type is immutable, the runtime type is always mutable. The downside is that it’s possible to change the collection elements by casting it to the correct runtime type. IMHO, this is no more severe than what allows standard reflection. There are several advantages, though:

  1. Faster time-to-market
  2. Java collection types benefited from years of improvement
  3. The underlying implementation can be changed in the future with full backward compatibility

To go further:

  • Kotlin documentation on collections

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • RESTFeel: 一个企业级的API管理&测试平台。RESTFeel帮助你设计、开发、测试您的APIRESTFeel功能简介:MongoDB configuration:Building From

    The build file is configured to download and use an embedded Tomcat server. So t...

    一个会写诗的程序员
  • Java 实现十进制数转换为二进制

    一个会写诗的程序员
  • RPC failed; curl 56 SSLRead() return error

    RPC failed; curl 56 SSLRead() return error

    一个会写诗的程序员
  • redis配置文件

    查看配置文件:https://raw.githubusercontent.com/antirez/redis/5.0.3/redis.conf

    chenchenchen
  • 从未失手的AI 预测:川普将赢得选举,入主白宫 (附深度学习生成川普语录教程)

    【新智元导读】 从2004年开始连续三次准确预测美国总统大选结果的AI系统MogAI10月28日发布最新预测,看好川普赢得与希拉里的2016总统之争。不管最终结...

    新智元
  • ROS和RRT的一些资料

    ROS和RRT结合的示例比较多,之前博文提过两次( 1 和 2 ),本文做一些汇总和整理,大部分都在roswiki和GitHub上有具体说明。需要认真阅读源码和...

    zhangrelay
  • IBM i System Modernization - Need of Hour

    IBM i has evolved overtime and organizations are modernizing their existing lega...

    saurabhkumawat
  • A Crash Course in 3D

    First, we do not pretend here that we will cover everything that is important fo...

    用户2398817
  • SAP CRM settype的重要性

    Product settype acts as a very important role in CRM WebClient UI architecture.

    Jerry Wang
  • SAP CRM settype的重要性

    Product settype acts as a very important role in CRM WebClient UI architecture.

    Jerry Wang

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动