How do I use List.replaceAll() method?

The List.replaceAll() method was introduced in Java 8. This method replaces each element of the list with the result of applying the operator to that element. The operator or function you pass to replaceAll() should be a UnaryOperator.

Here is a simple example:

package org.kodejava.util;

import java.util.ArrayList;
import java.util.List;
import java.util.function.UnaryOperator;

public class ListReplaceAllExample {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);

        // Define an UnaryOperator to square each number
        UnaryOperator<Integer> square = n -> n * n;

        // Use replaceAll() method to square each number in the list
        numbers.replaceAll(square);

        System.out.println(numbers);
    }
}

Outputs:

[1, 4, 9, 16, 25]

In this example, the UnaryOperator square squares each element. The List.replaceAll() method applies this operator to all elements in the list.

Note that replaceAll() modifies the original list and does not return a new list. Please also be aware that this operation is in-place and hence modifies the original List. If you want to keep the original List unchanged, create a new List and add elements to it after applying the function.

The primary purpose of the List.replaceAll() method in Java is to perform an in-place transformation of all elements within a list based on a given unary function or operation.

A Unary function or operation is one that takes a single input and produces a result. In the context of replaceAll(), the unary operation is typically provided as a lambda expression or method reference which is applied to each element in the list in turn.

If successful, replaceAll() modifies the list such that each original element has been replaced by the result of applying the provided unary operation to that element. This operation is performed on the original list, and no new list is created, making it an efficient option for transforming large lists.

Here is an example which doubles each integer in a list:

package org.kodejava.util;

import java.util.ArrayList;
import java.util.List;

public class ListReplaceAllSecondExample {
    public static void main(String[] args) {
        List<Integer> ints = new ArrayList<>();

        ints.add(1);
        ints.add(2);
        ints.add(3);

        // Double every integer in the List
        ints.replaceAll(n -> n * 2);

        System.out.println(ints); 
    }
}

Outputs:

[2, 4, 6]

In conclusion, List.replaceAll() provides a convenient and efficient way to modify all elements in a list according to a specified operation or function. It’s especially useful when using the Streams API and functional programming techniques introduced in Java 8.

How do I use Collection.removeIf() method?

The Collection.removeIf() method was introduced in Java 8, and it allows for the removal of items from a collection using a condition defined in a lambda expression.

The primary purpose of the Collection.removeIf() method in Java is to filter out elements from a collection based on a certain condition or predicate. It’s a more efficient and concise way of performing this type of operation than traditional for or iterator-based loops.

The method iterates over each element in the collection and checks whether it satisfies the condition described by the given Predicate. If the Predicate returns true for a particular element, removeIf() removes that element from the collection.

Here’s a simple example:

package org.kodejava.util;

import java.util.ArrayList;
import java.util.List;

public class CollectionRemoveIfExample {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);

        // Use removeIf method to remove all numbers greater than 2
        numbers.removeIf(n -> n > 2);

        System.out.println(numbers); // Outputs: [1, 2]
    }
}

In this example, n -> n > 2 is a lambda expression that defines a Predicate, which returns true for all numbers greater than 2. The removeIf() method uses this Predicate to determine which elements to remove.

Please be aware that not all Collection implementations support the removeIf() method. For example, if you try to use it with an unmodifiable collection (like the ones returned by Collections.unmodifiableList()), it will throw an UnsupportedOperationException.

As removeIf() is a default method, it’s provided with a default implementation, and it’s available for use with any classes that implement the Collection interface (like ArrayList, HashSet, etc.) without requiring those classes to provide their own implementation.

However, classes can still override this method with their own optimized version if necessary. Here’s another example of removeIf() method:

package org.kodejava.util;

import java.util.ArrayList;
import java.util.List;

public class CollectionRemoveIfSecond {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        names.add("David");
        names.add("Rosa");

        // Remove names that start with 'B'
        names.removeIf(name -> name.startsWith("B"));

        System.out.println(names); // Outputs: [Alice, Charlie, David, Rosa]
    }
}

Remember, it’s a bulk operation that can lead to a ConcurrentModificationException if the collection is modified while the operation is running (for example, removing an element from a collection while iterating over it with removeIf()), except if the collection is a Concurrent Collection.

In conclusion, the Collection.removeIf() default method provides a unified, efficient, and convenient way to remove items from a collection based on certain conditions.

What is the purpose of String.strip() method of Java 11?

The purpose of the String.strip() method in Java 11 is to remove whitespaces from both the beginning and end of a string. This is very similar to the String.trim() method available in earlier versions of Java, but there is a key difference between them.

Here’s the difference:

  • String.strip(): Introduced in Java 11, strip() uses the unicode definition of whitespace. It removes not only space characters but also all other types of unicode-defined spaces, such as the thin space \u2009, etc.
  • String.trim(): Available from Java 1.0, trim() is more limited. It considers a whitespace to be any character whose ASCII value is less than or equal to 32 (a space, tab, newline, and a few other control characters).

Here are examples of how they work:

package org.kodejava.lang;

public class StringStripExample {
    public static void main(String[] args) {
        // String.strip()
        String first = " \u2009Hello  ";
        System.out.println(first.strip()); // Outputs "Hello"

        // String.trim()
        String second = " \u2009Hello  ";
        System.out.println(second.trim()); // Outputs "\u2009Hello"
    }
}

Output:

Hello
 Hello

Thus, strip() method is more comprehensive in removing different types of whitespace defined in Unicode, while trim() only removes ASCII control characters and spaces.

There are also String.stripLeading() and String.stripTrailing() methods that were introduced in Java 11, and they are similar to the strip() method, but they only remove the whitespace characters from either the beginning or the end of the string, respectively.

