Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashtable

 
Santhini Nesaraj
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know how to add the values to it
have you checked the API? or else java sun tutorial?
 
Santhini Nesaraj
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some code that uses a HashMap:


Here's the output:

 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));
}
}

 
Krishnan Loganathan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic