10:40 pm - Monday May 21, 2018

Hashtable example program for Get() Collection values

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

/**

*@author :CandidJava.com

*@descriptions : This program explain to get the collection of values from Hashtable

*/

import java.util.Enumeration;

import java.util.Iterator;

import java.util.Hashtable;

import java.util.Collection;

class GetCollectionOfValuesFromHashtable{

public static void main(String[] args) {

//creating Hashtable Object

Hashtable ht = new Hashtable();

ht.put("1","One");

ht.put("2","Two");

ht.put("3","Three");

//Initializing collection variables to get the values from the table

Collection c = ht.values();

System.out.println("Values of Collection created from Hashtable are :");

//to get the next values sequentially using iteration

Iterator itr = c.iterator();

while(itr.hasNext())

System.out.println(itr.next());

//to remove the variable from the collection using remove() method

c.remove("One");

System.out.println("Hashtable values after removal fromCollection are :");

//enumeration is used to call the values of hashtable

Enumeration e = ht.elements();

while(e.hasMoreElements())

System.out.println(e.nextElement());

}

}

output:

D:\CandidJava\Util\HashTable>javac GetCollectionOfValuesFromHashtable.java

Note: GetCollectionOfValuesFromHashtable.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

D:\CandidJava\Util\HashTable>java GetCollectionOfValuesFromHashtable

Values of Collection created from Hashtable are :

Three

Two

One

Hashtable values after removal fromCollection are :

Three

Two

BACK

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply