Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Is there a way to initialize an array or a collection by using a simple lambda expression?

Something like

// What about this?
Person[] persons = new Person[15];
persons = () -> {return new Person()};

Or

// I know, you need to say how many objects
ArrayList<Person> persons = () -> {return new Person()};
share|improve this question
up vote 18 down vote accepted

Sure - I don't know how useful it is, but it's certainly doable:

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

public class Test
{
    public static void main(String[] args)
    {
        Supplier<Test> supplier = () -> new Test();
        List<Test> list = Stream
            .generate(supplier)
            .limit(10)
            .collect(Collectors.toList());
        System.out.println(list.size()); // 10
        // Prints false, showing it really is calling the supplier
        // once per iteration.
        System.out.println(list.get(0) == list.get(1));
    }
}
share|improve this answer
1  
Or directly .generate(() -> new Test()), just to use the lambda operator requested by OP... would rather use .generate(Test::new). – Jean-François Savard yesterday
4  
@Jean-FrançoisSavard: Yes, I was only separating out the declaration to show the types involved. – Jon Skeet yesterday

If you already have a pre-allocated array, you can use a lambda expression to populate it using Arrays.setAll or Arrays.parallelSetAll:

Arrays.setAll(persons, i -> new Person()); // i is the array index

To create a new array, you can use

Person[] persons = IntStream.range(0, 15)  // 15 is the size
    .mapToObj(i -> new Person())
    .toArray(Person[]::new);
share|improve this answer

If you want to initialize it using Java 8, you don't really need to use a lambda expression. You can achieve that using Stream:

Stream.of(new Person()).collect(Collectors.toList());
share|improve this answer
2  
I think the OP wants to call the lambda expression multiple times to populate the list - see if my answer makes sense to you. – Jon Skeet yesterday
    
@JonSkeet Totally makes sense. – Maroun Maroun yesterday
2  
This will create a list containing a single Person instance, thus, it’s a complicated way to do the same as Collections.singletonList(new Person()) – Holger yesterday
    
@Holger singletonList return an immutable list. – Jean-François Savard yesterday
    
@Jean-FrançoisSavard: Collectors.toList() gives no guarantees about mutability or immutability, so that's not really a point in favor of this answer. – user2357112 yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.