How do I reverse the order of LinkedList elements?

To reverse the order of LinkedList elements we can use the reverse(List<?> list) static method of java.util.Collections class. Here is the example:

package org.kodejava.example.util;

import java.util.LinkedList;
import java.util.Collections;

public class LinkedListReverseOrder {
    public static void main(String[] args) {
        LinkedList<String> grades = new LinkedList<>();
        grades.add("A");
        grades.add("B");
        grades.add("C");
        grades.add("D");
        grades.add("E");
        grades.add("F");

        System.out.println("Output in original order:");
        System.out.println("=========================");
        for (String grade : grades) {
            System.out.println("Grade = " + grade);
        }

        // Reverse the element order in the linked list object.
        Collections.reverse(grades);

        System.out.println("Output in reverse order:");
        System.out.println("=========================");
        for (String grade : grades) {
            System.out.println("Grade = " + grade);
        }
    }
}

Here is the output of the program:

Output in original order:
=========================
Grade = A
Grade = B
Grade = C
Grade = D
Grade = E
Grade = F
Output in reverse order:
=========================
Grade = F
Grade = E
Grade = D
Grade = C
Grade = B
Grade = A

How do I determine whether a string is a palindrome?

This code checks a string to determine if it is a palindrome or not. A palindrome is a word, phrase, or sequence that reads the same backward as forward.

package org.kodejava.example.lang;

public class PalindromeChecker {

    public static void main(String[] args) {
        String text = "Sator Arepo Tenet Opera Rotas";

        PalindromeChecker checker = new PalindromeChecker();
        System.out.println("Is palindrome = " + checker.isPalindrome(text));
    }

    /**
     * This method checks the string for palindrome. We use StringBuilder to
     * reverse the original string.
     *
     * @param text
     * @return
     */
    private boolean isPalindrome(String text) {
        System.out.println("Original text = " + text);

        String reverse = new StringBuilder(text).reverse().toString();
        System.out.println("Reverse text  = " + reverse);

        // Compare the original text with the reverse one and ignoring its case
        return text.equalsIgnoreCase(reverse);
    }
}

How do I reverse array elements order?

In this example we are going to use the ArraysUtils helper class from Apache Commons Lang library to reverse the order of array elements. The method to reverse the order of array elements is ArrayUtils.reverse() method.

The ArrayUtils.reverse() method is overloaded so we can reverse other types of array such as java.lang.Object, long, int, short, char, byte, double, float and boolean.

package org.kodejava.example.commons.lang;

import org.apache.commons.lang.ArrayUtils;

public class ArrayReverseExample {

    public static void main(String[] args) {
        // Define colors array.
        String[] colors = {"Red", "Green", "Blue", "Cyan", "Yellow", "Magenta"};
        System.out.println(ArrayUtils.toString(colors));

        // Now we reverse the order of array elements. 
        ArrayUtils.reverse(colors);
        System.out.println(ArrayUtils.toString(colors));
    }
}

Here is the output of the code snippet above:

{Red,Green,Blue,Cyan,Yellow,Magenta}
{Magenta,Yellow,Cyan,Blue,Green,Red}

Maven Dependencies

<!-- http://repo1.maven.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6.jar -->
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>