How do I remove the first or the last found element from Deque?

This example demonstrates the use of Deque.removeFirstOccurrence() method call to remove the first occurrences of an element in the Deque object and Deque.removeLastOccurrence() method call to remove the last occurrences of an element in the Deque object.

package org.kodejava.util;

import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;

public class DequeRemoveDemo {
    public static void main(String[] args) {
        Deque<String> deque1 = new LinkedList<>();
        deque1.offer("a");
        deque1.offer("b");
        deque1.offer("c");
        deque1.offer("d");
        deque1.offer("e");
        deque1.offer("d");

        Deque<String> deque2 = new LinkedList<>(deque1);

        // Removes the first occurrence of letter "d" from deque1
        deque1.removeFirstOccurrence("d");

        Iterator<String> first = deque1.iterator();
        System.out.println("After removeFirstOccurrence(Object o): ");
        System.out.println("=======================================");
        while (first.hasNext()) {
            System.out.println(first.next());
        }

        // Removes the last occurrence of letter "d" from deque2
        deque2.removeLastOccurrence("d");
        Iterator<String> last = deque2.iterator();
        System.out.println("After removeLastOccurrence(Object o): ");
        System.out.println("========================================");
        while (last.hasNext()) {
            System.out.println(last.next());
        }
    }
}

The output of the code snippet above is:

After removeFirstOccurrence(Object o): 
=======================================
a
b
c
e
d
After removeLastOccurrence(Object o): 
========================================
a
b
c
d
e

How do I create a Last-In-First-Out Deque?

This example show you how to create a LIFO (Last In First Out) Deque. We call the Deque.peekLast() method to get the last element from the Deque, poll the last element and repeat until all the elements read.

package org.kodejava.util;

import java.util.Deque;
import java.util.LinkedList;

public class DequeLifoDemo {

    public static void main(String[] args) {
        // Create an instance of a Deque, here we use the LinkedList
        // class which implements the Deque interface.
        Deque<String> deque = new LinkedList<>();
        deque.add("one");
        deque.add("two");
        deque.add("three");
        deque.add("four");

        StringBuilder in = new StringBuilder("Items IN in order : ");

        // Returns an iterator over the elements in this deque in
        // proper sequence
        for (String s : deque) {
            in.append(s).append(",");
        }

        in.deleteCharAt(in.length() - 1);
        System.out.println(in);

        StringBuilder out = new StringBuilder("Items OUT in order: ");
        for (int i = 0; i < deque.size(); ) {
            out.append(deque.peekLast()).append(",");

            // Retrieves and removes the last element of this deque,
            // or returns null if this deque is empty.
            deque.pollLast();
        }

        out.deleteCharAt(out.length() - 1);
        System.out.println(out);
    }
}

The output of our code snippet is:

Items IN in order : one,two,three,four
Items OUT in order: four,three,two,one

How do I get timezone ids by their milliseconds offset?

package org.kodejava.util;

import java.util.TimeZone;

public class TimeZoneByOffset {
    public static void main(String[] args) {
        // Gets the available IDs according to the given time zone
        // offset in milliseconds.
        int offset = 8 * 60 * 60 * 1000;
        String[] timezones = TimeZone.getAvailableIDs(offset);

        System.out.println("List of available IDs for GMT:+08:00");
        System.out.println("====================================");
        for (String id : timezones) {
            System.out.println(id);
        }
    }
}

Here are the timezone ids in the GMT+8 offset:

List of available IDs for GMT:+08:00
====================================
Asia/Brunei
Asia/Choibalsan
Asia/Chongqing
Asia/Chungking
Asia/Harbin
Asia/Hong_Kong
Asia/Irkutsk
Asia/Kuala_Lumpur
Asia/Kuching
Asia/Macao
Asia/Macau
Asia/Makassar
Asia/Manila
Asia/Shanghai
Asia/Singapore
Asia/Taipei
Asia/Ujung_Pandang
Asia/Ulaanbaatar
Asia/Ulan_Bator
Australia/Perth
Australia/West
CTT
Etc/GMT-8
Hongkong
PRC
Singapore

How do I create port scanner program?

In this example you’ll see how to create a simple port scanner program to check the open ports for the specified host name.

package org.kodejava.net;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

public class PortScanner {
    public static void main(String[] args) throws Exception {
        String host = "localhost";
        InetAddress inetAddress = InetAddress.getByName(host);

        String hostName = inetAddress.getHostName();
        for (int port = 0; port <= 65535; port++) {
            try {
                Socket socket = new Socket(hostName, port);
                String text = hostName + " is listening on port " + port;
                System.out.println(text);
                socket.close();
            } catch (IOException e) {
                // empty catch block
            }
        }
    }
}

The result of the code snippet above are:

localhost is listening on port 21
localhost is listening on port 80
localhost is listening on port 135
localhost is listening on port 445
localhost is listening on port 3000
...
...
localhost is listening on port 63342
localhost is listening on port 63467
localhost is listening on port 64891
localhost is listening on port 64921
localhost is listening on port 65001