Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala equivalent of java.util.ArrayList

I am doing a project in Scala, but am fairly new to the language and have a Java background. I see that Scala doesn't have ArrayList, so I am wondering what Scala's equivalent of Java's ArrayList is called, and if there are any important differences between the Java and Scala versions.

EDIT: I'm not looking for a specific behavior so much as an internal representation (data stored in an array, but the whole array isn't visible, only the part you use).

like image 432
astay13 Avatar asked Nov 27 '11 17:11

astay13


People also ask

Can I use Java list in Scala?

A java list can be returned from a Scala program by writing a user defined method of Java in Scala. Here, we don't even need to import any Scala's JavaConversions object in order to make this conversions work. Now, lets see some examples.

What is the difference between list and array in Scala?

However, the Scala List is immutable and represents a linked list data structure. On the other hand, Scala array is flat and mutable.

What is the difference between SEQ and list in Scala?

A Seq is an Iterable that has a defined order of elements. Sequences provide a method apply() for indexing, ranging from 0 up to the length of the sequence. Seq has many subclasses including Queue, Range, List, Stack, and LinkedList. A List is a Seq that is implemented as an immutable linked list.


1 Answers

I can think of 3 more specific questions to address yours:

  • What is Scala's default collection?
  • What Scala collection has characteristics similar to ArrayList?
  • What's a good replacement for Array in Scala?

So here are the answers for these:

What is Scala's default collection?

Scala's equivalent of Java's List interface is the Seq. A more general interface exists as well, which is the GenSeq -- the main difference being that a GenSeq may have operations processed serially or in parallel, depending on the implementation.

Because Scala allows programmers to use Seq as a factory, they don't often bother with defining a particular implementation unless they care about it. When they do, they'll usually pick either Scala's List or Vector. They are both immutable, and Vector has good indexed access performance. On the other hand, List does very well the operations it does well.

What Scala collection has characteristics similar to ArrayList?

That would be scala.collection.mutable.ArrayBuffer.

What's a good replacement for Array in Scala?

Well, the good news is, you can just use Array in Scala! In Java, Array is often avoided because of its general incompatibility with generics. It is a co-variant collection, whereas generics is invariant, it is mutable -- which makes its co-variance a danger, it accepts primitives where generics don't, and it has a pretty limited set of methods.

In Scala, Array -- which is still the same Array as in Java -- is invariant, which makes most problems go away. Scala accepts AnyVal (the equivalent of primitives) as types for its "generics", even though it will do auto-boxing. And through the "enrich my library" pattern, ALL of Seq methods are available to Array.

So, if you want a more powerful Array, just use an Array.

What about a collection that shrinks and grows?

The default methods available to all collections all produce new collections. For example, if I do this:

val ys = xs filter (x => x % 2 == 0) 

Then ys will be a new collection, while xs will still be the same as before this command. This is true no matter what xs was: Array, List, etc.

Naturally, this has a cost -- after all, you are producing a new collection. Scala's immutable collections are much better at handling this cost because they are persistent, but it depends on what operation is executed.

No collection can do much about filter, but a List has excellent performance on generating a new collection by prepending an element or removing the head -- the basic operations of a stack, as a matter of fact. Vector has good performance on a bunch of operations, but it only pays if the collection isn't small. For collections of, say, up to a hundred elements, the overall cost might exceed the gains.

So you can actually add or remove elements to an Array, and Scala will produce a new Array for you, but you'll pay the cost of a full copy when you do that.

Scala mutable collections add a few other methods. In particular, the collections that can increase or decrease size -- without producing a new collection -- implement the Growable and Shrinkable traits. They don't guarantee good performance on these operations, though, but they'll point you to the collections you want to check out.

like image 173
Daniel C. Sobral Avatar answered Oct 01 '22 15:10

Daniel C. Sobral