Home > Mobile >  Having trouble with floorentry in Java
Having trouble with floorentry in Java

Time:11-02

I built the following TreeMap :

      TreeMap<Double, BigDecimal> myMap = new TreeMap<>();
      myMap.put(0.00, new BigDecimal(18000));
      myMap.put(62.00, new BigDecimal(25000));
      myMap.put(100.00, new BigDecimal(30000));
      myMap.put(150.00, new BigDecimal(55000));
      myMap.put(200.00, new BigDecimal(55000));
      myMap.put(200.00, new BigDecimal(55000));
      myMap.put(301.00, new BigDecimal(70000));

When I run :

variable = 100;
rebate = myMap.get(variable);

I get correct results (30000). But when I run :

variable = 75;
rebate = myMap.get(variable);

My entire program breaks. When I try running :

rebate = myMap.floorEntry(variable);

My IntelliJ throws me this error:

Required type:
BigDecimal


Provided:
Entry
<java.lang.Double,
java.math.BigDecimal>

stacktrace:
    <pre>java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
    [error]: Build step org.kie.kogito.quarkus.common.deployment.KogitoAssetsProcessor#generateModel threw an exception: java.lang.IllegalStateException: src/main/java/com/zappyride/software/business/P94/LambdaConsequence9423558E6DAA4D0C76452B4B4DFC5AA3.java (115:5948) : Type mismatch: cannot convert from Map.Entry&lt;Double,BigDecimal&gt; to BigDecimal
src/main/java/com/zappyride/software/business/P94/LambdaConsequence9423558E6DAA4D0C76452B4B4DFC5AA3.java (115:5948) : Type mismatch: cannot convert from Map.Entry&lt;Double,BigDecimal&gt; to BigDecimal
    at org.kie.kogito.quarkus.common.deployment.InMemoryCompiler.compile(InMemoryCompiler.java:100)
    at org.kie.kogito.quarkus.common.deployment.KogitoQuarkusResourceUtils.compileGeneratedSources(KogitoQuarkusResourceUtils.java:140)
    at org.kie.kogito.quarkus.common.deployment.KogitoAssetsProcessor.compileAndIndexJavaSources(KogitoAssetsProcessor.java:126)
    at org.kie.kogito.quarkus.common.deployment.KogitoAssetsProcessor.generateModel(KogitoAssetsProcessor.java:95)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:920)
    at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
    at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2415)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
    at java.base/java.lang.Thread.run(Thread.java:829)
    at org.jboss.threads.JBossThread.run(JBossThread.java:501)

How do I fix this? Essentially I need to be able to pass Java a key, and pull the corresponding value of the next lowest key if it is not a perfect match.

Thank you in advanced!

CodePudding user response:

As indicated in the Javadoc, TreeMap.floorEntry returns a Map.Entry<K, V>. You should change your setup to be:

Map.Entry<Double, BigDecimal> entry = myMap.floorEntry(variable);
  • Related