I have this hashCode function that I have been trying to implement with Eclipse but my Tests keep failing. I am trying to write a Hashcode function that returns that last 2 or 3 chars of a word or number. Here is my current code:

public int hashCode(String value){
 String test = value;
 int hash = 1;
 int len = test.length();
 System.out.println(len);
 for ( int i=0; i<len; i++ )
       {
       hash = 31 * hash + len;
       }
    return hash;
}

Here is my Junit Test:

@Test // hashCode()
 public void testHashCode1() {
  //Integer key1 = 123;
  String key2 = "and";
  assertEquals("and", key2.hashCode());
 }

Everytime I run my test it fails indicating:

expected but was: <96727>

Now I really would like for this function to take in either a Word or Number and return the last two or three chars of the object taken in. Does anyone know how to do this using hashCode. Any help would be most appreciated

Recommended Answers

All 14 Replies

Well, certainly not with * and +

You're looking for substring if you really want to do that, but I don't see any poinf to it, especially since hashcode is suppossed to return an int and not a String, so returning the "last 2 or 3 chars" isn't even applicable.

Ok lets say I wanted to return the last two digits of an int. I have modified my code a little but its still not returning,

@Override
public int hashCode() { 
	int hash = 1;
	Cell<K,V> cell = 
		int len = key.toString().length();
	hash = hash * 31 + (len == 0 ? 0 : len);
	return hash;
}

Can anyone help with returning last two numbers of an int?

int number = 2567;
String num = Integer.toString(number);
String str = num.substring(//put correct argument here);

I'm intentionally omitting the argument to substring so that I'm not just giving you all of the code. Oh, and keep in mind that in order to properly get the substring, you should check the length of the String first and make sure it is long enough, otherwise you may get an error.

Why do only want the last two digits? In any case, to return those as an int simply do modulus 100. Do you know what hascode is meant for? Even if you do only return the last two digits they will still never match the original String. What, exactly, are you trying to acheive? What is your goal with this entire thing?

commented: Haha, whoops. My answer was overkill +4

Why do only want the last two digits? In any case, to return those as an int simply do modulus 100. Do you know what hascode is meant for? Even if you do only return the last two digits they will still never match the original String. What, exactly, are you trying to acheive? What is your goal with this entire thing?

Well I am trying to return the hashCode of the last two or three digits because its part of a project. So I got it to work for a separate class that take in parameters but how to I use the original hashCode() method that takes no parameters, here is my working code:

public class HashCodeTwoDigit<T>{
		public int apply (T arg){
			String str = arg.toString(); //applys to toString to ie 456
			String str2 = str.substring(str.length()- 2, str.length()); //finds the position then goes to the end
			int zInt = Integer.parseInt(str2); //converts back into a Int
			return zInt;
			
		}
	}

Use substring on the string first and call hashcode on the returned substring.

Edit: BTW "as part of a project" tells us nothing I still don't even know what it is you are actually attempting. If you were to tell us the why you only want the hashcode of the last few chars (or the last few digits of the hashcode, you don't seem to be too clear on what it is you actually want), i.e. what the eventual goal is, we could probably help you out a bit more.

trying to put business logic into your hashcode algorithm (which you're attempting to do) is almost always a bad idea.

The hashcode should be such that items can be quickly identified in hashsets and hashtables, nothing more or less (apart from 2 items returning true on calling equals() also returning the same hashcode, but that should be a natural consequence of the above).

Use substring on the string first and call hashcode on the returned substring.

Edit: BTW "as part of a project" tells us nothing I still don't even know what it is you are actually attempting. If you were to tell us the why you only want the hashcode of the last few chars (or the last few digits of the hashcode, you don't seem to be too clear on what it is you actually want), i.e. what the eventual goal is, we could probably help you out a bit more.

To clarfy more, this hashcode() method is for a school project I am working on, basically we are trying to use JUnit testing to test our hashCode along with 11 other Map methods. One requirement is to use hashCode() to return last two, three digits, then the Digit Sum, then the modulo of some positive number. So any help on these would be most appreciated, also How would one write a JUnit test for this. I currently have:

@Test // hashCode()
	public void testHashCode1() {
		String str = "hello";
		String str2 = "jello";
		assertEquals(str.hashCode(), str2.hashCode());
	}

but the output is failing and giving me two different results even through they both end in "lo"

Like I said, use substring on the strings and call hashcode on that substring.

Well, can you post your current hashCode method so we can look at it and see why that might be?

Well, can you post your current hashCode method so we can look at it and see why that might be?

Well my current code is:

public int hashCode(){
		int hash = MyMap.key;
		String str = Integer.toString(MyMap.key);
		String str2 = str.substring(str.length() - 2, str.length());
		int zInt = Integer.parseInt(str2);
		return zInt;
	}

I am now trying to see how to construct a JUnit test but not sure how to format this test to have the test pass.

Well, your hashCode method is written in a way such that no matter how you called it, it will always return the same result (unless you change the value of MyMap.key inbetween invocations). Check out this description of how the hashCode method should be implemented.

The reason why your JUnit Test is probably failing is because calling str.hashCode calls the hashCode method of the String class, it does not call your hashCode method. In order to call your hashCode method, you would have to create an Object of that class type, then you would have to call the method using that Object. However, you could also declare your hashCode method as static and then you can invoke it and pass in your String.

For example,

public static int hashCode(String str){
String str2 = str.substring(str.length() - 2, str.length());
//Within the code you put here, you should use 'str' in such
//a way that [I]every time[/I] you call this method with the
//same exact String, it returns the same exact result.
return zInt;
}

If you don't understand how to implement the hashCode method, then perhaps you should read about hash tables. It can be confused, but read a few links and you'll start to get the idea. Then come ask questions.

Ok still having issues but I think I am getting closer, my JUnit testing is still not working though, here is my Revised code:

public static int hashCode(String str){
		int zInt;
		//str = str.toString();
		if(str.length() == 1) {
			zInt = Integer.parseInt(str);
			return zInt;
		}
		String str2 = str.substring(str.length() - 2, str.length());
		zInt = Integer.parseInt(str2);
		return zInt;
	}

Here is my JUnit testing:

public void testHashCode1() {
		String key1 = "hello";
		String key2 = "jello";
		assertEquals("lo", key1.hashCode());
		assertEquals("lo", key2.hashCode());
	}
	}

Trying to pull in the String and have it return the "lo". Still does not work though.

Your hashCode method returns an int. How could a String (which is what "lo" is) possibly be equal to an int? It can't be. I don't think you are at a level yet where you're prepared to tackle concepts such as hash tables (and by connection, hashCode). If you spend some serious time delving into some beginner java resources, then come back and re-read the replies here, I think it will be much easier for you to understand. I'm not trying to be rude by saying that, but it seems like you are not grasping a lot of concepts that already should be familiar to you once you are ready to tackle this problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.