tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Collections > Fill Collection

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.

File Name  :  
com/bethecoder/tutorials/utils/collections/CollectionsFill.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
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]



 
  


  
bl  br