Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-04-03
5
Medium Priority
?
556 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 100 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

876 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