- Java.util Package in Java
- java.util Package in Java
- Introduction to java.util Package in Java
- What does import java.util do in Java?
- java.util Package Import Statement Example
- Example 1: To Load the Whole Util Package
- Example 2: To Load a Specific Class from the java.util Package
- What is the Use of java.util Package in Java?
- What is Collections Framework in Java?
- java.util Package Interfaces
- java.util Package Classes
- java.util Package Enum
- java.util Package Exceptions
- Conclusion
Java.util Package in Java
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).
Following are the Important Classes in Java.util package :
- AbstractCollection: This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface.
- AbstractList: This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a “random access” data store (such as an array).
- AbstractMap: This class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.
- AbstractMap.SimpleEntry: An Entry maintaining a key and a value.
- AbstractMap.SimpleImmutableEntry: An Entry maintaining an immutable key and value.
- AbstractQueue: This class provides skeletal implementations of some Queue operations.
- AbstractSequentialList: This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a “sequential access” data store (such as a linked list).
- AbstractSet: This class provides a skeletal implementation of the Set interface to minimize the effort required to implement this interface.
- ArrayDeque: Resizable-array implementation of the Deque interface.
- ArrayList: Resizable-array implementation of the List interface.
- Arrays: This class contains various methods for manipulating arrays (such as sorting and searching).
- BitSet: This class implements a vector of bits that grows as needed.
- Calendar: The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.
- Collections: This class consists exclusively of static methods that operate on or return collections.
- Currency: Represents a currency.
- Date: The class Date represents a specific instant in time, with millisecond precision.
- Dictionary : The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values.
- EnumMap,V>: A specialized Map implementation for use with enum type keys.
- EnumSet: A specialized Set implementation for use with enum types.
- EventListenerProxy: An abstract wrapper class for an EventListener class which associates a set of additional parameters with the listener.
- EventObject: The root class from which all event state objects shall be derived.
- FormattableFlags: FomattableFlags are passed to the Formattable.formatTo() method and modify the output format for Formattables.
- Formatter: An interpreter for printf-style format strings.
- GregorianCalendar: GregorianCalendar is a concrete subclass of Calendar and provides the standard calendar system used by most of the world.
- HashMap : Hash table based implementation of the Map interface.
- HashSet: This class implements the Set interface, backed by a hash table (actually a HashMap instance).
- Hashtable: This class implements a hash table, which maps keys to values.
- IdentityHashMap : This class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values).
- LinkedHashMap : Hash table and linked list implementation of the Map interface, with predictable iteration order.
- LinkedHashSet: Hash table and linked list implementation of the Set interface, with predictable iteration order.
- LinkedList: Doubly-linked list implementation of the List and Deque interfaces.
- ListResourceBundle: ListResourceBundle is an abstract subclass of ResourceBundle that manages resources for a locale in a convenient and easy to use list.
- Locale – Set 1, Set 2: A Locale object represents a specific geographical, political, or cultural region.
- Locale.Builder: Builder is used to build instances of Locale from values configured by the setters.
- Objects: This class consists of static utility methods for operating on objects.
- Observable: This class represents an observable object, or “data” in the model-view paradigm.
- PriorityQueue: An unbounded priority queue based on a priority heap.
- Properties: The Properties class represents a persistent set of properties.
- PropertyPermission: This class is for property permissions.
- PropertyResourceBundle: PropertyResourceBundle is a concrete subclass of ResourceBundle that manages resources for a locale using a set of static strings from a property file.
- Random: An instance of this class is used to generate a stream of pseudorandom numbers.
- ResourceBundle: Resource bundles contain locale-specific objects.
- ResourceBundle.Control: ResourceBundle.Control defines a set of callback methods that are invoked by the ResourceBundle.getBundle factory methods during the bundle loading process.
- Scanner: A simple text scanner which can parse primitive types and strings using regular expressions.
- ServiceLoader: A simple service-provider loading facility.
- SimpleTimeZone: SimpleTimeZone is a concrete subclass of TimeZone that represents a time zone for use with a Gregorian calendar.
- Stack: The Stack class represents a last-in-first-out (LIFO) stack of objects.
- 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.
- TimeZone: TimeZone represents a time zone offset, and also figures out daylight savings.
- TreeMap: A Red-Black tree based NavigableMap implementation.
- TreeSet: A NavigableSet implementation based on a TreeMap.
- UUID: A class that represents an immutable universally unique identifier (UUID).
- Vector: The Vector class implements a growable array of objects.
- WeakHashMap: Hash table based implementation of the Map interface, with weak keys.
java.util Package in Java
The java.util package in Java is a built-in package that contains various utility classes and interfaces. It provides basic functionality for commonly occurring use cases. It contains Java’s collections framework, date and time utilities, string-tokenizer, event-model utilities, etc.
Introduction to java.util Package in Java
To support the needs of a software developer, Java provides various built-in and pre-written functionalities in the form of packages. These built-in packages contain various kinds of classes and interfaces that can be used to develop better and maintainable code. Some of the built-in Java packages are shown in the figure given below:
One of the most important Java packages is the java.util package. The java.util package contains several pre-written classes that can act as a base to solve commonly occurring problems in software development. It provides basic and essential source code snippets to the programmers that can lead to clean and maintainable code. It contains various classes and methods that perform common tasks such as string tokenization, date and time formatting, Java Collections implementation, etc.
What does import java.util do in Java?
To load the basic utility classes and interfaces provided by the java.util package, we need to use Java’s import keyword. The import keyword is used to access and load the package and its respective classes into the Java program. The syntax to import the package or its classes is given below:
Here, the first import statement is used to load a certain class from the specified package, whereas the second statement is used to import the whole package into the Java program. For the java.util package:
The above statement is used to load all the functionalities provided by the java.util package.
java.util Package Import Statement Example
Let’s look at some examples to understand the use of import statements while loading the java.util package:
Example 1: To Load the Whole Util Package
As discussed, this statement will load all the pre-written classes and interfaces provided by the java.util package.
Example 2: To Load a Specific Class from the java.util Package
The above statement is importing the Scanner class available in the java.util package. This class is widely used to take input from the user in a Java program.
What is the Use of java.util Package in Java?
The java.util package in Java consists of several components that provide basic necessities to the programmer. These components include:
Let’s explore these components and their usage in Java:
- Data Structure Classes: The java.util package contains several pre-written data structures like Dictionary , Stack , LinkedList , etc. that can be used directly in the program using the import statements.
- Date and Time Facility: The java.util package provides several date and time-related classes that can be used to create and manipulate date and time values in a program.
- Enumeration: It provides a special interface known as enumeration ( enum for short) that can be used to iterate through a set of values.
- Exceptions: It provides classes to handle commonly occurring exceptions in a Java program.
- Miscellaneous Utility Classes: The java.util package contains essential utilities such as the string tokenizer and random-number generator.
What is Collections Framework in Java?
Collections Framework in Java is a special part of the java.util package that can be used to represent and manipulate collections. It provides easy storage and organization of a group of objects (or collection). It includes pre-written classes, interfaces, and algorithms under a unified architecture.
java.util Package Interfaces
Interface | Description |
---|---|
Collections | Root interface for the Collections API |
Comparator | Provides sorting logic |
Deque | Implements Deque data structure |
Enumeration | Produces enumeration objects |
Iterator | Provides iteration logic |
List | Implements an ordered collection (Sequence) |
Map | Implements Map data structure (key-value pairs) |
Queue | Implements Queue data structure |
Set | Produces a collection that contains no duplicate elements |
SortedSet | Produces a set that remembers the total ordering |
java.util Package Classes
Class | Description |
---|---|
Collections | Provides methods to represent and manage collections |
Formatter | An interpreter for format strings |
Scanner | Text scanner used to take user inputs |
Arrays | Provides methods for array manipulation |
LinkedList | Implements Doubly-linked list data structure |
HashMap | Implements Map data structure using Hash tables |
TreeMap | Implements Red-Black tree data structure |
Stack | Implements last-in-first-out (LIFO) data structure |
PriorityQueue | Implements unbounded priority queue data structure |
Date | Represents a specific instance of time |
Calendar | Provides methods to manipulate calendar fields |
Random | Used to generate a stream of pseudorandom numbers. |
StringTokenizer | Used to break a string into tokens |
Timer, TimerTask | Used by threads to schedule tasks |
UUID | Represents an immutable universally unique identifier |
java.util Package Enum
Enum Class | Description |
---|---|
Formatter.BigDecimalLayoutForm | Used for BigDecimal formatting |
Locale.Category | Used for locale categories |
Locale.FilteringMode | Provides constants for filtering mode selection |
Locale.IsoCountryCode | Used to specify the type defined in ISO 3166 |
java.util Package Exceptions
Exception Class | Description |
---|---|
ConcurrentModificationException | When an object is modified concurrently without permission |
EmptyStackException | Indicates that the Stack is empty |
InputMismatchException | When the user does not provide valid input |
MissingResourceException | Indicates absence of a resource |
NoSuchElementException | Indicates absence of requested element |
PatternSyntaxException | Indicates syntax error in a regular-expression pattern |
IllegalFormatException | Indicates that a format string contains illegal syntax |
Conclusion
- Util package in Java is a built-in package that contains several pre-written utility classes and interfaces.
- The import java.util.*; statement can be used to load the contents of the java.util package in a Java program.
- It consists of components such as data structures, exceptions, enumerations, utility classes, etc.
- It allows us to write clean and maintainable code.
- It contains the Collections Framework which is used to represent and manipulate collections.