Java Code Examples for java.util.Collections.replaceAll()

The following are Jave code examples for showing how to use replaceAll() of the java.util.Collections class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: javaide   File: ReplaceAllVectorExample.java   View Source Code Vote up 5 votes
public static void main(String[] args) {

    //create a Vector object
    Vector v = new Vector();

    //Add elements to Vector
    v.add("A");
    v.add("B");
    v.add("A");
    v.add("C");
    v.add("D");

    System.out.println("Vector Contains : " + v);

    /*
      To replace all occurrences of specified element of Java Vector use,
      static boolean replaceAll(List list, Object oldVal, Object newVal) method
      of Collections class.

      This method returns true if the list contained one more elements replaced.

    */

    Collections.replaceAll(v, "A", "Replace All");

    System.out.println("After Replace All, Vector Contains : " + v);
  }
 
Example 2
Project: javaide   File: ReplaceAllArrayListExample.java   View Source Code Vote up 5 votes
public static void main(String[] args) {

    //create an ArrayList object
    ArrayList arrayList = new ArrayList();

    //Add elements to Arraylist
    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("A");
    arrayList.add("C");
    arrayList.add("D");

    System.out.println("ArrayList Contains : " + arrayList);

    /*
      To replace all occurrences of specified element of Java ArrayList use,
      static boolean replaceAll(List list, Object oldVal, Object newVal) method
      of Collections class.

      This method returns true if the list contained one more elements replaced.

    */

    Collections.replaceAll(arrayList, "A", "Replace All");

    System.out.println("After Replace All, ArrayList Contains : " + arrayList);
  }