Common collection operations

Question Click to View Answer

Use a builder to create Array(10, 20).

val b = Array.newBuilder[Int]
b += 10
b += 20

Use a factory method to create Array("hello", "hello", "hello").

Array.fill(3)("hello")

Combine Array(1, 2) and Array(3, 4) to create Array(1, 2, 3, 4).

Array(1, 2) ++ Array(3, 4)

Multiple every element in Array(1, 2, 3) by two.

Array(1, 2, 3).map(i => i * 2)

Remove the even numbers from Array(1, 2, 3, 4, 5).

Array(1, 2, 3, 4, 5).filter(i => i % 2 == 1)

Grab the first two elements from Array(1, 2, 3, 4, 5).

Array(1, 2, 3, 4, 5).take(2).

Remove the first two elements from Array(1, 2, 3, 4, 5)

Array(1, 2, 3, 4, 5).drop(2)

Create an array with the elements from index positions 1 through 4 from Array(1, 2, 3, 4, 5)

Array(1, 2, 3, 4, 5).slice(1, 4)

Create an array with the unique elements in Array(1, 1, 1, 2, 3, 2)

Array(1, 1, 1, 2, 3, 2).distinct

Find the first even value that's greater than four in this array Array(1, 2, 3, 4, 5, 6, 7, 8)

val arr = Array(1, 2, 3, 4, 5, 6, 7, 8)
arr.find(i => i % 2 == 0 && i > 4) // Some(6)

Find the even values greater than 10 in Array(1, 2, 3)

val arr = Array(1, 2, 3)
arr.find(i => i % 2 == 0 && i > 10) // None

Return true if Array(1, 2, 3) contains a value greater than one.

val arr = Array(1, 2, 3)
arr.exists(i => i > 1) // true

Return false if Array(1, 2, 3) does not contain a value greater than zero.

val arr = Array(1, 2, 3)
arr.exists(i => i < 0) // false

Convert Array("a", "b", "c") into the String "a,b,c"

Array("a", "b", "c").mkString(",")

Convert Array("a", "b", "c") to "{a,b,c}"

Array("a", "b", "c").mkString("{", ",", "}")

Sum all the elements in Array(1, 2, 3, 4) with a one liner.

val arr = Array(1, 2, 3, 4)
arr.foldLeft(0)((memo, y) => memo + y)

Multiply all the elements in Array(1, 2, 3, 4)

val arr = Array(1, 2, 3, 4)
arr.foldLeft(1)((memo, y) => memo * y)

Multiply all the elements in Array(1, 2, 3, 4) with placeholder syntax.

val arr = Array(1, 2, 3, 4)
arr.foldLeft(1)(_ * _) // 24

Add all the elements in Array(1, 2, 3, 4) with a for loop.

var total = 0
for (i <- Array(1, 2, 3, 4)) total += i
total // 10

Explain the problem with the following code:

val arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
val res = arr.map(_ + 1).filter(_ % 2 == 0).slice(1, 3)
res // Array(4, 6)

Refactor the code so it's better.

This code is inefficient because it creates intermediate collections that are immediately thrown away.

arr.map(_ + 1) creates Array(2, 3, 4, 5, 6, 7, 8, 9, 10), filter(_ % 2 == 0) creates Array(2, 4, 6, 8, 10) and slice(1, 3) creates Array(4, 6).

You can avoid unnecessarily creating intermediate collections with view and to.

val arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
val res = arr.view.map(_ + 1).filter(_ % 2 == 0).slice(1, 3).to(Array)
res // Array(4, 6)

You only need to worry about this when your collections are large enough to create a performance bottleneck.

Convert Array(1, 2, 3) to a vector.

Array(1, 2, 3).to(Vector)

Convert Vector(1, 2, 3) to an Array.

Vector(1, 2, 3).to(Array)

Convert Array(1, 1, 2, 2, 3, 3) to a Set.

Array(1, 1, 2, 2, 3, 3).to(Set) // Set(1, 2, 3)

Notice how the set does not include any duplicate values.