I needed to create a LinkedSet for a data structures assignment based on:
import java.util.*;
public class LinkedSet<Item> implements Set<Item>, Iterable<Item>{
private Node<Item> firstNode; //reference to first node in this set
private Node<Item> lastNode; //reference to the last node in this set
private int length; //number of items in this set
...
}
The only thing I'm confused on is the Set interface has a hashCode() function that I need to implement. I know its going to be
public int hashCode() {
//some code
}
For example, if the driver code was:
import java.util.*;
public class DriveLinkedSet{
public static void main(String[] args){
LinkedSet<String> set = new LinkedSet<String>();
LinkedSet<String> set2 = new LinkedSet<String>();
System.out.println("set = " + set);
System.out.println("Is set empty? " + set.isEmpty());
System.out.println("The hashcode of set = " + set + " is " + set.hashCode());
System.out.println("The size of set is " + set.size() + "\n");
System.out.println("Add 'Jan' to set. Is this ok? " + set.add("Jan"));
System.out.println("Add 'Nik' to set. Is this ok? " + set.add("Nik"));
System.out.println("Add 'Leo' to set. Is this ok? " + set.add("Leo"));
System.out.println("Add 'Jan' to set. Is this ok? " + set.add("Jan"));
System.out.println("Add 'Cal' to set. Is this ok? " + set.add("Cal"));
System.out.println("Add 'Bob' to set. Is this ok? " + set.add("Bob"));
System.out.println("Add 'Leo' to set. Is this ok? " + set.add("Leo") + "\n");
System.out.println("set = " + set);
System.out.println("Is set empty? " + set.isEmpty());
System.out.println("The hashcode of set = " + set + " is " + set.hashCode());
}
The hash code part would output: The hashcode of set = {Jan, Nik, Leo, Cal, Bob} is 363296
Any help or guidance to the right direction would be appreciated =)

New Topic/Question
Reply



MultiQuote






|