Solved

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

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

744 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

13 Experts available now in Live!

Get 1:1 Help Now