Solved

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

Posted on 2002-04-03
5
551 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

738 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