14

I'm looking at the API and the :+ method returns a new LinkedList. The append method will only allow the appending of another linked list. The += method needs a var to work. Why would anyone ever need these if the LinkedList is mutable? What craziness is this?

If I had something like this in Java

final LinkedList myList = new LinkedList<String>();
mylist.add("balh");

How do I achieve the same thing in Scala?

dpington
  • 1,844
  • 3
  • 17
  • 29

4 Answers4

10

If append can only take a LinkedList then why not use

mylist append LinkedList("something")

or

mylist append LinkedList(otherContainer: _*)

There is a reason for allowing only other LinkedLists in append, I think, because this guarantees the following:

l1 = LinkedList(1, 2, 3)
l2 = LinkedList(4)
l3 = LinkedList(5)

l1 append l2
// l1 == LinkedList(1, 2, 3, 4)
// l2 == LinkedList(4)

l2 append l3
// l1 == LinkedList(1, 2, 3, 4, 5)
// l2 == LinkedList(4, 5)
// l3 == LinkedList(5)
Debilski
  • 66,976
  • 12
  • 110
  • 133
8

You can use a Buffer to build your values and convert it in the data structure using mapResult.

//Create a buffer which will build a linked list
val buf = new ArrayBuffer[String]  mapResult { xs => LinkedList( xs:_* ) }

//You can append elements with +=, it is overriden to allow its use on a val
buf += "Something"
buf += "else"

//At the end you get your list
val lst = buf.result

// lst ==  LinkedList(Something, else) 
paradigmatic
  • 40,153
  • 18
  • 88
  • 147
3

Mutability is on the actual elements within the list and not on the structure that the list has (persistence). This is also stated in the scaladoc.

If returning a new List each time is not what you are after, and you cannot use var, you could always use the Java LinkedList.

val mylist = new java.util.LinkedList[String]
mylist add "something"

I'd stick with the Scala lists, if at all possible.

gpampara
  • 11,989
  • 3
  • 27
  • 26
  • 2
    Note that `java.util.LinkedList` is a doubly linked List which is something different from Scala’s `LinkedList`. – Debilski Nov 09 '11 at 08:19
  • 2
    Another option is to use `scala.collection.mutable.ListBuffer[A]` and then `.toList` when you want a `List`. – gpampara Nov 09 '11 at 08:55
  • @Debilski in which aspects is diffrent? I ask, because I can't understand which implementation of List Scala provides when typing `List(1, 2, 3)`. Is singly-linked list? Or double linked list? – WelcomeTo May 08 '13 at 07:13
  • `List` (= `scala.collection.immutable.List`) is an immutable singly-linked list. – Debilski May 08 '13 at 08:35
2

First, please pay more attention to the docs:

This class implements single linked lists where both the head (elem) and the tail (next) are mutable.

So what it is giving you is mutable head and tail. These operations are represented through three methods:

  • append and insert change tail, so they receive a LinkedList as argument
  • update change the head of an element.

If you want a class that can grow, look at the classes extending Growable.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681