set

How to get the first element from a Scala Set

I have no idea what I was thinking, but today I learned (or was reminded) that if you want the first element from a Scala Set you should use its head method, or headOption. For some reason I kept thinking that take should do the job, but you can see the results in the Scala REPL, where take(1) returns a Set:

Kotlin functions to create Lists, Maps, and Sets

Table of Contents1 - Kotlin Arrays2 - Kotlin List functions3 - Kotlin Map functions4 - Kotlin Set functions5 - Summary: Kotlin List, Map, and Set creation functions

With Kotlin you can create lists, maps, and sets with standard functions that are automatically in scope. Here are those functions.

How to use sortable Sets in Scala (SortedSet, TreeSet, LinkedHashSet)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.28, “How to Use Sortable Sets in Scala”

Problem

You want to be able to store and retrieve items from a set in a sorted order.

Solution

To retrieve values from a set in sorted order, use a SortedSet. To retrieve elements from a set in the order in which elements were inserted, use a LinkedHashSet.

A SortedSet returns elements in a sorted order:

How to delete elements from Sets in Scala (operators, methods)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.27, “How to Delete Elements from Sets in Scala”

Back to top

Problem

You want to remove elements from a mutable or immutable set.

Back to top

Solution

Mutable and immutable sets are handled differently, as demonstrated in the following examples.

Back to top

Mutable Sets

When working with a mutable Set, remove elements from the set using the -= and --= methods, as shown in the following examples:

Table of Contents

  1. Problem
  2. Solution
  3. Mutable Sets
Back to top

How to add elements to a Set in Scala (operators, methods)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.26, “How to Add Elements to a Set in Scala”

Problem

You want to add elements to a mutable set, or create a new set by adding elements to an immutable set.

Solution

Mutable and immutable sets are handled differently, as demonstrated in the following examples.

Mutable set

Add elements to a mutable Set with the +=, ++=, and add methods:

How to extract unique elements from a Scala sequence

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.21, “How to Extract Unique Elements from a Scala Sequence”

Problem

You have a collection that contains duplicate elements, and you want to remove the duplicates.

Solution

Call the distinct method on the collection:

How to manually declare a type when creating a Scala collection instance

Note: This is an excerpt from the Scala Cookbook (partially re-worded and re-formatted for the internet). This is one of the shorter recipes, Recipe 10.5, “How to manually declare a type when creating a collection instance.”

Problem

You want to create a collection of mixed types that share a common hierarchy, and Scala isn’t automatically assigning the type you want.