How do I use List.of() factory method to create a list object?

In Java, you can use the List.of() factory method to create an unmodifiable List consisting of specified elements. This method is available from Java 9 onwards.

Here is a simple example:

package org.kodejava.util;

import java.util.List;

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

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

        names.add("Bob"); // throws java.lang.UnsupportedOperationException
    }
}

In the code above, we have created a list of names including “Rosa”, “John”, “Mary”, and “Alice”. This newly created list is unmodifiable, so attempting to add, update, or remove elements from it will throw an UnsupportedOperationException.

There are several overloaded versions of the List.of() method that each accept different numbers of arguments. The versions range from no argument (which creates an empty list) to 10 explicit arguments of type E. Here’s an example:

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

However, if we need to create a list with more than 10 elements, we have another overloaded version of List.of() method which accepts an array or varargs.

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

Remember that these lists are unmodifiable. That means, if you try to modify the list (add, update, or remove elements) after they have been created, an UnsupportedOperationException will be thrown. Also, List.of() doesn’t allow null elements. If you pass null, it will throw UnsupportedOperationException.

How does Base64 encoding work?

Base64 encoding scheme is designed to encode binary data, especially when that data needs to be stored and transferred over media that designed to deal with text. Base64 encoding helps to ensure that the data remains intact without modification during transport.

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. Base64 is used commonly in a number of applications including email via MIME (Multipurpose Internet Mail Extensions), and storing complex data in XML or JSON.

Radix-64 refers to a numeral system that uses 64 unique characters to represent data. In the case of Base64 encoding, the 64 characters are typically the uppercase letters AZ, the lowercase letters az, the numerals 09, and an additional two characters, usually + and /. These characters are used to encode data into a text format that can be safely transferred over various systems designed to handle text data.

Here’s a brief overview of how the Base64 encoding process works:

  • Take the input data (which is binary data). For example, we have the text “Hello”. Each character is converted into their ASCII code.
  • Split the input data into chunks of 24 bits (3 bytes). If the input is not divisible by 24, it is padded with zeros on the right to make up a full chunk.
ASCII H = 72 e = 101 l = 108 l = 108 o = 111
Binary 01001000 01100101 01101100 01101100 01101111
  • Each chunk of 24 bits is then split into four chunks of 6 bits.
6-bits 010010 000110 010101 101100
Value 18 6 21 44
6-bits 011011 000110 111100 [00 = PAD]
Value 27 6 60
  • Each 6-bit chunk is then mapped to an encoded character using an index table (below), which is a set of 64 distinct characters—these are A–Z, a–z, 0–9, + and / in the Base64 alphabet.
Value 18 6 21 44 27 6 60
Encoding S G V s b G 8
  • If the last 8-bit block (third block for normal Base64 encoding) had the padding zero bits, the output of the corresponding 6-bit block (fourth block for normal Base64 encoding) is replaced with one = symbol. If the second 8-bit block also had padding zeros, then fourth, and third blocks of Base64 encoding will have = symbols.
Value 18 6 21 44 27 6 60 PAD
Encoding S G V s b G 8 =
  • The output is the encoded string.
SGVsbG8=

This process can be reversed to decode a Base64 string back into the original binary data.

Do note, though, while Base64 can encode any binary data, it is not encryption nor should it be used for encryption purposes – it does not hide or secure information, it’s merely an encoding scheme.

Base64 Index Table

Value Encoding Value Encoding Value Encoding Value Encoding
0 A 16 Q 32 g 48 w
1 B 17 R 33 h 49 x
2 C 18 S 34 i 50 y
3 D 19 T 35 j 51 z
4 E 20 U 36 k 52 0
5 F 21 V 37 l 53 1
6 G 22 W 38 m 54 2
7 H 23 X 39 n 55 3
8 I 24 Y 40 o 56 4
9 J 25 Z 41 p 57 5
10 K 26 a 42 q 58 6
11 L 27 b 43 r 59 7
12 M 28 c 44 s 60 8
13 N 29 d 45 t 61 9
14 O 30 e 46 u 62 +
15 P 31 f 47 v 63 /

Differences between getEncoder() and getUrlEncoder() method of java.util.Base64 class

The java.util.Base64.getEncoder() and java.util.Base64.getUrlEncoder() methods in Java both return a Base64 encoder. However, they exhibit different behaviors mainly due to the encoding scheme they follow for handling URL and filename safe characters.

  • java.util.Base64.getEncoder(): This method returns a Base64.Encoder that encodes using the Basic type base64 encoding scheme as per RFC 4648 Section 4.

Basic Base64 encoding does not handle URL and filename safe characters well. It uses + for 62, / for 63, and = for padding. These characters can cause problems in URLs.

An example of its usage.

package org.kodejava.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64EncoderExample {
    public static void main(String[] args) {
        Base64.Encoder encoder = Base64.getEncoder();
        String str = "https://www.google.com/search?q=Hello+World";
        String encodedString = encoder.encodeToString(str.getBytes(StandardCharsets.UTF_8));
        System.out.println("encodedString = " + encodedString);
    }
}

Output:

encodedString = aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9zZWFyY2g/cT1IZWxsbytXb3JsZA==
  • java.util.Base64.getUrlEncoder(): This method returns a Base64.Encoder that encodes in URL and filename safe type base64 scheme as per RFC 4648 Section 5.

URL and filename safe Base64 encoding replaces + with - , and / with _. It also omits padding characters. This makes it safer for use in URLs or file-system paths.

An example of its usage.

