Solved

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

Posted on 2002-04-03
5
548 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java - Why doesn't this JFrame work 3 58
Java DateChooser? 3 36
hibernate insert example 13 28
ejb on wildfly 5 19
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

776 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