Java Collections – Sets

There are two java data types I’m going to cover, hashsets and treesets. 

Hashing

A hash table is another type array.  A hash table consists of keys and values.  Each key must be unique and  they are used to reference the data stored in the value field.  Here is a sample snippet for a hash table.

public class HashSet
private HashSet hash;

// Builds a hash set from a string array
public HashSet(String[] arr){
this.hash = new HashSet();

for(String str: arr)
this.hash.add(str);
}

Binary Trees

Trees are implemented in a similar fashion as hash sets.  Using TreeSet will organize your data into a binary tree.

public class TreeSet{
private TreeSet tree;

//Builds binary tree from string array
public TreeSet(String[] arr){

this.tree = TreeSet();
for (String str: arr)
this.tree.add(str);
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s