package org.kodejava.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64UrlEncoderExample {
    public static void main(String[] args) {
        Base64.Encoder urlEncoder = Base64.getUrlEncoder();
        String str = "https://www.google.com/search?q=Hello+World";
        String urlEncodedString = urlEncoder.encodeToString(str.getBytes(StandardCharsets.UTF_8));
        System.out.println("encodedString = " + urlEncodedString);
    }
}

Output:

encodedString = aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9zZWFyY2g_cT1IZWxsbytXb3JsZA==

So the choice between these two would depend on where you intend to use the encoded bytes. If it’s to be in a URL or a filepath, it’s recommended to use the getUrlEncoder() due to its url safe encoding scheme. For other use cases, the getEncoder() method should suffice.

Similar to the getEncoder() and getUrlEncoder() methods, the java.util.Base64 class provides the getDecoder() and getUrlDecoder() methods for Base64 decoding.

  • java.util.Base64.getDecoder(): This method returns a Base64.Decoder that decodes using the Basic type base64 encoding scheme.

Example:

package org.kodejava.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64DecodeExample {
    public static void main(String[] args) {
        Base64.Decoder decoder = Base64.getDecoder();

        // "Hello, World!" in Base64
        String str = "SGVsbG8sIFdvcmxkIQ==";
        String decodedString = new String(decoder.decode(str), StandardCharsets.UTF_8);
        System.out.println("decodedString = " + decodedString);
    }
}

Output:

decodedString = Hello, World!
  • java.util.Base64.getUrlDecoder(): This method returns a Base64.Decoder that decodes using the URL and Filename safe type base64 encoding scheme.

Example:

package org.kodejava.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64UrlDecoderExample {
    public static void main(String[] args) {
        Base64.Decoder urlDecoder = Base64.getUrlDecoder();

        // "Hello, World!" in URL-safe Base64
        String urlSafeStr = "SGVsbG8sIFdvcmxkIQ";
        String decodedString = new String(urlDecoder.decode(urlSafeStr), StandardCharsets.UTF_8);
        System.out.println("decodedString = " + decodedString);
    }
}

Output:

decodedString = Hello, World!

The choice between these two would again depend on from where you are getting the encoded bytes. If it’s from a URL or a filepath, you would want to use getUrlDecoder(). For other use cases, the getDecoder() method should suffice.

How do I use map() method of Optional object?

The map method of the Optional class in Java is used to transform the value contained in the Optional. map allows you to apply a function on the value inside the Optional and returns an Optional that contains the result of the function.

Here is an example of how to use it:

package org.kodejava.util;

import java.util.Optional;

public class OptionalMapExample {
    public static void main(String[] args) {

        // Create an Optional<String>
        Optional<String> optional = Optional.of("Hello");

        // Use map method to transform the contained value
        Optional<Integer> transformedOptional = optional.map(String::length);

        // Use ifPresent to print the result if the Optional is not empty
        transformedOptional.ifPresent(System.out::println);
    }
}

In this example, we start with an Optional<String> that contains the string “Hello”. We then use map to apply the String::length method on the contained string. This transforms the Optional<String> into an Optional<Integer>, where the integer is the length of the string.

Lastly, we use ifPresent to print the result. In this case, the integer 5 will be printed.

Here is another example, where map helps us to handle null values:

Optional<String> optional = Optional.ofNullable(null);

// If optional is not present, it will print "0"
System.out.println(optional.map(String::length).orElse(0));

In this case, trying to apply String::length on a null value would result in a NullPointerException. However, using map in combination with Optional, allows us to safely transform the value and even provide a default result (“0” in this case) if the Optional is empty. This makes handling null values more reliable and your code less error-prone.

How do I use flatMap() method of Optional object?

The flatMap method is a special method in the Optional class in Java, if a method returns an Optional, you can use flatMap to avoid nested Optional<Optional<T>> situations.

Here is an example:

package org.kodejava.util;

import java.util.Optional;

public class OptionalFlatMap {
    public static void main(String[] args) {
        Optional<String> nonEmptyGender = Optional.of("male");
        Optional<String> emptyGender = Optional.empty();

        System.out.println("Non-Empty Optional:: " + nonEmptyGender.flatMap(OptionalFlatMap::getGender));
        System.out.println("Empty Optional:: " + emptyGender.flatMap(OptionalFlatMap::getGender));
    }

    static Optional<String> getGender(String gender) {
        if (gender.equals("male")) {
            return Optional.of("Gender is male");
        } else if (gender.equals("female")) {
            return Optional.of("Gender is female");
        } else {
            return Optional.empty();
        }
    }
}

In this example, two Optional<String> objects are created: one with a value (nonEmptyGender) and one without a value (emptyGender).

The flatMap method is used to apply the method getGender to the value of each Optional<String> (if it exists). Since getGender returns an Optional<String>, using flatMap avoids creating Optional<Optional<String>> objects, and instead directly returns an Optional<String>, that we can easily consume.

The getGender method returns an Optional object, that describes the gender if it is “male” or “female”, or an empty Optional if the gender is neither “male” nor “female”.

The result of calling flatMap will hence be an Optional<String> describing the gender if the gender is “male” or “female”, or an empty Optional in all other cases. This applies to both the non-empty and the empty Optional<String> in the example.

The final output will be:

Non-Empty Optional:: Optional[Gender is male]
Empty Optional:: Optional.empty

In both cases, note that flatMap directly returns the result of getGender, which itself is an Optional. This is different from if map was used, which would have resulted in a nested Optional.