Java Collections replaceAll() Method



Description

The Java Collections replaceAll(List<T>, T, T) method is used to replace all occurrences of one specified value in a list with another.

Declaration

Following is the declaration for java.util.Collections.replaceAll() method.

public static <T> boolean replaceAll(List<T> list,T oldVal,T newVal)

Parameters

  • list − This is the list in which replacement is to occur.

  • oldVal − This is the old value to be replaced.

  • newVal − This is the new value with which oldVal is to be replaced.

Return Value

The method call returns 'true' if list contained one or more elements e such that (oldVal==null ? e==null : oldVal.equals(e)).

Exception

UnsupportedOperationException − This is thrown if the specified list or its list-iterator does not support the set operation.

Replacing All Instances of a Value with Given Value From a List of Integers Example

The following example shows the usage of Java Collection replaceAll(List, T, T) method. We've created a list object, populated it with some integers. Then its one of the value is replaced and then result is printed.

package com.tutorialspoint;

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

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(3,2,3,3,5));

      System.out.println("Initial collection value: " + list);
      // replace all 3 of this collection
      Collections.replaceAll(list, 3, 1);
      System.out.println("Final collection value: "+list);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial collection value: [3, 2, 3, 3, 5]
Final collection value: [1, 2, 1, 1, 5]

Replacing All Instances of a Value with Given Value From a List of Strings Example

The following example shows the usage of Java Collection replaceAll(List, T, T) method. We've created a list object, populated it with some strings. Then its one of the value is replaced and then result is printed.

package com.tutorialspoint;

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

public class CollectionsDemo {

   public static void main(String[] args) {
      List<String> list = new ArrayList<>(Arrays.asList("C","B","C","C"));

      System.out.println("Initial collection value: " + list);
      // replace C of this collection
      Collections.replaceAll(list, "C","A");
      System.out.println("Final collection value: "+list);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial collection value: [C, B, C, C]
Final collection value: [A, B, A, A]

Replacing All Instances of a Value with Given Value From a List of Objects Example

The following example shows the usage of Java Collection replaceAll(List, T, T) method. We've created a list object, populated it with some Student objects. Then its one of the value is replaced and then result is printed.

package com.tutorialspoint;

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

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam"), new Student(1, "Julie"), new Student(1, "Julie")));

      System.out.println("Initial collection value: " + list);
      // replace Julie with null in this collection
      Collections.replaceAll(list, new Student(1, "Julie"), null);
      System.out.println("Final collection value: "+list);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial collection value: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 1, Julie ], [ 1, Julie ]]
Final collection value: [null, [ 2, Robert ], [ 3, Adam ], null, null]
java_util_collections.htm
Advertisements