How do I get all available Locale language codes?

When you want to internationalize your application, you need to know the language code and country code. These codes will be used as the properties file name (the file name must be ended in language_COUNTRY.properties). Here is an example that show how to get all supported language’s code.

package org.kodejava.util;

import java.util.Locale;

public class LocaleCountryLanguageCode {
    public static void main(String[] args) {
        // Gets an array of all installed locales. The returned array represents
        // the union of locales supported by the Java runtime environment and by 
        // installed LocaleServiceProvider implementations.
        Locale[] locales = Locale.getAvailableLocales();

        for (Locale locale : locales) {
            System.out.printf("Locale name: %s = %s_%s%n",
                    locale.getDisplayName(), locale.getLanguage(), locale.getCountry());
        }
    }
}

Here are some of the results produces by the code above:

...
Locale name: Kikuyu (Latin, Kenya) = ki_KE
Locale name: Spanish (Brazil) = es_BR
Locale name: Koyra Chiini (Mali) = khq_ML
Locale name: English (Solomon Islands) = en_SB
Locale name: Tibetan (Tibetan, China) = bo_CN
Locale name: Cherokee (United States) = chr_US
Locale name: Kinyarwanda (Rwanda) = rw_RW
Locale name: Tachelhit (Tifinagh, Morocco) = shi_MA
Locale name: Arabic (Iraq) = ar_IQ
Locale name: Nyankole = nyn_
...

How do I use the SortedMap interface?

package org.kodejava.util;

import java.util.*;

public class SortedMapDemo {
    public static void main(String[] args) {
        SortedMap<String, String> sorted = new TreeMap<>();
        sorted.put("United States", "New York");
        sorted.put("United Kingdom", "London");
        sorted.put("Netherlands", "Amsterdam");
        sorted.put("Japan", "Tokyo");
        sorted.put("France", "Paris");

        // Gets the first(lowest) key currently in this map
        String firstKey = sorted.firstKey();

        // Gets the last (highest) key currently in this map
        String lastKey = sorted.lastKey();

        System.out.println("First key: " + firstKey);
        System.out.println("Last key : " + lastKey);

        // Gets a view of the portion of this map whose keys range
        // from fromKey(Japan), inclusive, to toKey(United Kingdom),
        // exclusive. (If fromKey and toKey are equal, the returned
        // map is empty.)
        SortedMap<String, String> sub = sorted.subMap("Japan",
                "United Kingdom");
        Set<String> subKeys = sub.keySet();
        System.out.println("\nSub Map: ");
        System.out.println("============");
        for (String key : subKeys) {
            System.out.println(key);
        }

        // Gets a view of the portion of this map whose keys are
        // strictly less than toKey(in this example is Netherlands)
        SortedMap<String, String> head = sorted.headMap("Netherlands");
        Set<String> headKeys = head.keySet();
        System.out.println("\nHead Map:");
        System.out.println("============");
        for (String key : headKeys) {
            System.out.println(key);
        }

        // Gets a view of the portion of this map whose keys are
        // strictly greater than or equal fromKey(in this example
        // is Netherlands)
        SortedMap<String, String> tail = sorted.tailMap("Netherlands");
        Set<String> tailKeys = tail.keySet();
        System.out.println("\nTail Map:");
        System.out.println("============");
        for (String key : tailKeys) {
            System.out.println(key);
        }
    }
}

Here is the output of the program:

First key: France
Last key : United States

Sub Map: 
============
Japan
Netherlands

Head Map:
============
France
Japan

Tail Map:
============
Netherlands
United Kingdom
United States

How do I convert a java.util.Vector into an array?

This example demonstrates how to convert a Vector object into an array. You can use the Vector.copyInto(Object[] array) method to create a copy of the Vector object in array form.

package org.kodejava.util;

import java.util.Vector;

public class VectorToArray {
    public static void main(String[] args) {
        Vector<Integer> vector = new Vector<>();
        vector.add(10);
        vector.add(20);
        vector.add(30);
        vector.add(40);
        vector.add(50);

        // Declares and initializes an Integer array.
        Integer[] numbers = new Integer[vector.size()];

        // Copies the components of this vector into the specified
        // array of Integer
        vector.copyInto(numbers);

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

The result of the code snippet above:

Number: 10
Number: 20
Number: 30
Number: 40
Number: 50

How do I remove elements from Deque?

This example shows you how to remove some elements from the Deque object. We can use the following methods for removing elements from Deque: remove(), remove(Object o), removeFirst(), removeLast().

package org.kodejava.util;

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

public class RemoveDequeDemo {
    public static void main(String[] args) {
        Deque<String> deque = new LinkedList<>();
        deque.add("A");
        deque.add("B");
        deque.add("C");
        deque.add("D");
        deque.add("E");
        deque.add("F");

        // Removes and retrieves the head of this Deque
        deque.remove();      // Removes "A"

        // Removes the first occurrence of element from this Deque
        deque.remove("F");   // Removes "F"

        // Retrieves and removes the first element of this deque
        deque.removeFirst(); // Removes "B"

        // Retrieves and removes the last element of this deque
        deque.removeLast();  // Removes "E"

        for (String item : deque) {
            System.out.println("Item = " + item);
        }
    }
}

How do I add elements into a Deque?

To add elements into a Deque object we can use the add(), addFirst() and addLast() method call.

package org.kodejava.util;

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

public class DequeAddElement {
    public static void main(String[] args) {
        // Create an instance of Deque using LinkedList class.
        Deque<String> deque = new LinkedList<>();

        // Insert a word into the Deque
        deque.add("jumps");

        // Insert words at the beginning of the current Deque
        // elements
        deque.addFirst("fox");
        deque.addFirst("brown");
        deque.addFirst("quick");
        deque.addFirst("The");

        // Insert words at the end of the current Deque elements
        deque.addLast("over");
        deque.addLast("the");
        deque.addLast("lazy");
        deque.addLast("dog");

        for (String word : deque) {
            System.out.println("Word = " + word);
        }
    }
}

Here is the output:

Word = The
Word = quick
Word = brown
Word = fox
Word = jumps
Word = over
Word = the
Word = lazy
Word = dog