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

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

问题描述

查看问题,其中提问者对<$中某些元素的第一个和最后一个实例感兴趣c $ c> List ,看起来更有效的解决方案是使用 DoubleLinkedList ,可以从列表的末尾向后搜索。

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天全站免登陆