File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hashtable" Watch "Hashtable" New topic
Author

Hashtable

Santhini Nesaraj
Greenhorn

Joined: May 16, 2005
Posts: 9
I want to create a Hashtable and add values for eg.(empname,empid,empsalary) to it and retrieve the values by looping
(for loop). It would be great if someone could give me the solution to this problem really soon because I have been working on it for two days. Links Tutorials and books does not help.

Thanks in advance!!
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Links Tutorials and books does not help

could you please post the code you'v got so far so that ranchers can get to help you?


java amateur
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Santhini, you posted the same question in the beginners forum, try not to cross post. I gave you a link to the Collections tutorial which should have at least got you started. Post some code and maybe someone can help you progress.
Santhini Nesaraj
Greenhorn

Joined: May 16, 2005
Posts: 9
This is the requirement that I have got that I posted I have no idea how to start it.
Insert the values into the hashtable and retrieve them thro loop.
This is what I have done so far:

import java.util.ArrayList;
import java.util.Hashtable;

public class Information {
public static void main(String[] args) {
ArrayList al[]= new ArrayList[2];

I dont know how to add the values to it. The arraylist has to be converted to hashtable it seems and retieve the value thro loop.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
I dont know how to add the values to it
have you checked the API? or else java sun tutorial?
Santhini Nesaraj
Greenhorn

Joined: May 16, 2005
Posts: 9
import java.util.ArrayList;
import java.util.Hashtable;

public class Information {
public static void main(String[] args) {
ArrayList al[]= new ArrayList[2];
al[0]=

(i am stuck over here)

}
}
how do i add the values (empname, empid,empsalary ) to it.
I have refferred a lot of books and sites nothing helps. I want the exact answer for it.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673

Right so now we have a skeleton class which compiles and tells us the size of our collection which happens to be zero. What next? Well read the Hashtable API and find out how to add items to the collection. Remember we are working in Key/Value pairs so the answer isn't as simple as just using an add method but it is still pretty simple if you look hard enough through the API.
Also a thing of note here is that I programmed to the Map interface and not the concert class Hashtable.
[ June 02, 2005: Message edited by: Nigel Browne ]
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
Here's some code that uses a HashMap:


Here's the output:



Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Santhini Nesaraj:
I want to create a Hashtable and add values for eg.(empname,empid,empsalary) to it and retrieve the values by looping
(for loop). It would be great if someone could give me the solution to this problem really soon because I have been working on it for two days. Links Tutorials and books does not help.

Thanks in advance!!


One thing that will help is if you specify exactly what data needs to be stored in the Hashtable. Do you only want to store the name, id, and salary for ONE employee? Or do you need to store multiple employees? You should be able to write code that answers the first question using the examples already given. However, if you want the second question, you might need to think about it further. The solution that comes to mind is to create an Employee class that you can store in the Hashmap based on some key. If this is what you need, then I see two tasks at the moment: creating the Employee class and deciding what to use as a key in the Hashmap. I'll let you think about it from here. Let us know what you come up with. If you get stuck, please come back with some specific questions.

HTH

Layne


Java API Documentation
The Java Tutorial
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
Originally posted by Santhini Nesaraj:
I want to create a Hashtable and add values for eg.(empname,empid,empsalary) to it and retrieve the values by looping
(for loop). It would be great if someone could give me the solution to this problem really soon because I have been working on it for two days. Links Tutorials and books does not help.

Thanks in advance!!



Hashtable ht = new Hashtable();
ht.put("one","hello");
ht.put("two","Nischal");
ht.put("three","here");

Enumeration e = ht.keys();
while(e.hasMoreElements())
{
String key = e.nextElement();
System.out.println("Key is "+key+" value is "+ht.get(key));
}
}



Thnx
Krishnan Loganathan
Greenhorn

Joined: Apr 24, 2004
Posts: 23
Hi,
I cannt able to get your question. Whether u want to store all the details about the employee ( id,name, age, salary etc...) or any one in the hashtable ?

If u want to store all the details about the employee means, please create a class Employee with id, name, age salary as the attributes and store the employee object in the hash table as follows


public class Employee {
private String id = null;
private String name = null;
private double salary = null;

public Employee(String id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}

public void setId(String id) {
this.id = id;
}

public String getId() {
return id;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setSalary(double salary) {
this.salary = salary;
}

public double getSalary() {
return salary;
}

}


import java.util.*;

public class HashTableDemo {

public static void main(String[] args) {
Hashtable empDemo = new HashTable();
Employee emp1 = new Employee("1","one",1.0);
Employee emp2 = new Employee("2","two", 2.0);

empDemo.put(emp1.getId(), emp1);
empDemo.put(emp2.getId(), emp2);

Iterator keyIterator = empDemo.keySet().iterator();
while(keyIterator.hasNext()) {
String key = (String) keyIterator.next();
Employee emp = (Employee) empDemo.get(key);
System.out.println(" Id == > " + emp.getId() + " Name ==> " + emp.getName() + " Salary ==> " + emp.getSalary());
}
}
}


The Output :-

Id ==> 1 Name ==> One Salary ==> 1.0
Id ==> 2 Name ==> Two Salary ==> 2.0


Regards,
Loga
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hashtable
 
Similar Threads
Hashtable elements to vector
Sorting HashTable by values
unable to get hashtable values from javabean to jsp page
Traversing a HashSet in a datatable
HashSet will allow null values?