测试类
package cn.edut.hash.test;
import java.lang.reflect.Method;
import org.junit.Test;
import cn.edut.hash.SeparateChainingHashTable;
public class SeparateChainingHashTabletEST {
@Test
public void test() throws Exception {
SeparateChainingHashTable<Integer> hashTable = new SeparateChainingHashTable<>(103) ;
Method method = hashTable.getClass().getDeclaredMethod("isPrime", long.class);
method.setAccessible(true);
Object result = method.invoke(hashTable, 103);
System.out.println(result);
System.out.println(long.class);
System.out.println(Long.class);
Object[] cc = {(long)103};
System.out.println(cc[0].getClass());
System.out.println("------");
result = false ;
result = TestMethod(hashTable, "isPrime" , new Class[]{long.class} , new Object[] {103}) ;
System.out.println(result);
}
private <T> Object TestMethod(T instance ,String methodName, Object... args) throws Exception {
Class<?>[] cs = new Class[args.length];
for(int i=0 ; i<cs.length ; i++) {
cs[i] = args[i].getClass() ;
}
return TestMethod(instance,methodName,cs , args);
}
private <T> Object TestMethod(T instance ,String methodName, Class<?>[] paraClasses , Object[] paraValues ) throws Exception {
Method method = instance.getClass().getDeclaredMethod(methodName,paraClasses);
method.setAccessible(true);
Object result = method.invoke(instance, paraValues);
return result ;
}
}
目标类
package cn.edut.hash;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class SeparateChainingHashTable<T> {
public SeparateChainingHashTable() {
this(DEFAULT_TABLE_SIZE);
}
@SuppressWarnings("unchecked")
public SeparateChainingHashTable(int size) {
keyList = new ArrayList[size];
}
public void insert(T e) {
}
public void remove(T e) {
}
public boolean contains() {
return false;
}
public void makeEmpty() {
}
private static final int DEFAULT_TABLE_SIZE = 101;
private List<T>[] keyList;
private int currentSize;
private void rehash() {
}
private int myhash(T e) {
return e.hashCode() % keyList.length;
}
private long nextPrime(long n) {
while(isPrime(++n)) {}
return n;
}
private boolean isPrime(long n) {
if (n < 2) {
return false;
} else if (n > 2) {
if(n%2==0) {
return false ;
}else {
long len = (long) Math.sqrt(n);
for(long i=3 ; i < len ;i++) {
if(n%i==0) {
return false;
}
}
}
}
return true;
}
}