List.fill (int) (method ()) equivalent in Java?

There is a method in Scala that looks something like this.

List[(A, B)] = List.fill(n)(doSomething(arg))

My question is, is there any way to do this in Java, or if it needs to be done through a series of long ones forand what you have.

There is in Java Collections.fill, but it does not seem to do what I want.

Scala is executed 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 look like Java to me, or not from what I know about Java, or what I have been able to find in the documentation so far.

This Scala code can be found on page 7 of functional programming in Scala by Paul Kiyana and Runar Bjarnason.

+4
source share
5 answers

There is a Java 8 equivalent:

  • , , ,

:

List<Result> collect = IntStream.range(0, 5)
                      .mapToObj(i -> doSomething(i))
                      .collect(Collectors.toList());

public Result doSomething(Integer i) {
  return ...;
}
+5

, "" java, "".

List list = new ArrayList();
for(int i = 0; i < n; i++) list.add(buyCoffee(cc));

"", scala. ?

Java , , : , scala.

, java8, (, ), (, byCoffee , .)

Java-, java, .

+1

JEP .

, . Java 8 fill:

import java.util.*;
import java.util.function.*;

public class j {
  public static <T> List<T> fill(int n, Function<Integer, T> f) {
    return Collections.unmodifiableList(new ArrayList<T>(n) {{
      for (int i = 0; i < n; i++) add(f.apply(i));
    }});
  }
  public static void main(String[] args) {
    List<Integer> values = fill(10, i -> 2 * i);
    for (int x : values) System.out.println(x);
  }
}

JEP , , API .

+1

:

ArrayList<Coffee> array = new ArrayList<Coffee>();
// array.add("Coffe Expresso") ...
CreditCard cc = new CreditCard("1234 4321 1234 4321");
for(Coffee obj : array){
   obj = new Coffee("Coffee - no sugar");
   buyCoffee(obj, cc);
}

public void buyCoffee(Coffee coffee, CreditCard cc){
    //...
}
0

, , , , "" . Java, .

class Cafe
{

Results buyCoffee(CreditCard cc)
{
    Coffee cup = new Coffee();
    List<Results> myList = new ArrayList<Results>();

    myList.add(0, new Results(cup, new Charge(cc, cup.price)));
    return myList.get(0);
}


Map<List<Coffee>, Charge> buyCoffees(CreditCard cc, int n)
{
    List<Results> purchases = new ArrayList<Results>(n);
    for(int a = 0; a < n; a++)
    {
        Results r = new Results();
        r = buyCoffee(cc);
        purchases.add(r);
    }

    List<Coffee> coffeeList = new ArrayList<Coffee>(n);
    List<Charge> chargeList = new ArrayList<Charge>(n);

    for(int b = 0; b < n; b++)
    {
        Coffee c = purchases.get(b).ThisCoffee;
        Charge charge = purchases.get(b).ThisCharge;
        coffeeList.add(c);
        chargeList.add(charge);
    }

    Charge fc = new Charge();
    Double[] ChargeAmountArray = new Double[n];

    for(int d = 0; d < n; d++)
    {
        Double x = chargeList.get(d).amount;
        ChargeAmountArray[d] = x;
    }

    double sum = 0;

    for(Double d: ChargeAmountArray)
    {
        sum += d;           // 50.0@n=5, 100.0@n=10
    }

    Charge finalCharge = new Charge(cc, sum);

    Map<List<Coffee>, Charge> map;
    map = new HashMap<List<Coffee>, Charge>();
    map.put(coffeeList, finalCharge);
    return map;
}
}
0

Source: https://habr.com/ru/post/1626528/


All Articles