Hashtable with JDK1.5

R

R.Venkat

i have 2 data files each with a string and an integer separated by a
space/tab. i have to compare the integer values based on the string as
the key.i wrote the following piece of code:
-------------------------------Start Code---------------------------
import java.util.*;
import java.io.*;

class DataIntegrityTest
{
public static void main(String[] args) throws Exception
{
Hashtable lengths1 = new Hashtable<String,Integer>();
Hashtable lengths2 = new Hashtable<String,Integer>();

BufferedReader in=new BufferedReader(new
FileReader("d:\\data1.txt"));
String s="";
while((s=in.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(s," ");
String columnName=st.nextToken();
int length = Integer.parseInt(st.nextToken());
lengths1.put(columnName,length);
}
in.close();

BufferedReader in1=new BufferedReader(new
FileReader("d:\\data2.txt"));
String s1="";
while((s1=in1.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(s1," ");
String columnName=st.nextToken();
int length = Integer.parseInt(st.nextToken());
lengths2.put(columnName,length);
}
in1.close();

for(Enumeration e = lengths1.keys();e.hasMoreElements();)
{
String key = (String) e.nextElement();
if(((Integer)lengths1.get(key)).intValue() <
((Integer)lengths2.get(key)).intValue())
{
System.out.println(key+" "+ lengths1.get(key) +" "+
lengths2.get(key));
}
}
}
}
-------------------------------End Code---------------------------
the for loop that i've used does not fully use the new JDK 1.5 foreach
loop. Any tips as how to enhance this piece of code so that it uses
the 'foreach' loop in JDK1.5?
Thanks
 
D

Doug Pardee

i have 2 data files each with a string and an integer separated by a
space/tab. i have to compare the integer values based on the string as
the key.i wrote the following piece of code:
-------------------------------Start Code---------------------------
import java.util.*;
import java.io.*;

class DataIntegrityTest
{
public static void main(String[] args) throws Exception
{
Hashtable lengths1 = new Hashtable<String,Integer>();
Hashtable lengths2 = new Hashtable<String,Integer>(); [snip]
for(Enumeration e = lengths1.keys();e.hasMoreElements();)
{
String key = (String) e.nextElement();
if(((Integer)lengths1.get(key)).intValue() <
((Integer)lengths2.get(key)).intValue())
{
System.out.println(key+" "+ lengths1.get(key)
+" "+ lengths2.get(key));
}
}
}
}
-------------------------------End Code---------------------------
the for loop that i've used does not fully use the new JDK 1.5 foreach
loop. Any tips as how to enhance this piece of code so that it uses
the 'foreach' loop in JDK1.5?
Thanks

First let's clean up the generics:

Hashtable<String,Integer> lengths1 =
new Hashtable<String,Integer>();
Hashtable<String,Integer> lengths2 =
new Hashtable<String,Integer>();
[snip]
for(Enumeration<String> e = lengths1.keys();e.hasMoreElements();)
{
String key = e.nextElement();
if((lengths1.get(key)).intValue() <
(lengths2.get(key)).intValue())

Now let's take advantage of auto-unboxing -- realizing that in so
doing, we're guaranteeing that we haven't put any null values in
either hashtable:

if(lengths1.get(key) < lengths2.get(key))

And now the change that you originally asked for -- using the enhanced
for loop:

for(String key: lengths1.keySet())
{
if(lengths1.get(key) < lengths2.get(key))

Notice the change from keys() to keySet().

Now isn't that loop a lot nicer?
 
R

R.Venkat

didnt compile.........:(.......
Changed the code like this to work.......
Am i missing something?
-------------------------------------------------------------------------------
for(Object key : lengths1.keySet())
{
if(((Integer)lengths1.get((String)key)).intValue() >
((Integer)lengths2.get((String)key)).intValue())
{
System.out.println(key+" "+ lengths1.get((String)key) +" "+
lengths2.get((String)key));
}
}
 
B

Bjorn Abelli

"R.Venkat" ...
didnt compile.........:(.......
Changed the code like this to work.......
Am i missing something?

Probably.

Did you miss Doug's "cleaning up" of the generics to define the types of
keys and values also on the left side?

Hashtable<String,Integer> lengths1 =
new Hashtable<String,Integer>();
Hashtable<String,Integer> lengths2 =
new Hashtable<String,Integer>();

// Bjorn A
 
R

R.Venkat

Found out what i missed.......i hadnt used
Hashtable<String,Intger> lenghts1 = new Hashtable<String,Integer>();
had used
Hashtable lenghts1 = new Hashtable<String,Integer>();
Thanks,
 
T

Tor Iver Wilhelmsen

Hashtable lenghts1 = new Hashtable<String,Integer>();

Yes, that would make the reference "non-generic" even if the object
is. And the compiler uses the reference to decide what it can do.
 
Joined
Jan 27, 2010
Messages
27
Reaction score
0
Enhanced for loop in java

Old form of looping.

for (int i=0; i < nameArray.length; i++)
{
System.out.println("Name: " + array);
}
With Java SE 5.0. an enhanced looping would be like

for (String name : nameArray)
{
System.out.println("Name: " + name);
}

where nameArray is an array of Strings

Hope u find this tip very useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,840
Latest member
BrendanG78

Latest Threads

Top