Introduction to java.util Package

Introduction to java.util Package

·

3 min read

Hello all👋 I hope you are doing well. This is going to be a short introductory article about the most useful package in Java i.e., java.util package.

Let's begin...

Let's first understand package

What is a package?
In short a Java package is collection of similar type of classes.
A Package can be defined as a collection of similar types of classes, interfaces and sub-packages in the form of directory structure. You can read more about packages in one of my article here.

java.util

The basic utility classes required to a programmer are present in this package. It contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).

To use any class you have to import java.util package at top of the program:-

import java.util.*;

or

import java.util.Class_name;


To make it easy let's take an example, let's suppose you want to print date and time in your program you will need to import java.util package.

import java.util.Date;
//or
//import java.util.*;

public class Demo {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println("The date is : " + date);
    }
}

You can run your code online here

java.util.png

What is use of java.util package?

  • For Java collections.
  • For random number generation.
  • For Calendar.
  • For string parsing.
  • For internationalization support by using the internationalization supported classes from java.util package (Locale).

Some important and generally used classes

Some important and generally used classes and interfaces which are present inside the java.util package are:-

  • Arrays :- This class contains various methods for manipulating arrays.
  • ArrayList :- This class is resizable-array implementation of the List interface.
  • Collections :- This class consists exclusively of static methods that operate on or return collections.
  • Date :- This class represents a specific instant in time, with millisecond precision.
  • EventObject :- This class is the root class from which all event state objects shall be derived.
  • Formatter :- An interpreter for printf-style format strings.
  • HashMap :- The HashMap class Hash table based implementation of the Map interface.
  • HashSet :- The HashSet class implements the Set interface, backed by a hash table (actually a HashMap instance).
  • HashTable :- The HashTable class implements a hash table, which maps keys to values.
  • LinkedList :- The LinkedList class Doubly-linked list implementation of the List and Deque interfaces.
  • Locale :- A Locale object represents a specific geographical, political, or cultural region.
  • Objects :- This class consists of static utility methods for operating on objects.
  • Random :- An instance of this class is used to generate a stream of pseudorandom numbers.
  • Scanner :- A simple text scanner which can parse primitive types and strings using regular expressions. (Read more)
  • StringTokenizer :- The string tokenizer class allows an application to break a string into tokens.
  • Timer :- A facility for threads to schedule tasks for future execution in a background thread.
  • TimerTask :- A task that can be scheduled for one-time or repeated execution by a Timer.
  • TreeMap :- The TreeMap class A Red-Black tree based NavigableMap implementation.
  • TreeSet :- The TreeSet class A NavigableSet implementation based on a TreeMap.

Resources-

Documentation || Tutorial

Okay so that's enough for now.

Thank you for reading.

Please share your thoughts about it and correct me if I'm wrong.

I hope you liked it and found it helpful.

Cover:- Rajat Gour

Connect with me on Twitter or LinkedIn

My personal blog blog.ritvikdubey.com