How do I shuffle elements of an array?

package org.kodejava.util;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ArrayShuffle {
    public static void main(String[] args) {
        // Initialize the contents of our array
        String[] alphabets = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};

        // As the Collections.shuffle() method need a list for the parameter
        // we convert our array into List using the Arrays class.
        List<String> list = Arrays.asList(alphabets);

        // Here we just simply used the shuffle method of Collections class
        // to shuffle out defined array.
        Collections.shuffle(list);

        // Run the code again and again, then you'll see how simple we do
        // shuffling
        for (String alpha : list) {
            System.out.print(alpha + " ");
        }
    }
}

An example of the generated results are:

F H E A B I G J D C  

How do I create an encrypted string for a password?

You are creating a user management system that will keep user profile and their credential or password. For security reason you’ll need to protect the password, to do this you can use the MessageDigest provided by Java API to encrypt the password. The code example below show you an example how to use it.

package org.kodejava.security;

import java.security.MessageDigest;

public class EncryptExample {
    public static void main(String[] args) {
        String password = "secret";
        String algorithm = "SHA";

        byte[] plainText = password.getBytes();

        try {
            MessageDigest digest = MessageDigest.getInstance(algorithm);
            digest.reset();
            digest.update(plainText);
            byte[] encodedPassword = digest.digest();

            StringBuilder builder = new StringBuilder();
            for (byte b : encodedPassword) {
                if ((b & 0xff) < 0x10) {
                    builder.append("0");
                }
                builder.append(Long.toString(b & 0xff, 16));
            }

            System.out.println("Plain    : " + password);
            System.out.println("Encrypted: " + builder.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Here is the example of our encrypted password:

Plain    : secret
Encrypted: e5e9fa1ba31ecd1ae84f75caaa474f3a663f05f4

How do I use for-each in Java?

Using for-each command to iterate arrays or a list can simplified our code. Below is an example how to do it in Java. The first loop is for iterating array and the second for iterating a list containing a some names.

package org.kodejava.lang;

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

public class ForEachExample {
    public static void main(String[] args) {
        Integer[] numbers = {10, 100, 1000, 10000, 100000, 1000000};

        for (Integer i : numbers) {
            System.out.println("Number: " + i);
        }

        List<String> names = new ArrayList<>();
        names.add("Musk");
        names.add("Nakamoto");
        names.add("Einstein");

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

The result of the code snippet:

Number: 10
Number: 100
Number: 1000
Number: 10000
Number: 100000
Number: 1000000
Name: Musk
Name: Nakamoto
Name: Einstein

How do I convert string to an integer or number?

package org.kodejava.lang;

public class StringToInteger {
    public static void main(String[] args) {
        // Some random selected number, could representing a decimal,
        // hexadecimal or octal number.
        String myLuckyNumber = "13";

        // We convert a string to an integer by invoking parseInt() method
        // of the Integer class.
        int number = Integer.parseInt(myLuckyNumber);
        System.out.println("My lucky number is: " + number);

        // We can also converting a string representation of a number other
        // then the decimal base, for instance an hexadecimal by providing
        // the radix to the method.
        number = Integer.parseInt(myLuckyNumber, 16);
        System.out.println("My lucky number is: " + number);

        number = Integer.parseInt(myLuckyNumber, 8);
        System.out.println("My lucky number is: " + number);
    }
}

Our code results are:

My lucky number is: 13
My lucky number is: 19
My lucky number is: 11

How do I create a zip file?

package org.kodejava.util.zip;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZippingFileExample {
    public static void main(String[] args) {
        String source = "data.txt";
        String target = "data.zip";

        try (ZipOutputStream zos = 
                 new ZipOutputStream(new FileOutputStream(target));
             InputStream is = 
                 ZippingFileExample.class.getResourceAsStream("/" + source)) {
            if (is != null) {
                // Put a new ZipEntry in the ZipOutputStream
                zos.putNextEntry(new ZipEntry(source));

                int size;
                byte[] buffer = new byte[1024];

                // Read data to the end of the source file and write it
                // to the zip output stream.
                while ((size = is.read(buffer, 0, buffer.length)) > 0) {
                    zos.write(buffer, 0, size);
                }

                zos.closeEntry();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}