Lostsoul
Lostsoul

Reputation: 26037

How to set a default value for items list?

I'm trying to convert some python code to java and need to setup a default value of a list. I know the default value, the size of the list and my goal is to setup a default value and then later in my program change them. In python I simply do this(to create 10 items with a value of zero):

list = [0]*10  

I am trying to do:

List<Integer> list1 = Arrays.asList(0*10); // it just multiples 0 by 10.

It doest work, I know I can do something like this:

for(int i = 0;i<10;i++)
{
  list1.add(0); 
}

I was wondering if there was an better way(instead of the for loop)?

Upvotes: 16

Views: 80356

Answers (9)

Tamir Adler
Tamir Adler

Reputation: 421

List<Integer> list1 = IntStream.range(0, 10)
                         .mapToObj(i -> 0)
                         .collect(Collectors.toList());

Upvotes: 0

GetBackerZ
GetBackerZ

Reputation: 448

If you are using java 8 or above you can do the following. Here are the required imports:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

Here is the code to get it to work

List<Integer> integers = new ArrayList() {{
      IntStream.range(0,5).forEach((i) ->  add(0));
}};

The double braces are not a mistake they are required! I hope this helps.

Upvotes: 0

Michel Jung
Michel Jung

Reputation: 3296

Collections.nCopies is your friend if you need a list instead of an array:

List<Integer> list = Collections.nCopies(10, 0);

If a mutable list is needed, wrap it:

List<Integer> list = new ArrayList<>(Collections.nCopies(10, 0));

Upvotes: 22

Jon Skeet
Jon Skeet

Reputation: 1502526

There's nothing built into the standard libraries, as far as I'm aware. But you can easily write such a method once and call it from wherever you want. For example:

public static <T> List<T> newArrayList(T value, int size) {
    List<T> list = new ArrayList<T>(size);
    for (int i = 0; i < size; i++) {
        list.add(value);
    }
    return list;
}

If you never want to change the size of the list (i.e. add or remove elements), Mike Samuel's answer is probably more efficient. Also note that if you're using a mutable type, you may not get what you want:

List<StringBuilder> list = newArrayList(new StringBuilder(), 10);
list.get(0).append("Foo");
System.out.println(list.get(5)); // Prints "Foo"

... as each element in the list will be a reference to the same object.

Upvotes: 2

amlane86
amlane86

Reputation: 678

I would stick with the for loop.

BTW... 0*10 = 0 so just enter in the amount you need instead

ArrayList<Integer> list = new ArrayList<Integer>(10);

Upvotes: -2

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

You can try:

List<Integer> list1 = Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

There are 10 zeroes. You need to know the number of elements at the compile time, but you have only one line. If you don't know the number of elements at compile time, then you can use the suggested Arrays.fill() approach.

Upvotes: 3

Mark Byers
Mark Byers

Reputation: 838806

Maybe you just need an array?

int[] array = new int[10];

You need a list if you need to change the size of it dynamically. If you don't need this feature, an array may suit your needs, and it will automatically initialize all the values to 0 for you.

Upvotes: 3

Mike Samuel
Mike Samuel

Reputation: 120546

Arrays.fill lets you avoid the loop.

Integer[] integers = new Integer[10];
Arrays.fill(integers, 0);
List<Integer> integerList = Arrays.asList(integers);

Upvotes: 29

cdeszaq
cdeszaq

Reputation: 31300

In Java, not really. Java is relatively verbose when it comes to this sort of stuff, so there isn't much you can do that is simple, other than a loop like you have.

Upvotes: -2

Related Questions