How do I generate a random array of numbers?

Using java.util.Random class we can create random data such as boolean, integer, floats, double. First you’ll need to create an instance of the Random class. This class has some next***() methods that can randomly create the data.

package org.kodejava.util;

import java.util.Arrays;
import java.util.Random;

public class RandomDemo {
    public static void main(String[] args) {
        Random r = new Random();

        // generate some random boolean values
        boolean[] booleans = new boolean[10];
        for (int i = 0; i < booleans.length; i++) {
            booleans[i] = r.nextBoolean();
        }
        System.out.println(Arrays.toString(booleans));

        // generate a uniformly distributed int random numbers
        int[] integers = new int[10];
        for (int i = 0; i < integers.length; i++) {
            integers[i] = r.nextInt();
        }
        System.out.println(Arrays.toString(integers));

        // generate a uniformly distributed float random numbers
        float[] floats = new float[10];
        for (int i = 0; i < floats.length; i++) {
            floats[i] = r.nextFloat();
        }
        System.out.println(Arrays.toString(floats));

        // generate a Gaussian normally distributed random numbers
        double[] doubles = new double[10];
        for (int i = 0; i < doubles.length; i++) {
            doubles[i] = r.nextGaussian();
        }
        System.out.println(Arrays.toString(doubles));
    }
}

The result of the code snippet above are:

[true, true, false, true, false, true, true, false, false, true]
[34195704, 1462972230, -475641915, -1531017612, 332184915, 1555901473, -276309016, 1433394157, -451221924, -1178823255]
[0.3040716, 0.28814012, 0.34028566, 0.021003246, 0.49158317, 0.37954164, 0.5403056, 0.54187375, 0.7157934, 0.5964742]
[1.051268591002577, -1.1283332046139989, 0.8351395528722437, -0.24598168031182968, 0.7123515366756693, -0.9661681996105319, -1.6107009669125059, 0.43994917963255387, 1.1309345726165914, 1.2321618489324313]

For an example to create random number using the Math.random() method see How do I create random number?.

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 create a queue using LinkedList class?

package org.kodejava.util;

import java.util.LinkedList;
import java.util.Queue;

public class QueueDemo {
    public static void main(String[] args) {
        // Create an instance of a queue, ere we use the LinkedList class which
        // implements the Queue interface. Add some elements to the queue using
        // the offer method.
        Queue<String> queue = new LinkedList<>();
        queue.offer("First visitor");
        queue.offer("Second visitor");
        queue.offer("Third visitor");
        queue.offer("Fourth visitor");

        // Let see the size of our queue
        System.out.println("Size: " + queue.size());

        // The peek and element method read the head of the queue without removing
        // the element. The difference is, if the queue is empty peek method
        // return null while element method throws a NoSuchElementException
        // exception.
        System.out.println("Queue head using peek   : " + queue.peek());
        System.out.println("Queue head using element: " + queue.element());

        // The poll method retrieves and then removes the head of the queue.
        // On the next code will process all the element of the queue. When no
        // item in the queue the poll method will return null.
        Object data;
        while ((data = queue.poll()) != null) {
            System.out.println(data);
        }
    }
}

The code snippet above print the following output:

Size: 4
Queue head using peek   : First visitor
Queue head using element: First visitor
First visitor
Second visitor
Third visitor
Fourth visitor

How do I use the Stack class in Java?

Stack is an extension of the java.util.Vector class that provided a LIFO (last-in-first-out) data structure. This class provide the usual method such as push() and pop(). The peek method is used the get the top element of the stack without removing the item.

package org.kodejava.util;

import java.util.Stack;

public class StackExample {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();

        // We stored some values in the stack object.
        for (int i = 0; i < 10; i++) {
            stack.push(i);
            System.out.print(i + " ");
        }
        System.out.println();

        // Searching for an item in the stack. The position returned
        // as the distance from the top of the stack. Here we search
        // for the 3 number in the stack which is in the 7th row of
        // the stack.
        int position = stack.search(3);
        System.out.println("Search result position: " + position);

        // The current top value of the stack
        System.out.println("Stack top: " + stack.peek());

        // Here we're popping out all the stack object items.
        while (!stack.empty()) {
            System.out.print(stack.pop() + " ");
        }
    }
}

The result of the code snippet:

0 1 2 3 4 5 6 7 8 9 
Search result position: 7
Stack top: 9
9 8 7 6 5 4 3 2 1 0 

How do I convert string date to long value?

package org.kodejava.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringDateToLong {
    public static void main(String[] args) {
        // Here we have a string date, and we want to covert it to long value
        String today = "25/09/2021";

        // Create a SimpleDateFormat which will be used to convert the string to
        // a date object.
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        try {
            // The SimpleDateFormat parse the string and return a date object.
            // To get the date in long value just call the getTime method of
            // the Date object.
            Date date = formatter.parse(today);
            long dateInLong = date.getTime();

            System.out.println("Date         = " + date);
            System.out.println("Date in Long = " + dateInLong);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

The result of the code snippet:

Date         = Sat Sep 25 00:00:00 CST 2021
Date in Long = 1632499200000