Question:
Java question - How do you store data in hashtables and Vector of objects.?
Woot woot
2010-02-18 13:33:44 UTC
Hi Everyone,

so my assignment is to compare the efficiency of how long it takes to search for a character string (Persons name) and integer (telephone number) pair in a hastable versus a vector of objects in Java. my question is how do i store the data in a hashtable and store that same data as a vector of objects.

thanks for your help!
Three answers:
Kaushalya Damitha
2010-02-18 19:14:02 UTC
HI

Can you explain the Q more ?

what is the Key ? the phone No or Hash of Name ?



i think i can help. give me a buzz with more in info ill post the answer





Kaushalya Damitha :)
Jack
2010-02-18 13:42:26 UTC
If I remember correctly a hash table is basically an array (or vector) of linked lists.



So the values of this array are basically your 'keys' which is fancy compsci talk for 'a value that will narrow down your data significantly.' Good examples are the model year in a car parts database, or the first letter of someone's last name in an address book. The linked list that follows thereafter is your actual data in an order of your choosing. (Alphabetical, whatever.)



So to create a linked list and add data to it, you basically create an array with a pointer to the first node and populate it from there. Piece of cake.



In a vector (or array, whatever you want to call it) you just define your length and populate it. Also simple.



Just so you get your answer now, the more data you've got, the less time it takes to find a record in a hash table than a straight-up array.
karma
2016-05-31 09:59:01 UTC
Use ArrayList al = new ArrayList(); Player is a class that has class Player { String name; String team; //? //whatever else; } Now, ArrayList is the same as Vector. Vector adds overhead because it synchronizes (unnecessary). With either ArrayList or Vector you can get by Object to search, use a method private Player getPlayer( String game ) { for( Player p : al ) { if ( p.getGame() == game ) return p; } return null; } Player playa = al.get( p ); playa.addGame("South Block");


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...