How do I sort an Enumeration?

package org.kodejava.example.util;

import java.util.*;

public class EnumerationSort {
    public static void main(String[] args) {
        // Creates random data for sorting source. Will use java.util.Vector
        // to store the random integer generated.
        Random random = new Random();
        Vector<Integer> data = new Vector<Integer>();
        for (int i = 0; i < 10; i++) {
            data.add(Math.abs(random.nextInt()));
        }

        // Get the enumeration from the vector object and convert it into
        // a java.util.List. Finally we sort the list using
        // Collections.sort() method.
        Enumeration enumeration = data.elements();
        List<Integer> list = Collections.list(enumeration);
        Collections.sort(list);

        // Prints out all generated number after sorted.
        for (Integer number : list) {
            System.out.println("number = " + number);
        }
    }
}

An example result of the code above is:

number = 64029842
number = 72921309
number = 230054456
number = 256151404
number = 345486128
number = 387933255
number = 398974777
number = 626349261
number = 654063730
number = 1319397578

How do I shuffle elements of an array?

package org.kodejava.example.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 + " ");
        }
    }
}

The generated results are:

F H E A B I G J D C  

How do I know the minimum and maximum number in array?

package org.kodejava.example.util;

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

public class ArrayMinMax {
    public static void main(String[] args) {
        // Creates an array of integer numbers in it.
        Integer[] numbers = {8, 2, 6, 7, 0, 1, 4, 9, 5, 3};

        // To get the minimum or maximum value from the array we can
        // use the Collections.min() and Collections.max() methods.
        // But as this method requires a list type of data we need
        // to convert the array to list first.
        int min = Collections.min(Arrays.asList(numbers));
        int max = Collections.max(Arrays.asList(numbers));

        // Viola! we get the minimum and the maximum value from the
        // array.
        System.out.println("Min number: " + min);
        System.out.println("Max number: " + max);
    }
}

And here are the results:

Min number: 0
Max number: 9

How do I create an empty collection object?

Sometimes you need to return an empty collection from your Java methods. The java.util.Collections utility class have three different static constants for creating empty List, Set and Map.

  • Collections.EMPTY_LIST
  • Collections.EMPTY_SET
  • Collections.EMPTY_MAP

There are also methods when you want to create type-safe empty collections.

  • Collections.emptyList()
  • Collections.emptySet()
  • Collections.emptyMap()

Bellow it the code example.

package org.kodejava.example.util;

import java.util.*;

public class EmptyCollectionDemo {
    public static void main(String args[]) {
        List list = Collections.EMPTY_LIST;
        System.out.println("list.size()  = " + list.size());
        Set set = Collections.EMPTY_SET;
        System.out.println("set.size()   = " + set.size());
        Map map = Collections.EMPTY_MAP;
        System.out.println("map.size()   = " + map.size());

        // For the type-safe example use the following methods.
        List<String> strings = Collections.emptyList();
        System.out.println("strings      = " + strings.size());

        Set<Long> longs = Collections.emptySet();
        System.out.println("longs.size() = " + longs.size());

        Map<String, Date> dates = Collections.emptyMap();
        System.out.println("dates.size() = " + dates.size());
    }
}

The output are:

list.size()  = 0
set.size()   = 0
map.size()   = 0
strings      = 0
longs.size() = 0
dates.size() = 0