2 replies to this topic
#1
Posted 26 October 2010 - 09:21 PM
Hi.. i was searching for how to iterate in hashtables. i have found this artical and it says about 2 ways of iterations. i want to find the most efficient way of iterating in a hashtable. i have shown the 2 ways below in blue(1) and green(2) colors. do you have any idea about which one is the most common way of hashtable iteration? what is more efficient?
Hashtable balance = new Hashtable();
Enumeration names;
String str;
double bal;
balance.put("John Doe", new Double(3434.34));
balance.put("Tom Smith", new Double(123.22));
balance.put("Jane Baker", new Double(1378.00));
balance.put("Todd Hall", new Double(99.22));
balance.put("Ralph Smith", new Double(-19.08));
names = balance.keys();
while(names.hasMoreElements()) {
str = (String) names.nextElement();
System.out.println(str + ": " +
balance.get(str));
Hashtable balance = new Hashtable();
String str;
double bal;
balance.put("John Doe", new Double(3434.34));
balance.put("Tom Smith", new Double(123.22));
balance.put("Jane Baker", new Double(1378.00));
balance.put("Todd Hall", new Double(99.22));
balance.put("Ralph Smith", new Double(-19.08));
Set set = balance.keySet(); // get set-view of keys
// get iterator
Iterator itr = set.iterator();
while(itr.hasNext()) {
str = (String) itr.next();
System.out.println(str + ": " +
balance.get(str));
|
|
|
#2
Posted 27 October 2010 - 06:08 AM
Neither of them need to be efficient in this sample code, an iterator can do much more than a while loop on the array keys, so it would be naturally more efficient than your own implementation in a large set of code.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 28 October 2010 - 10:06 AM
If you need the key and the value you should use entrySet, it is much faster.
Otherwise you have to iterate twice (once to get to the key, once to find the value matching the key).
I think HashMap is (or can be) faster than Hashtable also, because it is not synchronized (which often you don't need)...
Otherwise you have to iterate twice (once to get to the key, once to find the value matching the key).
I think HashMap is (or can be) faster than Hashtable also, because it is not synchronized (which often you don't need)...
HashMap<String, Double> balance = new HashMap<String, Double>();
balance.put("John Doe", new Double(3434.34));
balance.put("Tom Smith", new Double(123.22));
balance.put("Jane Baker", new Double(1378.00));
balance.put("Todd Hall", new Double(99.22));
balance.put("Ralph Smith", new Double(-19.08));
Iterator<Entry<String, Double>> itr = balance.entrySet().iterator();
while (itr.hasNext()) {
Entry<String, Double> entry = itr.next();
System.out.println(entry.getKey() + ": " + entry.getValue());
}
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









