-1

我正在尝试编写一个代码,该代码以纳秒为单位计算 Java Map 的 containsKey 操作(例如它运行该操作的速度)。方法有一个Map<Double, Double> D作为参数,返回的值应该是 containsKey 操作的正常使用时间,单位为纳秒。我真的不知道如何继续这个。Netbeans IDE 给出错误消息,表示我在这里有某种无限循环。

到目前为止,这是我的代码:

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;
}
}

以下是错误消息:calculateNanotime.calculateNanotime.containsKeyTime(calculateNanotime.java:35) 线程“主”java.lang.StackOverflowError 中的异常

在calculateNanotime.calculateNanotime.containsKeyTime(calculateNanotime.java:42)

4

2 回答 2

2

您的无限循环错误很可能来自此

public class calculateNanotime implements calculateNanotime

你有一个实现同名接口的类。

请参阅Java 类和接口名称冲突

于 2020-11-04T15:33:44.313 回答
1

您没有无限循环,您拥有的是一个没有基本情况可终止的递归函数。您的 containsKeyTime 方法可能应该将您想要检查地图是否包含作为键的值作为参数。也许是这样的:

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;
    }
}
于 2020-11-04T15:36:31.870 回答