|
Fill Collection
The following example shows how to use Collections.fill API.
It replaces all of the elements of the specified list with the specified element.
|
package com.bethecoder.tutorials.utils.collections;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsFill {
public static void main (String [] args) {
List<String> strList = Arrays.asList(new String [] {
"OLD", "OLD", "OLD", "OLD"
});
System.out.println("Before fill : " + strList);
//Replaces all of the elements of the specified list with the specified element.
Collections.fill(strList, "NEW");
System.out.println("After fill : " + strList);
}
}
|
| |
It gives the following output,
Before fill : [OLD, OLD, OLD, OLD]
After fill : [NEW, NEW, NEW, NEW]
|
|