3 Replies - 344 Views - Last Post: 29 May 2012 - 11:40 AM Rate Topic: -----

#1 cre8tion  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 12

hashCode() function

Posted 29 May 2012 - 11:25 AM

Hi,

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 =)

Is This A Good Question/Topic? 0
  • +

Replies To: hashCode() function

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10477
  • View blog
  • Posts: 38,835
  • Joined: 27-December 08

Re: hashCode() function

Posted 29 May 2012 - 11:35 AM

If you are covering hashing in class, then you should be familiar with it to some degree. What hash function does your instructor want you to use? There are a bunch of hash functions, so this is more of a question for your instructor.

More reading on hash functions as well: http://en.wikipedia....i/Hash_function
Was This Post Helpful? 0
  • +
  • -

#3 cre8tion  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 12

Re: hashCode() function

Posted 29 May 2012 - 11:38 AM

We haven't went over anything about hashing yet, I will speak with my instructor tomorrow. Thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: hashCode() function

Posted 29 May 2012 - 11:40 AM

You could just iterate over each element in the Set and sum the .hashCode()'s of each element.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1