为什么 Scala 集合中没有不可变的双链表? [英] Why no immutable double linked list in Scala collections?

查看:18
本文介绍了为什么 Scala 集合中没有不可变的双链表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 this 问题,其中提问者对 List,似乎更有效的解决方案是使用可以从列表末尾向后搜索的 DoubleLinkedList.然而,集合 API 中只有一种实现并且它是可变的.

Looking at this question, where the questioner is interested in the first and last instances of some element in a List, it seems a more efficient solution would be to use a DoubleLinkedList that could search backwards from the end of the list. However there is only one implementation in the collections API and it's mutable.

为什么没有不可变的版本?

Why is there no immutable version?

推荐答案

因为每次要进行更改时都必须复制整个列表.使用普通链表,您至少可以添加到列表中,而无需复制所有内容.如果您确实想在每次更改时复制所有内容,则不需要为此使用链表.你可以只使用一个不可变数组.

Because you would have to copy the whole list each time you want to make a change. With a normal linked list, you can at least prepend to the list without having to copy everything. And if you do want to copy everything on every change, you don't need a linked list for that. You can just use an immutable array.

这篇关于为什么 Scala 集合中没有不可变的双链表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