Java Toolkit Class

Java Toolkit Class

The java.util package has many practical classes, interfaces, and exceptions.

Vector class, stack class, hash table, enumeration interface, calendar class, random function class, mapping interface and attribute class.

Vector class

vector is heterogeneous, which can store different objects and dynamically increase storage capacity

All vector operations are performed on Object classes. Object objects retrieved from vector space need to be transformed downward.

The number of elements is less than or equal to the capacity.

Only objects can be put in. The basic data type should use its wrapper class.

common method

Vector()Construct empty vector
Vector(int initialCapacity)Initializes a vector of the specified size
Vector(int initialCapacity,int capacityIncrement)capacityIncrement Value of each expansion: 0 Double each expansion
add()Add Element
addElement()Add to End
capacity()Return capacity size
elementAt()Returns the object element at the specified location
elements()Returns an object for each vector element that is allowed to access
firstElement()Returns the first object element
get()Returns the object element at the specified location
indexOf()Find the location of the specified object element. If it does not exist, return - 1
isEmpty()Judge whether it is empty
remove()Clear the element of the specified position or object
removeAllElements()Clear All Elements
setElementAt()Set the specified object element at the specified location to overwrite the original
setSize()Set capacity size
size()Number of returned elements
trimToSize()Set the actual capacity as the internal cache capacity
toArray()Return vector element array
subList()Return vector position
removeElementAt()Delete the object where the index refers
import java.util.Vector;

