A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/util/index.htm below:

Java.util Package Tutorial

Java.util Package Tutorial Java - java.util Package

The java.util package is a standard package of Java SDK. It contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes. This package is very useful as it provides commonly used Collections like ArrayList, HashMap, Set etc. Java.util provides StrinTokenizer classes for String operations and similarly other utility classes for event model handlings, date and time operations and lot more. This reference will take you through simple and practical methods available in java.util package.

Importing java.util Package

Being an inbuilt package of Java, we're not required to download any external library for java.util package and its all classes can be imported using following syntax:

import java.util.*;

Here we've used * operator to import all classes from java.util package and now any class can be used in the program. In case of specific class, for example ArrayList, we can import a class using following syntax:

import java.util.ArrayList;
Why java.util Package is used in Java Programs

Java.util package classes contains utility classes like collections frameworks, String operations utilities, Date and Time operations utilities. Following list shows some of the categories of classes of java.util package.

Important classes in java.util Package

Following is the list of important classes in java.util.package:

Examples of java.util Package

Practice the following examples to learn the concept and usage of java.util package clasess.

Example of java.util.ArrayList

The following program illustrates several of the methods supported by ArrayList −

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {

   public static void main(String args[]) {
      // create an array list
      ArrayList al = new ArrayList();
      System.out.println("Initial size of al: " + al.size());

      // add elements to the array list
      al.add("C");
      al.add("A");
      al.add("E");
      al.add("B");
      al.add("D");
      al.add("F");
      al.add(1, "A2");
      System.out.println("Size of al after additions: " + al.size());

      // display the array list
      System.out.println("Contents of al: " + al);

      // Remove elements from the array list
      al.remove("F");
      al.remove(2);
      System.out.println("Size of al after deletions: " + al.size());
      System.out.println("Contents of al: " + al);
   }
}

This will produce the following result −

Output
Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]
Example of java.util.Date

This Java example demonstrates the from() method of Date class to get Date instance of current time.

package com.tutorialspoint;

import java.time.Instant;

// Import the Date package
import java.util.Date;

// Main public class
public class DateDemo {
   public static void main(String[] args) {

      // create a date of current time
      Date date = Date.from(Instant.now());

      // print the date instance
      System.out.println("Date: " + date.toString());
   }
}
Output

Let us compile and run the above program, this will produce the following result −

Date: Mon Apr 01 10:20:08 IST 2024
Example of java.util.Random

The following example shows the usage of Java Random doubles() method. Firstly, we've created a Random object and then using doubles() we retrieved a stream of random double values and printed first value.

package com.tutorialspoint;

import java.util.Random;
import java.util.stream.DoubleStream;

public class RandomDemo {
   public static void main( String args[] ) {

      // create random object
      DoubleStream randomNoStream = new Random().doubles();
      // print a random double value
      randomNoStream.limit(1).forEach( i -> System.out.println(i));
   }    
}
Output

Let us compile and run the above program, this will produce the following result.

0.5129590222446587
When java.util Package is Used?

Whenever we need to manipulate collections, Strings, perform event handling etc, we can rely on utility classes present in java.util package. This reference has been prepared for the beginners to help them understand the basic functionality related to all the methods available in Java.util package.

Prerequisites

Before you start doing practice with various types of examples given in this reference, I'm making an assumption that you are already aware of basic Java Programming.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4