Here is what they do:

  • String.stripLeading(): This method removes any leading whitespace from the string. “Leading” in this context means any whitespace characters at the beginning of the string.
  • String.stripTrailing(): This method removes any trailing whitespace from the string. “Trailing” in this context means any whitespace characters at the end of the string.

Both stripLeading() and stripTrailing() use the Unicode definition of whitespace, the same as strip() method.

Here are examples of how they work:

package org.kodejava.lang;

public class StringStripLeadingTrailingExample {
    public static void main(String[] args) {
        // Strip leading whitespace
        String first = " \u2009Hello World  ";
        System.out.println(first.stripLeading());  // Outputs "Hello World  "

        // Strip trailing whitespace
        String second = " \u2009Hello World  ";
        System.out.println(second.stripTrailing()); // Outputs " \u2009Hello World"
    }
}

Output:

Hello World  
  Hello World

As demonstrated, stripLeading() removed the whitespace characters from the front of the string, and stripTrailing() removed the whitespace characters from the end of the string.

While \u00A0 is technically a type of whitespace (specifically, a non-breaking space or NBSP), it isn’t considered as such by the strip(), stripLeading(), and stripTrailing() methods, which follow the Character.isWhitespace(char) method’s definition of what constitutes a whitespace character.

According to the Java documentation, the Character.isWhitespace(char) method, which the strip() methods use, considers the following characters as whitespace:

  • ‘\t’ U+0009 HORIZONTAL TABULATION
  • ‘\n’ U+000A LINE FEED
  • ‘\u000B’ U+000B VERTICAL TABULATION
  • ‘\f’ U+000C FORM FEED
  • ‘\r’ U+000D CARRIAGE RETURN
  • ‘\u001C’ U+001C FILE SEPARATOR
  • ‘\u001D’ U+001D GROUP SEPARATOR
  • ‘\u001E’ U+001E RECORD SEPARATOR
  • ‘\u001F’ U+001F UNIT SEPARATOR
  • SPACE_SEPARATOR category types

The \u2009 (thin space) and \u0020 (space) are part of SPACE_SEPARATOR category according to Unicode standard and will be correctly stripped.

The \u00A0 (non-breaking space) is actually part of a different category called the NO-BREAK_SPACE and is not considered whitespace by Character.isWhitespace(char), so it won’t be stripped.

How do I use Map.of() factory method to create a map object?

In Java, the Map.of() factory method can be used to create an unmodifiable map of specified key-value pairs. This method is available in Java 9 and later versions.

Creating a map is a bit more complicated than creating lists or sets. Because we need to provide keys and values when creating a map. When using the Map.of() factory method we set the content of the map by alternating between the keys and values of the map.

Consider the following example:

package org.kodejava.util;

import java.util.Map;

public class MapOfExample {
    public static void main(String[] args) {
        Map<String, Integer> map = Map.of("John", 25, "Mary", 30, "Alice", 27, "Rosa", 22);

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}

Output:

Rosa : 22
Mary : 30
John : 25
Alice : 27

In the example above, the Map.of("John", 25, "Mary", 30, "Alice", 27, "Rosa", 22) statement creates an unmodifiable map with three key-value pairs. After the map is created, any attempt to modify the map (add, update or remove elements) will throw an UnsupportedOperationException.

Note that Map.of() doesn’t accept null keys or values. If a null key or value is provided, then a NullPointerException is thrown. Besides, if duplicate keys are provided, an IllegalArgumentException is thrown.

The Map.of() method is overloaded to accept up to 10 key-value pairs. If there are more than 10 pairs, you can use Map.ofEntries() factory method to create a map. This is how we use it:

Map<String, Integer> map = Map.ofEntries(
    Map.entry("John", 25),
    Map.entry("Mary", 30),
    Map.entry("Alice", 27),
    Map.entry("Bob", 32),
    // ...
);

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + " : " + entry.getValue());
}

Map.entry() is another factory method provided to create Map.Entry object.

How do I use Set.of() factory method to create a set object?

As with List.of(), in Java 9, the Set.of() factory method can be used to create an unmodifiable set of specified elements.

Here is a simple example:

package org.kodejava.util;

import java.util.Set;

public class SetOfExample {
    public static void main(String[] args) {
        Set<String> names = Set.of("Rosa", "John", "Mary", "Alice");

        for (String name : names) {
            System.out.println(name);
        }
    }
}

Output:

John
Rosa
Alice
Mary

In this example, the Set.of("Rosa", "John", "Mary", "Alice") statement creates an unmodifiable set of strings containing “Rosa”, “John”, “Mary”, and “Alice”. The resulting set is unmodifiable, so attempting to add, update, or remove elements from it will throw an UnsupportedOperationException.

If you try to create a Set by providing a duplicate elements, an IllegalArgumentException will be thrown. A Set is a type of collection container that cannot have duplicate values in it.

Note that the Set.of() method doesn’t accept null values. If you try to insert a null value, it will throw a NullPointerException. If you add a null value using the add() method UnsupportedOperationException will be thrown.

Set.of() is overloaded similarly to List.of(), allowing you to create a set with varying numbers of elements. The below examples demonstrate the use of Set.of() with different numbers of arguments:

Set<String> a = Set.of(); // An empty set
Set<String> b = Set.of("One"); // A set with one element
Set<String> c = Set.of("One", "Two"); // A set with two elements
// ...
Set<String> j = Set.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"); // A set with ten elements

If you need to create a set with more than 10 elements, Set.of() offers an overloaded version that accepts an array or varargs:

Set<String> set = Set.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven");

Remember that sets created with Set.of() are unmodifiable. Attempting to add, remove or change an element in these sets after their creation causes an UnsupportedOperationException.

Also, Set.of() doesn’t allow duplicate or null elements. If you pass duplicate or null values, it will throw IllegalArgumentException and NullPointerException respectively.