public class VectorDemo {
    public void test(){
        Vector vectTemp = new Vector(4,3);
        System.out.println("Number of elements: " + vectTemp.size());
        System.out.println("Capacity size: " + vectTemp.capacity());
        // Add Element
        vectTemp.add(new Integer(3));
        vectTemp.addElement(new Double(4.0));
        vectTemp.insertElementAt(new Float(5.0f),1);
        vectTemp.add("hello world");
        System.out.println("Number of elements: " + vectTemp.size());
        System.out.println("Capacity size: " + vectTemp.capacity());
        // toString() will output all elements as strings
        // 2nd element

    public static void main(String[] args) {
        new VectorDemo().test();

Stack class


Can receive any type of object

common method

Stack()Construct an empty stack
empty()Judge whether it is empty
peek()Get the element at the top of the stack
pop()Delete the element at the top of the stack and return
push(Object item)Push
search(Object item)Check whether there is a specified object in the stack
import java.util.Stack;
public class StackDemo {
    public void test(){
        Stack stkTemp = new Stack();
        System.out.println("Whether the stack is empty: "+stkTemp.empty());
        stkTemp.push(new Integer(2));
        // View Element Location
        System.out.println("element wkk position:" +"wkk"));
        System.out.println("Element 2 Position:" + Integer(2)));
        // Top stack element peek
        //Top stack element pop

    public static void main(String[] args) {
        new StackDemo().test();

  • The top element of the stack is at 1
  • The position of the next element at the top of the stack is 2
Hashtable class

The Dictionary class is an abstract class that provides a unified interface for dictionary tables.

Hashtable is a set of key value pairs and a concrete implementation of the abstract Dictionary class. It can store any object.

common method

Hashtable()Construct an empty hash table
containsKey()Whether the object is a key in the table
containsValue()Whether the object is a value in the table
elements()Returns the object that allows access to each object element
get()Returns the corresponding value according to the specified key
put(Object key, Object value)Put a set of key value pairs into the table
keys()Returns the object that allows access to each key
size()Number of keys
remove()Delete the corresponding key value pair according to the specified key

Each key corresponds to a unique key value pair, and the key in a hash table cannot be duplicate

Any object can act as a key, and any object can also act as a value.

import java.util.Hashtable;
public class HashtableDemo {
    public void test(){
        Hashtable hshTemp = new Hashtable();
        hshTemp.put("001",new Integer(1));
        hshTemp.put("002",new Integer(2));

        // Number of elements
        System.out.println("Number of elements: " + hshTemp.size());
        // Get value
        // Judge whether there is a value 1
        System.out.println("Valuable 1: " + hshTemp.containsValue(new Integer(1)));
        // Hashtable to String

    public static void main(String[] args) {
        new HashtableDemo().test();

Enumeration interface

Enumeration corresponds to a collection of objects, which is generally implemented with Vector, Stack, Hashtable or other classes.

Use their elements() method to get a collection of elements.


Common methods:

hasMoreElements()Determine whether the object has multiple elements
nextElement()Get the next object in the object collection
import java.util.Vector;
import java.util.Enumeration;

public class EnumerationDemo {
    public void test(){
        Vector vctTemp = new Vector(3,2);
        Enumeration enumTemp ;
        // Add String
        for(int intTemp = 1;intTemp < 4;intTemp++){
            String string = "character string" + intTemp;

        enumTemp = vctTemp.elements();
            String  strElement = (String)enumTemp.nextElement();
            System.out.println("Element value: " + strElement);

    public static void main(String[] args) {
        new EnumerationDemo().test();

Enumeration is an interface, so you cannot use the new() method to define an instance

Calendar class

Working principle: obtain the system time, time zone and other data from the local computer system to obtain specific time information.

Common Attributes

DAY_OF_MONTHThe day of the month
DAY_OF_WEEKThe day of the week
DAY_OF_YEARThe current day
WEEK_OF_YEARWeek of the year
Calendar()Constructor, but protected, cannot be directly new
getInstance()Get an instance of the calendar class
get(int field)Get the value of an attribute. The field is the attribute defined in the calendar
set(int field)Set the value of a property
getTime()Return the date corresponding to the calendar (date)
Clear(int field)Clear the value of a property of the calendar class
import java.util.Calendar;

public class CalendarDemo {
    public void test(){
        Calendar cldTemp = Calendar.getInstance();
        // Print basic calendar information
        System.out.println("Year:" + cldTemp.get(Calendar.YEAR));
        System.out.println("month: " + cldTemp.get(Calendar.MONTH));
        System.out.println("day: " + cldTemp.get(Calendar.DATE));
        // Specific calendar information
        System.out.println("upper/afternoon: " + cldTemp.get(Calendar.AM_PM));
        System.out.println("hour: " + cldTemp.get(Calendar.HOUR));
        System.out.println("minute: " + cldTemp.get(Calendar.MINUTE));
        System.out.println("second: " + cldTemp.get(Calendar.SECOND));
        System.out.println("millisecond: " + cldTemp.get(Calendar.MILLISECOND));
        // Convert to Array
        System.out.println("array: " + cldTemp.toString());

    public static void main(String[] args) {
        new CalendarDemo().test();
  • The whole calendar is stored in an array
  • The constructor of the calendar class is protected, so it cannot be directly created with new, but obtained with getInstance()
Random class

The Random class is a pseudorandom number generator.

common method

RandomConstruction method
nextBoolean()Returns a boolean variable true/false
nextDouble()Returns a double number 0.0 - 1.0
nextFloat()0.0 - 1.0
nextGaussian()Generate a double precision random number 0.0 - 1.0 from a pseudo discrete Gaussian distribution
import java.util.Random;

public class RandomDemo {
    public void test() {
        Random rndTemp = new Random();
        System.out.println("Boolean: " + rndTemp.nextBoolean());
        System.out.println("double: " + rndTemp.nextDouble());
        System.out.println("float: "+ rndTemp.nextFloat());
        System.out.println("long: " + rndTemp.nextLong());
        System.out.println("int: " + rndTemp.nextInt());
        // Integer within 10
        System.out.println("int(10 within): " +rndTemp.nextInt(10));

    public static void main(String[] args) {
        new RandomDemo().test();

  • nextInt(n) returns an integer of [0, n) ->n>0
Map Interface

The Map interface defines a framework for implementing basic mapping data structures.

The Hashtable class implements the Map interface.

The Map interface defines the method of storing and retrieving information on the basis of indexes. The keys in the Map interface can be of any type.

Common methods:

It is basically for the key and the object corresponding to the key

clear()Clear All
containsKey()Whether there is a specified key
containsValue()Whether there is a specified value
entrySet()Returns the collection corresponding to the mapping object
hashCode()Returns the hash code of the mapping object itself
isEmpty()Is it empty
keySet()Collection of return keys
put()Add Key Value Pair
putAll(Map t)Add all the mappings in the known mapping t to this mapping
remove()Delete mapping by key
size()Number of returned maps
values()Collection of return values

Map is an interface that needs to be implemented.

The HashMap class implements the Map interface.

import java.util.HashMap;
import java.util.Set;
import java.awt.Rectangle;

public class HashMapDemo {
    public void test(){
        HashMap hmTemp = new HashMap();
        // Construct arrays and pre store key objects
        Object objectKeyTemp [] = new Object[3];
        hmTemp.put("small",new Rectangle(0,0,5,5));
        hmTemp.put("midium",new Rectangle(0,0,15,15));
        hmTemp.put("large",new Rectangle(0,0,30,30));
        // number
        System.out.println("number: " + hmTemp.size());
        // Whether the key small is included
        System.out.println("Does it contain small key: " + hmTemp.containsKey("small"));
        System.out.println("Does it contain Rectangle(0,0,15,15): "+hmTemp.containsValue(new Rectangle(0,0,15,15)));
        // Returns the collection of keys in the map
        Set setTemp = hmTemp.keySet();
        objectKeyTemp = setTemp.toArray();
        for (int i = 0; i < objectKeyTemp.length; i++) {
            if(objectKeyTemp[i] instanceof String) {
                String s = (String) objectKeyTemp[i];
                System.out.println("key Value of: " + s);

    public static void main(String[] args) {
        new HashMapDemo().test();

Properties Class

Mainly used to read Java configuration files

In Java, the configuration file is usually a. properties file, which is configured as a key value pair.

The Properties class is a subclass of the Hashtable class. It has all the methods of the Hashtable class object

common method

getProperty()Returns the attribute value based on the specified key or value
load(InputStream inStream)Read the key and its specified object from the specified input stream for loading
propertyNames()Return the key in the property object to the enumeration interface
setProperty(String key,String value)Set a set of attribute object pairs in the attribute object, where the key and value are of String type
store(OutputStream out,String header)Load the attribute object in the attribute object into the specified output stream, and specify the name at the same time

Configuration file:

age = 25
name = wkk
package com.wkk;

import java.util.Enumeration;
import java.util.Properties;
public class PropertiesDemo {
    public void test(){
        Properties ppTemp = new Properties();
            ppTemp.load(new FileInputStream("src/com/wkk/"));
        }catch (FileNotFoundException e){
        }catch(IOException e){
        Enumeration content = ppTemp.propertyNames();
            String key = (String)content.nextElement();
            String value =ppTemp.getProperty(key);
            System.out.println(key + ":" + value);

    public static void main(String[] args) {
        new PropertiesDemo().test();

Tags: Java Algorithm programming language

Posted by SoreE yes on Wed, 21 Sep 2022 22:24:03 +0530