Hash Table Extract

Hi,

I'm hoping that someone will be able to explain how I extract the first element (in this case the name) from the hashtable in the server code below (case N:).

import java.net.*;

import java.io.*;

import java.util.*;

publicclass Server{

private PrintWriter pw;

private BufferedReader bf;

private ServerSocket ss;

private Socket s;

//Hashtable used to store test data

//Set up hash table with test data

private Hashtable emails;

public Server()throws Exception{

emails =new Hashtable();

emails.put("Ince","ince@hotmail.com");

emails.put("Roberts","roberts@hotmail.com");

emails.put("Timms","timms2@yahoo.com");

emails.put("Rowlands","Rowlands@tiny.com");

emails.put("Eustace","Eustace@yahoo.com");

emails.put("Lord","Lord3@hotmail.com");

System.out.println("...Setting up server socket");

ss =new ServerSocket(1200);

System.out.println("..waiting for connection ");

s = ss.accept();

System.out.println("..connection made");

InputStream is = s.getInputStream();

bf =new BufferedReader(new InputStreamReader(is));

OutputStream os = s.getOutputStream();

pw =new PrintWriter(os,true);

}

publicvoid Run()throws Exception

{

boolean cont =true;

while(cont ==true)//server runs while true

{

String clientLine = bf.readLine();

System.out.println( clientLine );//used to check the connection

switch(clientLine.charAt(0))

{

case'E':

{

String Name ="";

String staffName =null;

Name = clientLine.substring(1,clientLine.length());

staffName = (String) emails.get(Name);

pw.println(staffName);

break;

}

case'N':

{

String email ="";

String emailAddress =null;

email = clientLine.substring(1,clientLine.length());

emailAddress = (String) emails.get(email);

pw.println(emailAddress);

break;

}

case'U':

{

String number = (String) emails.get(clientLine);

if(number ==null)

pw.println(number);

break;

}

case'Q':

pw.close();

bf.close();

s.close();

cont =false;

break;

}

}

}

publicstaticvoid main(String[]args)throws Exception{

{

Server serv =new Server();

serv.Run();

}

}

}

[4974 byte] By [nick211001a] at [2007-11-27 7:36:38]
# 1

1) Use HashMap if possible, rather than Hashtable.

2) HashMap and Hashtable don't have a "first" element. You can call entrySet(), keySet(), or values() and get the first element from the resulting collection's iteration, but that could be any element from the map--first added, last added, middle. It has no relation to the order added or to any sorting.

If you want to retrieve in the order added, use LinkeHashMap.

If you want to retrieve in a sorted order, use a SortedMap, such as TreeMap.

Make sure you implement equals and hashCode correctly, and if you want to sort, implement Comparable or provide a Comparator.

http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf

[url=http://www.onjava.com/lpt/a/3286]Making Java Objects Comparable[/url]

http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

http://www.javaworld.com/javaworld/jw-12-2002/jw-1227-sort.html

jverda at 2007-7-12 19:17:12 > top of Java-index,Java Essentials,New To Java...