Java String RemoveIf And ReplaceAll Methods

What is RemoveIf And ReplaceAll java methods?

RemoveIf And ReplaceAll java methods mutate the collections on which they’re invoked. In other words, they change the collection itself, unlike stream operations, which produce a new (copied) result. Why would such methods be added? Modifying collections can be errorprone and verbose. So Java 8 added removeIf java and replaceAll java to help.

Java 8 introduced a couple of methods into the List and Set interfaces:

  • removeIf java removes element matching a predicate. It’s available on all classes that implement List or Set (and is inherited from the Collection interface).
  • replaceAll java is available on List and replaces elements using a (UnaryOperator) function.
  • sort is also available on the List interface and sorts the list itself.

Removeif Java:

Consider the following code, Removeif Java tries to remove transactions that have a reference code starting with a digit:

for (Transaction transaction : transactions) {
if(Character.isDigit(transaction.getReferenceCode().charAt(0))) {
transactions.remove(transaction);
}
}

Can you see the problem? Unfortunately, this code may result in a ConcurrentModificationException. Why? Under the hood, the for-each loop uses an Iterator object, so the code executed is as follows:

for (Iterator<Transaction> iterator = transactions.iterator();
iterator.hasNext(); ) {
Transaction transaction = iterator.next();
if(Character.isDigit(transaction.getReferenceCode().charAt(0))) {
transactions.remove(transaction);
}
}

Notice that two separate objects manage the collection:

  • The Iterator object, which is querying the source by using next() and hasNext()
  • The Collection object itself, which is removing the element by calling remove()

As a result, the state of the iterator is no longer synced with the state of the Removeif Java collection, and vice versa. To solve this problem, you have to use the Iterator object explicitly and call its remove() method:

for (Iterator<Transaction> iterator = transactions.iterator();
iterator.hasNext(); ) {
Transaction transaction = iterator.next();
if(Character.isDigit(transaction.getReferenceCode().charAt(0))) {
iterator.remove();
}
}

This code has become fairly verbose to write. This code pattern is now directly expressible with the Java 8 Removeif Java method, which is not only simpler but also protects you from these bugs. It takes a predicate indicating which elements to remove:

transactions.removeIf(transaction ->
Character.isDigit(transaction.getReferenceCode().charAt(0)));

Sometimes, though, instead of removing an element, you want to replace it. For this purpose, Java 8 added replaceAll.

ReplaceAll Java

The replaceAll java method on the List interface lets you replace each element in a list with a new one. Using the Streams API, you could solve this problem as follows:


Java Replaceall Example

referenceCodes.stream()
.map(code -> Character.toUpperCase(code.charAt(0)) +
code.substring(1))
.collect(Collectors.toList())
.forEach(System.out::println);

This java replaceall example code results in a new collection of strings, however. You want a way to update the existing collection. You can use a ListIterator object as follows (supporting a set() method to replace an element):

for (ListIterator<String> iterator = referenceCodes.listIterator();
iterator.hasNext(); ) {
String code = iterator.next();
iterator.set(Character.toUpperCase(code.charAt(0)) + code.substring(1));
}

As you can see, this code is fairly verbose. In addition, as we explained earlier, using Iterator objects in conjunction with collection objects can be error-prone by mixing iteration and modification of the collection. In Java 8, you can simply write

referenceCodes.replaceAll(code -> Character.toUpperCase(code.charAt(0)) +
code.substring(1));

You Can Read This Article for more information:

how to use factory methods to create java collections?

Keywords:

Removeif Java,ReplaceAll Java,replace all java,java replaceall example,java string replace all,java string replaceall example,