Scala Tutorial - Learn How To Use NonEmpty Function With Examples

By Nadim Bahadoor | Last updated: March 16, 2018 at 14:39 pm

Overview

In this tutorial, we will learn how to use the nonEmpty function with examples on collection data structures in Scala. The nonEmpty function is applicable to both Scala's Mutable and Immutable collection data structures.

 

The nonEmpty method will test whether a given collection is not empty and will return either true or false

 

As per the Scala documentation, the definition of the nonEmpty method is as follows:

def nonEmpty: Boolean

 

The nonEmpty method is a member of the TraversableOnce trait, but there are other concrete implementations for corresponding collection types.

Steps

1. How to initialize a Sequence of donuts

The code below shows how to initialize a Sequence of donut elements of type String.


println("Step 1: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donuts = $donuts")

You should see the following output when you run your Scala application in IntelliJ:


Step 1: How to initialize a Sequence of donuts
Elements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)

 

2. How to check if a sequence is not empty using nonEmpty function

The code below shows how to use the nonEmpty method to test whether the donuts Sequence is not empty.


println("\nStep 2: How to check if a sequence is not empty using nonEmpty function")
println(s"Is donuts sequence NOT empty = ${donuts.nonEmpty}")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to check if a sequence is not empty using nonEmpty function
Is donuts sequence NOT empty = true

 

3. How to create an empty sequence

The code below show how to create an empty Sequence of type String. We will then use this Sequence to test whether it is empty in Step 4.


println("\nStep 3: How to create an empty sequence")
val emptyDonuts: Seq[String] = Seq.empty[String]
println(s"Elements of emptyDonuts = $emptyDonuts")

You should see the following output when you run your Scala application in IntelliJ:


Step 3: How to create an empty sequence
Elements of emptyDonuts = List()

 

4. How to find out if sequence is empty using nonEmpty function

With the empty Sequence from Step 3, we can call the nonEmpty method. As expected, the result is false because Sequence emptyDonuts from Step 3 does not have any elements.

println("\nStep 4: How to find out if sequence is empty using nonEmpty function")
println(s"Is emptyDonuts sequence empty = ${emptyDonuts.nonEmpty}")

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to find out if sequence is empty using nonEmpty function
Is emptyDonuts sequence empty = false

This concludes our tutorial on Learn How To Use NonEmpty Function With Examples and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to initialize a Sequence of donuts
  • How to check if a sequence is not empty using nonEmpty function
  • How to create an empty sequence
  • How to find out if sequence is empty using nonEmpty function

Tip

  • Review the tutorials on Mutable and Immutable collection data structures in Scala.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to use par function.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: