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.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.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 a text to be checked for palindrome.
     * @return <code>true</code> if a text is palindrome.
     */
    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 the 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 another type of array such as java.lang.Object, long, int, short, char, byte, double, float and boolean.

package org.kodejava.commons.lang;

import org.apache.commons.lang3.ArrayUtils;

public class ArrayReverseExample {

    public static void main(String[] args) {
        // Define the "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

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.13.0</version>
</dependency>

Maven Central

How do I reverse a string by word?

In the other examples on this website you might have seen how to reverse a string using StringBuffer, StringUtils from Apache Commons Lang library or using the CharacterIterator.

In this example you’ll see another way that you can use to reverse a string by word. Here we use the StringTokenizer and the Stack class.

package org.kodejava.util;

import java.util.Stack;
import java.util.StringTokenizer;

public class ReverseStringByWord {
    public static void main(String[] args) {
        // The string that we'll reverse
        String text = "Jackdaws love my big sphinx of quartz";

        // We use StringTokenize to get each word of the string. You might try
        // to use the String.split() method if you want.
        StringTokenizer st = new StringTokenizer(text, " ");

        // To reverse it we can use the Stack class, which implements the LIFO
        // (last-in-first-out).
        Stack<String> stack = new Stack<>();
        while (st.hasMoreTokens()) {
            stack.push(st.nextToken());
        }

        // Print each word in reverse order
        while (!stack.isEmpty()) {
            System.out.print(stack.pop() + " ");
        }
    }
}

How do I reverse a string using CharacterIterator?

In this example we use the java.text.CharacterIterator implementation class java.text.StringCharacterIterator to reverse a string. It’s done by reading a string from the last index up to the beginning of the string.

package org.kodejava.text;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

public class StringCharacterIteratorReverseExample {
    private static final String text = "Jackdaws love my big sphinx of quartz";

    public static void main(String[] args) {
        CharacterIterator it = new StringCharacterIterator(text);

        System.out.println("Before = " + text);
        System.out.print("After  = ");
        // Iterates a string from the last index to the beginning.
        for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it.previous()) {
            System.out.print(ch);
        }
    }
}

The result of the code snippet above:

Before = Jackdaws love my big sphinx of quartz
After  = ztrauq fo xnihps gib ym evol swadkcaJ