How do I create a LinkedList?

A linked list is a fundamental data structure in programming. It can store data and a references to the next and/or to the previous node. There can be a singly linked list, a doubly linked list and a circularly linked list.

The code below show you how to use the java.util.LinkedList class to create a linked list. In the example we create a LinkedList to store a string object using the add(Object o) method. After create the list we iterate the list and print out the contents.

package org.kodejava.util;

import java.util.List;
import java.util.LinkedList;

public class LinkedListCreate {
    public static void main(String[] args) {
        // Creates a new instance of java.util.LinkedList and
        // adds five string object into the list.
        List<String> grades = new LinkedList<>();
        grades.add("A");
        grades.add("B");
        grades.add("C");
        grades.add("E");
        grades.add("F");

        // Iterates the LinkedList object using the for each
        // statement.
        for (String grade : grades) {
            System.out.println("Grade: " + grade);
        }
    }
}

This program will produce the following output:

Grade: A
Grade: B
Grade: C
Grade: E
Grade: F

How do I count number of weekday between two dates?

The code below helps you find the number of a specified weekday (Monday, Tuesday, Wednesday, etc.) between two dates. The solution we used below is to loop between the two dates and check if the weekday of those dates are equals to the day we want to count.

package org.kodejava.util;

import java.util.Calendar;

public class DaysBetweenDate {
    public static void main(String[] args) {
        Calendar start = Calendar.getInstance();
        start.set(2021, Calendar.OCTOBER, 1);
        Calendar end = Calendar.getInstance();
        end.set(2021, Calendar.OCTOBER, 31);

        System.out.print("Number Monday between " +
                start.getTime() + " and " + end.getTime() + " are: ");

        int numberOfDays = 0;
        while (start.before(end)) {
            if (start.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
                numberOfDays++;
                start.add(Calendar.DATE, 7);
            } else {
                start.add(Calendar.DATE, 1);
            }
        }

        System.out.println(numberOfDays);
    }
}

The result of our program is:

Number Monday between Fri Oct 01 07:55:06 CST 2021 and Sun Oct 31 07:55:06 CST 2021 are: 4

How do I get the last element of SortedSet?

package org.kodejava.util;

import java.util.SortedSet;
import java.util.TreeSet;

public class LastSetElement {
    public static void main(String[] args) {
        SortedSet<String> numbers = new TreeSet<>();
        numbers.add("One");
        numbers.add("Two");
        numbers.add("Three");
        numbers.add("Four");
        numbers.add("Five");

        // SortedSet orders the items it contains. We can get the last
        // item from the set using the last() method. The last item 
        // will be "Two".
        String lastElement = numbers.last();
        System.out.println("lastElement = " + lastElement);
    }
}

How do I get the first element of SortedSet?

package org.kodejava.util;

import java.util.TreeSet;
import java.util.SortedSet;

public class FirstSetElement {
    public static void main(String[] args) {
        SortedSet<String> numbers = new TreeSet<>();
        numbers.add("One");
        numbers.add("Two");
        numbers.add("Three");
        numbers.add("Four");
        numbers.add("Five");

        // SortedSet orders the items it contains. We can get the first
        // item from the SortedSet using the first() method. The fist item 
        // will be "Five".
        String firstElement = numbers.first();
        System.out.println("firstElement = " + firstElement);
    }
}

How do I generate UUID / GUID in Java?

UUID / GUID (Universally / Globally Unique Identifier) is frequently use in programming. Some of its usage are for creating random file names, session id in web application, transaction id and for record’s primary keys in database replacing the sequence or auto generated number.

To generate UUID in Java we can use the java.util.UUID class. This class was introduced in JDK 1.5. The UUID.randomUUID() method return a UUID object. To obtain the value of the random string generated we need to call the UUID.toString() method.

We can also get the version and the variant of the UUID using the version() method and variant() method respectively. Let’s see the code snippet below:

package org.kodejava.util;

import java.util.UUID;

public class RandomStringUUID {
    public static void main(String[] args) {
        // Creating a random UUID (Universally unique identifier).
        UUID uuid = UUID.randomUUID();
        String randomUUIDString = uuid.toString();

        System.out.println("Random UUID String = " + randomUUIDString);
        System.out.println("UUID version       = " + uuid.version());
        System.out.println("UUID variant       = " + uuid.variant());
    }
}

The result of our program is:

Random UUID String = 87a20cdd-25da-4dc2-b787-68054ec2c5ca
UUID version       = 4
UUID variant       = 2