1

Okay here's what I have to do. I have to create a method inside the HashSet program called toString2.

Here is what I did. I created a HashSet with 9 calendardates, January 1 through 9.

HashSet<CalendarDate> test = new HashSet<CalendarDate>();
    for (int i = 1; i < 9; i++){
        test.add(new CalendarDate(1,i));
    }

The output of "toString" is this:

[1/8, 1/2, 1/5, 1/1, 1/6, 1/3, 1/4, 1/7]

It spits them out in the Hash order they get put in, I guess.

Now I basically have to create a program that puts values in the right array index. If I put integers {1,91,71,5,45,7,9} in a Hashset, the program basically creates this:

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
     1               5       7       9
     91              45
     71

Seems straight forward. They are put into indexes by (integer % arraylength) which is 10, so the last digit of the numbers correspond with indexes.

So I have to create a program that produces a similar output with CalendarDate. My teacher said that this is what happens when I input January 1st through 8th.

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
    1/6 1/2 1/8                 1/7    
    1/3     1/5                 1/4    
            1/1

So that must be the Hash value of those, when you do the same thing with integers. So here is code I used.

public void toString(){

 int foo;
 if (!isEmpty()){
     for (int i = 0; i < elementData.length; i++){
         HashEntry<E> current = elementData[i];
         while (current != null){
             foo = current.hashCode();
             foo = foo % elementData.length;

             System.out.print(foo);

             current=current.next;

         }
     }
 }

} So it does the exact same thing. Gets the hashcode and then divides it by 10, or the length of the array.

Here is the output:

19829114

I don't know how these correspond. When we run toString, which runs through the 8 dates the same way as , I already said this, but we get this.

[1/8, 1/2, 1/5, 1/1, 1/6, 1/3, 1/4, 1/7]

I thought it would be return 32331188, as that is how my teacher said it would be. I just want to know what I am doing wrong. Thanks! Edit: It should return 32331188 based on this:

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
    1/6 1/2 1/8                 1/7
    1/3     1/5                 1/4
            1/1
  • 2
    You already asked this question as a different user with the same user name: http://stackoverflow.com/questions/37872702/java-hashset-and-calendardate#comment63202158_37872702 – Michael Markidis Jun 18 '16 at 03:04
  • This is a much more in depth version of the question. I provided a lot more. That question simply gets redirected to a question that isn't really related. I think my question is specific enough to get an answer. – Display Name Jun 18 '16 at 03:05
  • Ok. But what is CalendarDate? and how are we suppose to know the hash code for it? – Michael Markidis Jun 18 '16 at 03:08
  • Good question. The hashcode for it was about a 10 digit number for each one of them. I simply narrowed it down to the final number, just like before, just as the first program used. My teacher PROMISED that it would be 32331188 when I got 19829114. They're obviously different answers, but they are a bit similar in a way. Both using 4 or 5 different numbers. Anyways, I was wondering if maybe my "f" method was done a bit wrong. Like I somehow got the wrong hashcode. So if there's anything that is going to lead me to the wrong answer based on my code, that's what I'm asking. – Display Name Jun 18 '16 at 03:14
  • Shouldn't `current.hashCode() % elementData.length` just be the bucket/index the `HashEntry` is in? And if you look at your output, that doesn't seem to be the case. Do you really mean to be printing the *values* in the hash table? – David Ehrmann Jun 18 '16 at 04:19
  • I'm not sure, really. The 8 digit output I am getting corresponds with the 8 digits that belong under the data structure given. The first is 1/8, so therefore, since it belongs under 3, then my teacher would have gotten 3 doing this. I will build the structure later, that does that, using strings and a map and stuff. But for now, I am wondering how he got his order. I am wondering how he got 1/8, 1/5, and 1/1 under the 4th index. I am wondering how I can get my code to spit out "32331188" like his would. – Display Name Jun 18 '16 at 04:31

0 Answers0