Wednesday, August 2, 2023

java.util Package

 

                           java.util Package

 

String Tokenizer

The processing of text often consists of parsing a formatted input string. parsing division of text into a set of discrete parts, or tokens, which in a certain sequence can convey a semantic meaning. The String Tokenizer class provides the first step in this parsing process, often called the lexer (lexical analyzer) or scanner. String Tokenizer implements the Enumeration interface. Therefore, given an input string, you can enumerate the individual tokens contained in it using String Tokenizer.

The String Tokenizer constructors are shown here:

String Tokenizer (String str)

String Tokenizer(String str, String delimiters)

String Tokenizer(String str, String delimiters, bollean delimAs Token)

 

 

//Demonstrate String  Tokenizer.

File name: STDemo.java

import java.util.StringTokenizer;

class STDemo {

         static String in = "title=Java: The Complete Reference;"+

       "author=Schildt;"+

        "publisher=Osborne/McGraw-Hill;"+

       "copyright=2002";

      public static void main(String args[]) {

           StringTokenizer st=new StringTokenizer(in,"=;");

          while(st.hasMoreTokens())  {  

                               String key=st.nextToken();

                              String val=st.nextToken();

                       System.out.println(key+"\t"+val);

                        }

           }

}

OUTPUT

 


 The Enumeration Interface

Enumeration specifies the following two methods:

boolean hasMoreElements( )

Object nextElements( )

Vector         

vector implements a dynamic array. It is a strategy to minimize reallocation and wasted space.

 

Here are the vector constructors:

Vector()

Vector(int size)

Vector(int size, int incr)

Vector(collection c)

 

Method                                          Explanation

addElement()              Add an elements

                                          

elementAt()                Obtained the element

                                      at a specific location.

firstElement()              Fist element in the vector

lastElement()               last element in the vector

indexOf()                     index of an element

lastindexOf()

removeElement()         Remove an element

removeElementAt()

 

/*program to illustrate Dynamic Array that is VECTORS*/

File name: VectorDemo.java

import java.util.*;

 public class VectorDemo {

    public static void main(String []args) {

         Vector<String> vectStr = new Vector<String>(); //Declaring a Vector in Java

         vectStr.add("apple");

        vectStr.add("orange");

        vectStr.add("graphs");

        //Displaying vector elements

        System.out.println("The Vector elements are:");

        System.out.println("");

        for (int i=0;i<vectStr.size();i++){

                 System.out.println(vectStr.get(i));

              }

 }   

}

 

OUTPUT

 


Hash Table

One of the ways to store information for fast lookup is a hash table.A Hash Table stores information using a special calculation on the object to be stored.

Example

File name : HashDemo.java

class HashDemo {

       public static void main(String args[]) {

               String s1="hello";

               String s2="Hello";

               System.out.println("The hash code for "+s1+" is "+s1.hashCode());

               System.out.println("The hash code for "+s2+" is "+s2.hashCode());

             }

}

 

The output appears as given below:

 


0 comments:

Post a Comment