In Scala, there is a method that looks something like this.
List[(A, B)] = List.fill(n)(doSomething(arg))
My question is if there is any way to do this in Java, or if it would have to be done through a series of lengthy for
s and what have you.
Java does have Collections.fill
but it doesn't seem to do what I want it to do.
Scala implementation is as follows:
def buyCoffee(cc: CreditCard): (Coffee, Charge) =
{
val cup = new Coffee()
(cup, Charge(cc, cup.price))
}
def buyCoffees(cc: CreditCard, n: Int): (List[Coffee], Charge) =
{
val p: List[(Coffee, Charge)] = List.fill(n)(buyCoffee(cc))
}
This does not seem achievable in Java to me, or not from what I know of Java or what I have been able to find in the documentation thus far.
This Scala code can be found on Page 7 of Functional Programming in Scala by Paul Chiusana and Rúnar Bjarnason.