Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Java Util Package - Utility Package of Java

ava Utility package is one of the most commonly used packages in the java program. The Utility
Package of Java consist of the following components:

 collections framework
 legacy collection classes
  
 event model
  
 date and time facilities
  
 internationalization
  
 miscellaneous utility classes such as string tokenizer, random-number generator and bit
array
  

Here are some of the description of the utility classes of this package:

Data Structure Classes  


Data Structure Classes are very useful classes for implementing standard computer science data
structures: including BitSet, Dictionary, Hashtable, Stack and Vector. The Enumeration interface
of java.util package is used to count through a set of values. 

Date  
The Date class is used to manipulate calendar dates in a system-independent fashion. 

StringTokenizer  
This StringTokenizer class is used to convert a String of text into its tokens. 

Properties  
The properties table contains key/value pairs where both the key and the value are Strings and
the class is used by the System class to implement System properties. 

Observer and Observable  


Classes that implement the Observer interface can "watch" Observable objects for state changes.
When an Observable object changes it notifies all of its Observers of the change. 

Random-Number Generator  
The Random Number Generator class is used to generate the random-numbers. 

Enumeration  
The Enumeration interface defines a generic programming interface for iterating through a set of
values
.

java.util
The java.util package contains classes that deal with collections, events, date and time,
internationalization and various helpful utilities.

\ The java.util Package

The java.util package includes a number of utilities, including those for date and time functions,
tokenizing strings, and generating random numbers. The java.util package also gives a number
of data structures, or collection classes, that allow you to store and organize data efficiently.

java.util.StringTokenizer

The StringTokenizer class permits you to break up String objects into what are known as
tokens. Tokens are separated by what we call delimiters. The StringTokenizer class is handy
when you want to break apart a list of data items that are supposed to be in a specific format. For
example, consider the following list of names:

Suzy, Yetti, John, Mary, Rover, Ezekiel

A comma followed by a space character separates each item in the list. Assume, if you want to
parse only the names from the String and print them out. The following code constructs a
StringTokenizer object to parse the names and print them to the console:

String names = "Suzy, Yetti, John, Mary, Rover, Ezekiel";


String delimiter = ", ";
StringTokenizer st = new StringTokenizer(names, delimiter);
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}

The StringTokenizer constructor takes two String arguments: the String to tokenize and a
list of delimiter characters. Notice that the delimiter String includes both the space character as
well as the comma character. These characters do not want to be found sequentially in order to
be delimited; all the tokenizer wants is to find at least one of the characters found in the delimiter
String to work. Therefore, you would expect the program to print the following:

Suzy
Yetti
John
Mary
Rover
Ezekiel

java.util.Random

As you know, the Math random method creates a random number between 0.0 and 1.0. The first
time this method is invoked, it generates a new java.util.Random object, with the current
system time as its seed. Subsequent calls to the Math random method return the next generated
number from the same Random object.

Use of the Math random method works well, for most cases. However, there are times when
you will wish to have more control over the random values produced. For instance, when the
Random class is seeded with the same value, it will generate the same output. You may want to
specify a particular seed (a long value) so that you can control and monitor your results to make
sure you get the same output each time. You may also want to provide specific objects their own
Random object to make sure that each object gets its own unique distribution of random values,
rather than having them all feed from the same Random instance.

Another advantage of using the Random class over the Math random method is that you can get
values of different primitive types. The following code snippet demonstrates how you can get
random values of type float, boolean, long, and double:

// create a new Random object with an initial seed of 200


Random random = new Random(200);
// print out the next random int value
System.out.println(random.nextInt());
// print out the next random int, restricting output to numbers between 0 and 20
System.out.println(random.nextInt(20));
// print out the next random boolean value
System.out.println(random.nextBoolean());
// print out the next random float value
System.out.println(random.nextFloat());
// print out the next random double value
System.out.println(random.nextDouble());
// change the generator's seed to 100
random.setSeed(100);
// print out the next random long value
System.out.println(random.nextLong());

You might also like