Wednesday, 1 July 2015

Java : Collection Framework : Collections (ReplaceAll)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=DGe2SigO0B8&list=UUhwKlOVR041tngjerWxVccw

CollectionsExample.java
import java.util.ArrayList;
import java.util.Collections;

/*
 *  Example of  
 *  
 *      replaceAll(List<T> list,T oldVal,T newVal) method
 *   
 *  Parameters:  
 *  
 *      list - the list in which replacement is to occur.
 *      oldVal - the old value to be replaced.
 *      newVal - the new value with which oldVal is to be replaced. 
 *
 *  Returns:
 *
 *      true if old values are replaced with new values.
 *  
 */
public class CollectionsExample
{

    public static void main(String[] args)
    {

        ArrayList<Integer> arrayList = new ArrayList<Integer>();

        arrayList.add(2000);
        arrayList.add(3000);
        arrayList.add(4000);
        arrayList.add(1000);
        arrayList.add(2000);
        arrayList.add(2000);

        System.out.println("arrayList : " + arrayList + "\n");

        /*
         * Replaces all occurrences of one specified value in a list with
         * another.
         */

        boolean isReplaced = Collections.replaceAll(arrayList, 2000, 10);

        System.out.println("isReplaced : " + isReplaced + "\n");
        
        System.out.println("After Replaced , arrayList : " + arrayList);

    }

}
Output
arrayList : [2000, 3000, 4000, 1000, 2000, 2000]

isReplaced : true

After Replaced , arrayList : [10, 3000, 4000, 1000, 10, 10]
To Download CollectionsDemoReplaceAll Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionsDemoReplaceAll.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge