Solved

How do i use an iterator to get data from a hashtable?

Posted on 2002-04-03
5
547 Views
Last Modified: 2010-05-18
I am having a bit of trouble understanding collections in Java and how they relate to iterators.

I have a hashtable  - ResTable(res, value); and i need to use an iterator to go through each element in my hash table and copy the value res into a string array and also copy value into a separate int array.  How do i go about defining the iterator for my hashtable?
 
 
private static void CopyToArray(){
       
// I know that i have to define my collection by something like...  
   Collection myCollection = .....;  //  do i assign ResTable equal to mycollection?    How??
//Then i can say...
   Iterator i = myCollection.iterator();
   while (i.hasNext()){
      System.out.println(i.next());
      // Do what i need to to convert data from hash table to array
      //Cast datatype etc
   }
}
 

Any seggestion in either code format, psuedocode or basic english would be much appreciated.

Nutty
0
Comment
Question by:Nuttman
5 Comments
 
LVL 3

Expert Comment

by:rjackman
ID: 6917143
it seems that u want to get the values without knowing the keys in the table then u can do the following
Set keys = ResTable.keySet();
for(Iterator itr = keys.iterator();itr.hasNext();)
{
   String key = (String)itr.next();
   System.our.println("Keys---->"+key);
   System.out.println("Value--->"+ResTable.get(key));
}

cheers
RJ
0
 
LVL 6

Expert Comment

by:kotan
ID: 6917259
You don't need to assign ResTable to Collection. Because it is already a Collection.

To extract the keys of ResTable into String array,
String[] strArr = (String[]) ResTable.keySet().toArray(new String[0]);

For values, You can only copy into Integer object but not int primitive type.

Integer[] iArr = (Integer[]) ResTable.values().toArray(new Integer[0]);
0
 
LVL 1

Accepted Solution

by:
jose_tijo earned 25 total points
ID: 6917692
/*
 * HashToArray.java
 *
 * Created on 04 April 2002, 10:47
 */

/**
 *
 * @author  Tijo Jose
 * @version 1.0
 */
public class HashToArray {

    java.util.Hashtable hashTable;
   
    String[] stringArray;
   
    int[] intArray;
   
    Object[] objArray;
   
    java.util.Enumeration enumeration;
   
    public int Count;
   
    /** Creates new HashToArray */
    public HashToArray() {
        hashTable = new java.util.Hashtable();
        hashTable.put("One", "1" );
        hashTable.put("Two", "2");
        hashTable.put("Three", "3");
        stringArray = new String[hashTable.size()];
        intArray = new int[hashTable.size()];
    }

    /**
    * @param args the command line arguments
    */
    public static void main (String args[]) {
        HashToArray hta = new HashToArray();
        hta.CopyToArray();
    }

    public void CopyToArray() {
        objArray = hashTable.values().toArray();
        Count = 0;
        while(objArray.length > Count) {
            intArray[Count] = (new Integer(objArray[Count].toString())).intValue();
            Count ++;
        }
        Count = 0;
        while(intArray.length > Count) {
            System.out.println("Int - > " + intArray[Count]);
            Count++;
        }
       
        Count = 0;
        stringArray = (String[])hashTable.keySet().toArray(new String[0]);
        if(this.stringArray != null) {
            while(stringArray.length > Count) {
                System.out.println("String - > " + stringArray[Count]);
                Count++;
            }
        }
    }
}
0
 
LVL 5

Expert Comment

by:vemul
ID: 7898939
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to jose_tijo

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7959864
Comment from expert accepted as answer

Computer101
E-E Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now