• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashtables

 
Ranch Hand
Posts: 647
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
for the first time i am using the hashtable class.
i am getting some user information that i have to store as object & then store it in hashtable.
i.e. first create the object then put it in hash tables.
the object will be user.
parameters username,userID,birthdate,password etc.
object key will be userID.there can be n no of users.
I want to know how to create a hash tabel from this.
please help me..
thanks in advance..
trupti
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashTable users = new HashTable();
users.put(userID, UserObject);
Make sure that your UserObject class has all the other data.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The one other thing I'd mention about Hashtables, is that you must cast the objects once they are back out.

UserObject user = (UserObject) users.get(userID);
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi trupti ,
here i m giving two samples.Hope they will help u to some extent.
i m not able to make user id as a key to the hash table.
i think it will accept string type only.
i have to check it again.
if any queries let me know.
my mail id is : sreek_s@yahoo.com
samples:
import java.io.*;
class userDetails
{
String user_name;
int user_id;
String user_birth;
String user_pwd;

userDetails()
{
user_name = new String();
user_birth = new String();
user_pwd = new String();

}
// copy constructor
userDetails(userDetails copy)
{
user_name = copy.user_name;
user_id = copy.user_id;
user_birth = copy.user_birth;
user_pwd = copy.user_pwd;
}
public String toString() {
return "user name = "+user_name+";user id = "+user_id+";user birth = "+user_birth+";user pwd = "+user_pwd;

}
void setuser_name(String n)
{
user_name=n;
}
void setuser_id(int i)
{
user_id=i;
}
void setuser_birth(String b)
{
user_birth=b;
}
void setuser_pwd(String p)
{
user_pwd = p;
}


String getuser_name()
{
return user_name;
}
int getuser_id()
{
return user_id;
}
// similarly u can write other get methods

}

import java.io.*;
import java.util.*;
class storeDetails
{
public static void main(String[] args)
{
userDetails info = new userDetails();
info.setuser_name(" srikanth");
info.setuser_id(10);
info.setuser_birth("21-08-77");
info.setuser_pwd("sreek");
Hashtable user = new Hashtable();
user.put(info.getuser_name(),info);
System.out.println(user);
}
}

cheers,
Srikanth
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key in a Hashtable must be an Object. If you want to use ID then store it as an Integer instead of int.
 
You didn't tell me he was so big. Unlike this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic