Scala Tutorial - Learn How To Use ReverseIterator Function With Examples

By Nadim Bahadoor | Last updated: March 16, 2018 at 15:05 pm

Overview

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

 

The reverseIterator method returns an iterator which you can use to traverse the elements of a collection in reversed order.

 

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

def reverseIterator: Iterator[A]

The reverseIterator method is a member of SeqLike trait.

Steps

1. How to initialize a sequence of donut prices

The code below show how to initialize a Sequence of type Double to represent donut prices..

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 print all elements in reversed order using reverseIterator function

The code below shows how to use the reverseIterator to print all the elements in a collection in reversed order.


println("\nStep 2: How to print all elements in reversed order using reverseIterator function")
println(s"Elements of donuts in reversed order = ${donuts.reverseIterator.toList}")

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


Step 2: How to print all elements in reversed order using reverseIterator function
Elements of donuts in reversed order = List(Glazed Donut, Strawberry Donut, Plain Donut)

 

3. How to iterate through elements using foreach method

In this example, we use the reverseIterator on the donut Sequence which returns an Iterator of type String, i.e. Iterator[String]. To print the elements of this iterator, we then make use of the foreach method.


println("\nStep 3: How to iterate through elements using foreach method")
val reverseIterator: Iterator[String] = donuts.reverseIterator
reverseIterator.foreach(donut => println(s"donut = $donut"))

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


Step 3: How to iterate through elements using foreach method
donut = Glazed Donut
donut = Strawberry Donut
donut = Plain Donut

 

This concludes our tutorial on Learn How To Use ReverseIterator 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 donut prices
  • How to print all elements in reverse order using reverseIterator function
  • How to iterate through elements using foreach method

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 the scan 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: