Close

Java Collections - Collections.fill() Examples

Java Collections Java Java API 


Class:

java.util.Collections

java.lang.Objectjava.lang.Objectjava.util.Collectionsjava.util.CollectionsLogicBig

Method:

public static <T> void fill(List<? super T> list,
                            T obj)

Replaces all of the elements of the specified list with the specified element.


Examples


package com.logicbig.example.collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FillExample {

public static void main(String... args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
System.out.println(list);

Collections.fill(list, 100);
System.out.println(list);
}
}

Output

[1, 2, 3]
[100, 100, 100]




See Also