Java Util Examples List - Util Tutorials
The util package or java provides many utility interfaces
and classes for easy manipulation of in-memory data. The java util package tutorials
at RoseIndia.net introduces you with the
Java util package of JDK. All the examples are with free source
code. It includes many examples that demonstrate the syntax and example code of
java util package.
- What is java.util package?
Java Utility package is one of the most used packages in the java
programming world and package provides many interfaces and classes for easy
manipulation of data.
- Generating a Random Number
In many places you need random numbers to fulfill your requirements. Java
provides classes to help you in generation random numbers for your
- Breaking a String into Words
In this section, you will learn how to break the string into words. The java.util.*;
package provides you a simple method to breaking the string into separate
- Listing All Available Locales
Locale is the class of java.util.*;
package. This class represents the geographical, political and cultural
region. Every tasks which requires to perform the operation is called
- Setting the Default Locale
This section shows you how to set the default locale. Example presented in
the section, illustrates you how to get and set the default locale.
- Associating a Value with an Object
In this section, you will learn how to associate value with an object in
Java util. Here, you will know how to associate the value for the separate
code. Values regarding to the separate code are maintained as a record of
the specific person.
- Comparing Arrays
This section show you how to determine the given arrays are same or not. The
given program illustrates you how to compare arrays according to the content
of the that.
- Shuffling the Elements of a List or Array
Shuffling is the technique i.e. used to randomize the
list or array to prepare each and every element for the operation. In this
section, you will learn about shuffling the element of a list or array.
- What is Collection Framework?
Java provides the Collections Framework. In the Collection
Framework, a collection represents the group of the objects. And a collection
framework is the unified architecture that represent and manipulate
In this Example you will learn how to iterate collection
in java. A running example of java program is provided that shows you how
you can iterate collections in java.
- Java next()
In this section, you will get the detailed explanation about the next() method of
interface Iterator. We are going to use next()
method of interface Iterator in Java.
In this section, you will get the detailed explanation about the remove()
method of interface Iterator. We are going to use remove()
method of interface Iterator in Java.
In this section, you will get the detailed explanation about the
method of interface Enumeration. We are going to use nextElement()
method of interface Enumeration in Java.
In this section, you will get the detailed explanation about the hasMoreElement()
method of interface Enumeration. We are going to use hasMoreElement()
method of interface Enumeration in Java.
- Converting a Collection to an Array
Here is the illustration for the conversion from the collection to an array.
In this section, you will learn how to do this.
- Converting an Array to a Collection
Here is the illustration for the conversion from the an array to a
collection. In this section, you will learn how how to do this. The given
example gives you a brief introduction for converting an array to collection
without losing any data or element held by the array.
The example given below illustrates the conversion of a
collection into a array. In this example we creating an object of ArrayList,
adding elements into this object and storing this object into List
interface. Convert elements of ArrayList into an array by using toArray().
- Implementing a Queue
In this section, you will learn how to implement the queue. A queue holds a
collection of data or elements and follows the FIFO ( First
In First Out) rule.
- Implementing a Stack
In this section, you will learn how to implement a stack in Java. A Stack is
like a bucket in which you can put elements one-by-one in sequence and
retrieve elements from the bucket according to the sequence of the last
- Implementing a Least-Recently-Used
In this section, you will learn about the least-recently-
used(LRU) cache in Java. LRU Cache helps you how to use the
buffer and how to limit or fixed the size of buffer and how to manage
storing and retrieving process of the buffer which size is limited.
- Creating a Copy of a Collection
In this section, you will learn how to create and then copy the collection.
The copied collection contains the reference to the object. In-fact, objects
are not cloned.
- Making a Collection Read-Only
This section describes you how to make a collection read-only. In this
section, list (component of the collection) has been represented for making
it read-only. By default, list is not read-only.
- Sorting a List
This section gives you the best illustration for sorting elements of a
Collection. You can see how to sort all elements of a Collection in
ascending or descending order.
- Creating a Hash Table
This section explains the implementation of the hash table. What is the hash
table and how to create that? Hash Table holds the records according to the
unique key value. It stores the non-contiguous key for several values.
- Finding an Element in a Sorted Array
In the sorted array, searching is very easy. In this section, you will learn
how to sort an array and how to find a text in the sorted array. For binary
search first you must sort the array and then apply the binary search.
- Reading a Properties File
In this section, you will learn how to read the key-value of properties
files in Java. The properties file provides the general text editor, which
have the keys and it's values.
- Getting all
properties of a properties file
In this section, you will learn how to get all keys and it's values of the
properties files in Java. Java provides the features to collection of all
keys and values of the properties file.
- Setting property to
a properties file
In this section, you will learn how to add the keys and it's values of
the properties files in Java. You know the properties files have the
keys and values of the properties files.
- Scheduling a Timer Task to Run at a Certain Time
In this section, you can learn how to schedule a timer task to run at a
certain time and repeatedly. There is an example given for the illustration.
This program asks you for the way of performing the task whether at the
certain time or repeatedly.
- Getting the Current Time
In this section, through the given program you can get the current time in
the proper format (Hour:Minute:Second AM/PM). This program shows the current
time by using the various methods of the Calendar class.
- Getting Information of All Available Time
Time Zone is the specified part of the earth that holds the special standard
of the time i.e. termed as local time. Usually time zone is based on the GMT
(Greenwich Mean Time).
- Getting the Current Date
In this section, through the given program you can get the current date in
the proper format (Day/Month/Year). This program shows the current date by
using the various methods and fields of the Calendar class.
the Number of Days in a Month
This section will show the way of counting the number of days of a month in
the specified year. Following program takes the year as input through the
keyboard and shows the number of days in the February month of the mentioned
year if the year is valid other it will show the error message and terminate
- Comparing Dates
This example illustrates you how to compare two dates. Generally dates are
calculated in milliseconds. In this section, we will show you how to compare
two given dates. To compare dates the given program is helpful for
application development. You can easily use the code for comparing dates.
- Determining a Person's Age
Here, you can calculate the age of a person easily through the given
- Determining If a Year Is a Leap Year
Leap Year is the year contains an extra day. In the leap year, February
month contains 29 days since normally February month has 28 days. The year
which is completely divided by 4 that is the leap year.
- Determining the Day-of-Week for a Particular Date
This section simply show the current day in word i.e. the Day of Week for a
particular date. Here, you can learn about the way of doing that.
- What is a JAR file in
JAR file is the compressed file format. You can store many files in a
JAR file. JAR stands for the Java Archive. This file format is used to
distribute a set of java classes. This file helps you to reduce the file
size and collect many file in one by compressing files.
- Creating a JAR file in
This section provides you to the creating a jar file through the java source
code by using the jar tool command which is provided by the JDK (Java
- Listing the Entries of a JAR File Manifest
This section shows you how you can read the content of jar file from your
java program and list the content. This section helps you to view the
contents of the jar file without extracting it.
- Listing the Main Attributes in a JAR File Manifest
Jar Manifest file is the main section of a jar file.
This file contains the detailed information about the specific individual
jar file. It contains the complete information about the jar file, each and
every section or particular information is separated by new line.
- Creating a ZIP File
Zip file format is the popular method of data compression. Zip file contains
many files in compressed format. You can say the files are stored in the zip
format with compression. The file which is in the zip format holds the
- Listing the Contents of a ZIP File
Section, provides the way for listing all the elements of a zip file through
the java code. Following program helps you for the appropriate. You can
directly copy and paste the code in your java application for showing list
of elements present in the zip file format.
- Retrieving a Compressed File from a ZIP File
How to unzip (extract) a zip file? How to retrieve elements from a zip
format file? All these type of questions are solved through the following
Java Logging Program
This section introduce you about Java logging and how to use the logger in
Java program to log the error and messages into the log file. Explanation of
java logging is explained in the given example program.
- Logging an Exception
This section introduces you the concept of logging exception handling. When
certain conditions are not followed or wrong then compiler shows a message
which is mentioned under the specific exception.
- Preventing a Logger from Forwarding Log Records to Its Parent
This section describes how to forward log records to its parent. You know
that a logger sends both log records (messages declared before setting the
log for it's parent and log messages after setting for it's parent) to
handlers, parent or ancestor.
- Writing Log Records to a Log File
This section demonstrates for writing log records to a log file. Logger
provides different types of level like: warning, info and severe that have
- Writing Log Records to Standard Error
This section demonstrates how to write log records to standard error in
Java. Java provides the facility for handling an error through the help of
- Writing Log Records Only After a Condition Occurs
This section deals with log records those have been
written in a log file if the "if" conditions are certified otherwise not.
- Setting a Filter on a Logger Handler
This section illustrates you how to set filter on a logger handler in Java. Generally, Filter is a program that
provides the facility to get data if the certain conditions are satisfied otherwise
don't get any data. In other words, you get data to be filtered.
- What is Logging Level
Logger: Logger is the class of java.util.logging
package that extends the Object class. Java provides logging APIs like: Logger,
Level, Handler etc. for implementing logging features in your java application.
- Getting the Log Level of a Logger
This section helps you to learn about the getting log
level of a logger through the java program and illustrates how to get it. The Logger provides various types of levels
of a logger like: INFO, WARNING, SEVERE
and FINEST etc.
- Comparing Log Levels
This section describes, how to compare log levels to each other. In
Java, the Level class assigns a set of multiple logging levels that has
already an individual integer type value.
- Creating a Custom Log Level
This section tells you how to create a custom log level that means log
levels are created by users for own need. Java logging class provides some
levels like: INFO, WARNING, SEVERE, FINEST etc.
- Setting the Formatter of a Logger Handler
This section discussed how
to set the formatter of a logger handler. Java provides a logger handler that
uses a formatter for writing log records for a log file that can be either xml
file or other.
- Creating a Custom Formatter for a Logger Handler
This section tells you how to create a custom
formatter for a logger handler. Java provides two types formatter
SimpleFormatter and XMLFormatter. But, the java logging package allows for
creating a custom formatter through the logger handler.
- Limiting the Size of a Log File
This section illustrates you how to limit the size of a
log file in Java. Log files haven't any boundation at the file creation time but
Java provides the facility for limiting the size of a log file with the help of FileHandler.
- Regular Expression Search Program
Regular Expression is used to perform many string related operations. This
is the way to manipulate different kind of operations like search, edit and
manipulating string can be performed by the regular expression. It has both
type characters (literal characters and meta characters).
- Applying Regular Expressions on the Contents of a File
This section illustrates you how to search a string in a file content.
Following takes the file name from which the given string/text has to be
searched. File name must be text file (with ".txt" extension).
duplicate white space from a String
In this section, you will learn how to remove all white spaces from the
given string by using the regular expressions. This section gives you a
example for the best illustration about the way of removing all the
duplicate white spaces from the given string.
duplicate white space from a text file
This section shows you how to remove all duplicate white spaces from a
specific text file. In this section a program is given as follows for the
best illustration for how to remove all duplicate white spaces from the
given text file. This program reads the file line-by-line for search the
duplicate white spaces and replace all duplicate white spaces with the
single white space.
- Greedy and Nongreedy Matching in a Regular Expression
This section illustrates you how to match a string in the given string
through the Greedy and Non-Greedy way of matching. Here you can learn about
the Greedy and Non-Greedy way of matching the word.
- Escaping Special Characters in a Pattern
This section illustrates you how to escape all special characters from the
given string or text. Here, you can learn the way of detecting all special
characters present in the given string.
- Parsing Character-Separated Data with a Regular Expression
This section illustrates you how to split String data ( separated by
"," or "and/or"). These words are shown in a new line
without the word or characters which is used for separation the word of the
sentences through the given regular expression of the following program.
- Setting Case Sensitivity in a Regular Expression
This section describes the method to set the patter is either case sensitive
or not. Actually, by default the patter is in the case sensitive. Whenever
it need to be set for the case insensitivity then you have to use the
appropriate code followed in following program.
- Compiling a Pattern with Multiple Flags
In this section you will learn how to compile a pattern
with multiple flags like multiline, case insensitivity of the characters in the
string etc. This section provides you an example for the implementation of the
concept about how is it done?
- Capturing Text in a Group in a Regular Expression
This section illustrates you how to capture the text in
a group through the regular expression. Here, you can learn the procedure of
capturing text made by the given alphabets (mentioned in the code of the program
"a", "b" and "c") from the group of text.
- Getting the Indices of a Matching Group in a Regular Expression
Here, in this section, you will find the lower and
upper indices of the matching group of the text through the regular
expression in Java.
- Using the Captured
Text of a Group within a Replacement Pattern
In this section, you will learn how to use the captured
text from the group of the text. This section illustrates you how to capture the
string or text through the regular expression and it is also used through the
- Reading Lines from a String Using a Regular Expression
You will learn how to separate a string by detecting
the line. An example has been given in the section for the best illustration of
the procedure of reading of line from the file.
- Removing Line Termination Characters from a String
This section provides you an example of with program
code which reads the string or text of a text file which is mentioned by the
user and replacing all the new line character by the double quote with one space
(" ") wherever it found.
- Parsing a String into Paragraphs Using a Regular
This section tells you how to parse the string into the
paragraphs using regular expression in Java. In this section, you will learn
about the topic through the following program.
- Calculate process time in Java
This section provides the facility to calculate the process time of
the completion of the operation through the Java program. In this section,
an example with the complete code of the program is given for the best
illustration of the procedure of calculating the process time in which any
type of operation is completed after whole processing.
- Decreasing process time by caching through the Hash
Table in Java
This section illustrates you how to improve the
process time of any type of the operation performed by your java program.
You can easily improve the process completion time of the operation by using
more and more cache of your system.
- Simple Hash Table implementation in Java
This section describes the complete Hash Table implementation from
the basic in Java. In this section, you will see how to store some
information and retrieve when it need to search for checking whether the
given title exists or not. This section is very helpful for your java
application for which the following program supports for working the
software in very efficient manner.
- Hash Table for maintaining popup menus in Java
This section shows the procedure of maintaining popup menus through
hash table in your Java program. This section provides you an example with
the complete code of the program that has been used to create and maintain
popup menus in Java.
- Partial Search from Hash Table in Java
This section illustrates you how to put some string in the Hash
Table through the Java program and get these string easily. Here, you will
see that the given string which is mentioned with the command "java
<file name>" in the command line.
- Writing and reading from/to a serialized file through
Hash Table in Java
This section illustrates you how to read and write from/to a
serialized file through the hash table in Java. This section provides an
example with the complete code of the program.
- Thread Access through the Hash Table in Java
Here, you will learn about how a thread is accessed by using the
hash table in Java.
Get previous, current and next date in Java
In this section, you will learn how to get previous,
current and next date in java. The java util package provides the
facility of it.
Get first day of week
In this section, we will learn how to get the first day
of week in Java. As we know that GregorianCalendar provides the
current date, month and year in a yearly calendar.
this section you will learn about preferences and all
the methods that modify preference data are allowed to operate a certain
work. It allow to store the user and system configuration data .
Multiple Value initialization and retrieval by put and get method
In this example you are going to find out the flow of using put and get
method of the preferences. After reading, coding, and running you are able to
use the get and put method.
Listening for Changes to Preference Values in a Preference Node
In this section you will learn how to access the user's node and retrieve the
stored information in the class.
Export System Preferences
this section you will learn that how a user can access the System preferences.
Here we are going to make it easier to
understand the step be step process by the complete example on this topic.
Exporting the User Type Preferences in a Subtree of Preference Node
this section you will learn that how a user can access the User preferences.
Here we are going to make it easier to understand the step be step process by
the complete example on this topic.
- Saving and Retrieving a Preference Value
In this section you will learn about the way to retrieves the user preference node using the same
class and saves and retrieves a preference in the node.
Here an example is also provided for learning about the topic in detailed
- Change the
value of Preferences by user
In the given example we explain a simple
way for using the get() and put() method in preferences. In preferences the
get() is the most useful method with
the help of which you can stored data in the class and can change by put()
- Using get() method in preferences
Get is the most useful method with
the help of which you can stored data in the class and can also print the value
of that stored value by using simple print command.
- Using put() method in preferences
By this example you will learn that how a put function
is used in the program.
Frame with Preferences
In this section you will learn how to
use the preferences in frame. The example will make it easier to
understand to use the preferences with frame window.
Clone method example in Java
In this section, you will learn how to use
clone() method of Cloneable interface in java. The clone is a
method in java for handling the object duplication. This is the method of Cloneable
interface (java.lang package).
SortedMap (interface) example in java Collection Framework
A SortedMap is a map that maintains its entries in ascending order, sorted
according to the
keys' natural order, or according to a
SortedMap creation time.
- Determining If a Preference Node Contains a Specific Key
This section demonstrates you to determine whether the Preference Node
contains a Specific key or not. You can see in the given example that we
have create a method contain(String key) of Boolean type.
- Determining If a Preference Node Contains a Specific Value
This section demonstrates you to determine whether the Preference code
contains the specified value or not by enumerating the key/value pairs in a
preference node and checks each value for a match.
- Removing a Preference from a Preference Node
This section demonstrates you to remove the preference from a preference node.
You can see in the given example that in order to remove the preference from the
Preference node 'Roseindia'.
- Getting and Setting Java Type Values in a Preference
As you know that a preference node holds only string values. Therefore the
Preferences class provides some convenient methods to convert the Java types into
- Getting the Maximum Size of a Preference Key and Value
This section demonstrates you to get the maximum size of Preference key and
value. You can see in the given example that we have used the fields of class
- Creating a Preference Node
This section demonstrates you to create a Preference Node in C. As you know
that there are two types of Preference nodes, User Preference Node and System
- Removing a Preference Node
This section demonstrates you to remove the Preference node. You can see in
the given example that firstly you have to get the specified node using the
method userRoot().node("/Roseindia Employees") method.
- Determining If a Preference Node Exists
A preference node can be created automatically by using the methods Preferences.node(),
Preferences.userNodeForPackage(), or Preferences.systemNodeForPackage().
- Retrieving the Parent and Child Nodes of a Preference Node
In this section, we are going to retrieve the Parent and Child nodes of a
Preference Node. You can see in the given example that we have used Preferences.userNodeForPackage(java.lang.String.class)
to get the Preference node.
- Finding a Preference in a Preference Tree
You can see in the given example that we are retrieving the keys and values
of the Preference node 'Java Types'. For this, we have create a method find(Preferences
p) and use different methods inside it.
- Importing Preferences
Preferences can be exported using the methods Preferences.exportNode()
and Preferences.exportSubtree(). This exported data can be imported by
using the method Preferences.importPreferences().
- Listening for Changes to Preference Values in a Preference Node
This section demonstrates you to change the Preference values in a Preference
node. You can see in the given example, we have used the class PreferenceChangeEvent
which occurs when a preference is added.
- Determining When a Preference Node Is Added or Removed
This section illustrates you to determine when a Preference Node is added or
removed. You can see in the given example that we have add a new node by using
the method userRoot().node("/Hello World").
- Implementing a Simple Event Notifier
The Observer and Observable classes are useful for implementing a simple
event notifier. The Observer class informed the changes that occurred in
observable objects and Observable class represents an object that the
application wants to have observed.
Properties file in Java
In this section, you will know about the properties
file. The properties file is a simple text file. Properties file contains keys
and values for the specific key.
- Random alpha numeric string of fixed size
- Java current Date
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.