append

Scala Array class: methods, examples, and syntax alvin June 16, 2018 - 5:09pm

This page contains a large collection of examples of how to use the methods on the Scala Array class.

Scala List class: methods, examples, and syntax alvin May 22, 2018 - 1:00pm

This page contains a large collection of examples of how to use the methods on the Scala List class.

The Scala List class as an immutable, linear, linked-list class. It’s very efficient when it makes sense for your algorithms to (a) prepend all new elements, (b) work with it in terms of its head and tail elements, and (c) use functional methods that traverse the list from beginning to end, such as filter, map, foldLeft, reduceLeft.

Scala Seq class: methods, examples, and syntax

This page contains a large collection of examples of how to use the methods on the Scala Seq class.

Important note about Seq, IndexedSeq, and LinearSeq

As an important note, I use Seq in the following examples to keep things simple, but in your code you should be more precise and use IndexedSeq or LinearSeq where appropriate. As the Seq class Scaladoc states:

Scala: How to append and prepend items to Vector and Seq alvin April 27, 2018 - 1:01pm
Table of Contents1 - Solution2 - Example data3 - Append a single item4 - Append multiple elements5 - Prepend a single item6 - Prepend multiple elements7 - Seq works just like Vector8 - How to remember the method names9 - A possible problem

Scala FAQ: How do I append or prepend one or more elements to a Vector or Seq class?

Back to top

Solution

To append or prepend one or more elements to a Vector or Seq, use these methods:

How to merge Scala sequential collections (List, Vector, ArrayBuffer, Array, Seq)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.22, “How to Merge Scala Sequential Collections”

Problem

You want to join two sequences into one sequence, either keeping all of the original elements, finding the elements that are common to both collections, or finding the difference between the two sequences.

Solution

There are a variety of solutions to this problem, depending on your needs: