To import the java.util
package in Java, you will need to include the following import statement at the beginning of your code:
import java.util.*;
The java.util
package is a standard Java package that contains a collection of utility classes, including collections (e.g., lists, sets, maps), dates and times, random numbers, and miscellaneous utility classes (e.g., observer and event classes).
Here is an example of how you might use some classes from the java.util
package in your code:
import java.util.*; public class UtilExample { public static void main(String[] args) { // Create a random number generator Random random = new Random(); // Generate a random integer int randomInt = random.nextInt(); System.out.println("Random integer: " + randomInt); // Create a list List<String> list = new ArrayList<>(); list.add("item1"); list.add("item2"); list.add("item3"); // Iterate over the list for (String item : list) { System.out.println(item); } // Create a map Map<String, Integer> map = new HashMap<>(); map.put("key1", 1); map.put("key2", 2); map.put("key3", 3); // Get a value from the map int value = map.get("key2"); System.out.println("Value for key2: " + value); } }
This code uses the Random
class from java.util
to generate a random integer, the ArrayList
and for-each
loop constructs from java.util
to iterate over a list, and the HashMap
class from java.util
to create and retrieve values from a map.