Sunday, July 29, 2007

Java Code to Associate a Value with an Object

This example demonstrates how to associate a value with an arbitrary object. The technique involves saving the object and the associated value as a key/value pair in an IdentityHashMap. A HashMap cannot be used for this purpose since if two objects happen to equal via the Object.equals() method, one of the objects will not be stored.
// Create the map
Map objMap = new IdentityHashMap();

// Add the object and value pair to the map
Object o1 = new Integer(123);
Object o2 = new Integer(123);
objMap.put(o1, "first");
objMap.put(o2, "second");

// Retrieve the value associated with the objects
Object v1 = objMap.get(o1); // first
Object v2 = objMap.get(o2); // second

Thursday, July 26, 2007

Java Code to Set the Default Locale

There are two ways to change the default locale. The first is to set it on the command line:
> java -Duser.language=2-char-language-code -Duser.region=2-char-country-code MyApp

// Set only language code
> java -Duser.language=fr -Duser.region= MyApp
// Set language and country code
> java -Duser.language=fr -Duser.region=CA MyApp


The second way to change the default locale is to call Locale.setDefault():
// Get default locale
Locale locale = Locale.getDefault();

// Set the default locale to pre-defined locale
Locale.setDefault(Locale.FRENCH);

// Set the default locale to custom locale
locale = new Locale("fr", "CA");
Locale.setDefault(locale);

Tuesday, July 24, 2007

Java Code to Get Listing Of All Available Locales

Locale[] locales = Locale.getAvailableLocales();

for (int i=0; i // Get the 2-letter language code
String language = locales[i].getLanguage();

// Get the 2-letter country code; may be equal to ""
String country = locales[i].getCountry();

// Get localized name suitable for display to the user
String locName = locales[i].getDisplayName();
}

Here's a sample of output using a default locale of Locale.ENGLISH:
Language Code, Country Code, Localized Name
ar, , Arabic
ar, AE, Arabic (United Arab Emirates)
ar, BH, Arabic (Bahrain)
ar, DZ, Arabic (Algeria)
ar, EG, Arabic (Egypt)
ar, IQ, Arabic (Iraq)
ar, JO, Arabic (Jordan)
ar, KW, Arabic (Kuwait)
ar, LB, Arabic (Lebanon)

Here's a sample of output using a default locale of Locale.FRENCH:
Language Code, Country Code, Localized Name
ar, , arabe
ar, AE, arabe (Emirats Arabes Unis)
ar, EG, arabe (Egypte)
ar, IQ, arabe (Irak)
ar, JO, arabe (Jordanie)
ar, KW, arabe (Koweit)
ar, LB, arabe (Liban)

Friday, July 20, 2007

Breaking a String into Words

String aString = "word1 word2 word3";
StringTokenizer parser = new StringTokenizer(aString);
while (parser.hasMoreTokens()) {
processWord(parser.nextToken());
}

Tuesday, July 10, 2007

Generating a Random Number - Package java.util

Random rand = new Random();

// Random integers
int i = rand.nextInt();
// Continually call nextInt() for more random integers ...

// Random integers that range from from 0 to n
int n = 10;
i = rand.nextInt(n+1);

// Random bytes
byte[] bytes = new byte[5];
rand.nextBytes(bytes);

// Other primitive types
boolean b = rand.nextBoolean();
long l = rand.nextLong();
float f = rand.nextFloat(); // 0.0 <= f < 1.0
double d = rand.nextDouble(); // 0.0 <= d < 1.0


// Create two random number generators with the same seed
long seed = rand.nextLong();
rand = new Random(seed);
Random rand2 = new Random(seed);