Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala

What is fill in Scala?

Samia Ishaque

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The fill method in Scala populates a list with the same item (X) Y number of times. Below is the general syntax of how this is done:

List.fill(Y)("X")

Parameters

The fill method requires two necessary paramters as input. The first is Y, the number of times we wish to add an item to the list. The second is X, which is the item that we wish to add to the list.

Return value

The fill method returns a list filled with the desired values.

Example

Below is an example of how you can fill a list with the values you wish to add to it. We use the fill method to fill a list with the word hello five times.

object Main extends App {
   val items = List.fill(5)("hello")
   print(items)
}

RELATED TAGS

scala

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring