Reputation: 81
I am trying to crate a code, which calculates Java Map's containsKey -operation in nanoseconds (like how fast it runs that operation). Method has a Map<Double, Double> D as a parameter, and the value returned should be containsKey operations' normal use of time as nanoseconds. I have really no clue, how to continue this. Netbeans IDE gives error message, which says that I have some kind of infinite loop here.
Here is my code so far:
import java.util.*;
public class calculateNanotime implements calculateNanotime{
/*
* Measures Map's containsKey -operaation's time frequency as nanoseconds.
* @param D Map to be tested
* @return containsKey -operation's duration as nanoseconds
*/
//@Override
public long containsKeyTime(Map<Double, Double> D) {
// I should try with different key values
D.containsKey(1.0);
long beginning = System.nanoTime();
long end = System.nanoTime();
long result = end - beginning;
//I have a code in another class of this package, which will be testing how well this code work, after this is ready
result = testable.containsKeyTime(D);
return result;
}
}
Here are the erroe messages: Exception in thread "main" java.lang.StackOverflowError at calculateNanotime.calculateNanotime.containsKeyTime(calculateNanotime.java:35)
at calculateNanotime.calculateNanotime.containsKeyTime(calculateNanotime.java:42)
Upvotes: -1
Views: 119
Reputation: 38511
You don't have an infinite loop, what you have is a recursive function with no base case to terminate. Your containsKeyTime method should probably take as a parameter the value you want to check if the map contains as a key. Maybe something like :
public class calculateNanotime implements calculateNanotime {
@Override
public long containsKeyTime(Map<Double, Double> doubleMap, double value) {
long beginning = System.nanoTime();
boolean result = doubleMap.containsKey(value); //what to do with result?
return System.nanoTime() - beginning;
}
}
Upvotes: 1
Reputation: 4844
Your infinite loop error is likely to come from this
public class calculateNanotime implements calculateNanotime
where you have a class implementing an interface of the same name.
See Java Class and Interface Name Collision
Upvotes: 2