Hi Cecil,
There is nothing shocking me in your code. I think this is the idea.
Alternatively, instead of println(hs) you can use the iterator and
display the elements one by one (but I don't think it is mandatory).
Hash tables allow quick retrieval of the stored objects by a key. The
key allows to compute a "hash code" integer (Java provides all the
mechanic for computing the hash code). The hash code modulus "m" (where
"m" is the size of the hash table) gives the subscript of the element in
the hash table (approximatively, as a few different objects can lead to
the same number). So the access (search) is very fast: about one
operation instead of N/2 in a list or ~log N for a tree (in fact some
more operations, in order to manage duplicated hash code values for
different keys; bigger the hash table is, with more spare space, less
the number of operations is; I don't remember the formula, but I think
there were 3 operations in average for a hash table filled at 80%).
The hash table (possible implementation for sets, key->value maps -
including "Properties" - and so on) is a very interesting structure. A
hash table of hash tables, all of them up to N elements and 80% filling
factor leads to an average of 6 operations to find an element, no matter
how big N is. For a list of lists, the average number of operations is
N²/4 (250 thousands if N = 1000)!
The price to pay is that you cannot predict the order of the items in
the hash table.
I think the idea of the exercise is to play alone with a HashSet,
discover how it works and earn confidence in it.
Hope it helps
mihai
Cecil H a écrit :
Any suggestions or am I doing this right?
On Apr 1, 10:41 pm, Cecil H <[email protected]> wrote:
At the end of the first exercise Mr. Sang suggested we try the
following
4. For your own exercise, please do the following
Create your own NetBeans project named as MyHashSet
Create your own HashSet object with initial capacity of 5
Add the following objects to the newly created HashSet object
2 String objects
2 MyOwnClass object (You will have to create MyOwnClass.java first)
3 Integer objects
Display the HashSet object
****** I'm a little confused by the HashSet data structure
This is what I came up with but I have doubts. Can any one tell me if
I am on the right track or explain it to me a little more.
I have posted the the code and results below:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package MyHashSet;
import java.util.HashSet;
/**
*
* @author Trey-Asus
*/
public class Main {
public static void main (String [] args)
{
HashSet hs = new HashSet(5);
MyOwnClass moc = new MyOwnClass("FirstName", "LastName",
1,2,3);
MyOwnClass moc1 = new MyOwnClass("FirstName2", "LastName2",
4,5,6);
System.out.println(" " + hs.add(moc.getFName()));
System.out.println(" " + hs.add(moc.getLName()));
System.out.println(" " + hs.add(moc.getFirstNumber()));
System.out.println(" " + hs.add(moc.getSecondNumber()));
System.out.println(" " + hs.add(moc.getThirdNumber()));
System.out.println(" " + hs.add(moc1.getFName()));
System.out.println(" " + hs.add(moc1.getLName()));
System.out.println(" " + hs.add(moc1.getFirstNumber()));
System.out.println(" " + hs.add(moc1.getSecondNumber()));
System.out.println(" " + hs.add(moc1.getThirdNumber()));
// Print out the HashSet object
System.out.println(hs);
}
}
/*
=========================================================================== ==
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package MyHashSet;
class MyOwnClass{
String fName;
String lName;
int first;
int second;
int third;
public MyOwnClass (String fName, String lName, int first, int
second, int third)
{
this.fName = fName;
this.lName = lName;
this.first = first;
this.second = second;
this.third = third;
}
public String getFName()
{
return fName;
}
public String getLName()
{
return lName;
}
public int getFirstNumber()
{
return first;
}
public int getSecondNumber()
{
return second;
}
public int getThirdNumber()
{
return third;
}
}
/
*************************************************************************** **/
Results
run:
true
true
true
true
true
true
true
true
true
true
[1, 2, 3, 4, 5, 6, FirstName2, LastName2, FirstName, LastName]
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
To unsubscribe, reply using "remove me" as the subject.