【问题标题】:Return the Minvalue and key pair from the For Loop/ArrayList in Java Android从 Java Android 中的 For Loop/ArrayList 返回 Minvalue 和 key 对
【发布时间】:2021-04-09 00:43:26
【问题描述】:

我有这个函数,它返回 entry.getKey() 和 entry.getValue()。当我返回函数时,我会得到如下响应

 Log.d("myTag", entry.getKey() + " " +entry.getValue());


healthy  0.345
unhealthy  0.0
healthy  0.543

从这个列表中,我想要的是具有最小 entry.getValue() 的对。

示例:上面的列表应该只返回 不健康的 0.0

我尝试通过 Collections minimum 返回,但它与 int 类型和 list 类型发生冲突...如何返回具有最小 entry.getValue() 的对?

**注意:参考 Log.d 并返回识别码**

    public List<Recognition>recognizeImage(final Bitmap bitmap, final  int sensorOriwentation){

        Bitmap resized_bitmap = Bitmap.createScaledBitmap(bitmap, 224, 224, true);

        List<Recognition> recognitions = new ArrayList<>();
        inputImageBuffer = loadImage(resized_bitmap,sensorOriwentation);
        tensorClassifier.run(inputImageBuffer.getBuffer(),probabilityImageBuffer.getBuffer().rewind());

        Map<String,Float> labelledProbability = new TensorLabel(labels,
                probabilityProcessor.process(probabilityImageBuffer)).getMapWithFloatValue();

        for (Map.Entry<String, Float>entry : labelledProbability.entrySet()){

            recognitions.add(new Recognition(entry.getKey(),entry.getValue()));
            Log.d("myTag", "entry.getKey():  "+ entry.getKey() + "   " + "entry.getValue():  "+entry.getValue());

        }


        return recognitions;
    }

【问题讨论】:

  • this 回答你的问题了吗?

标签: java android for-loop arraylist


【解决方案1】:

您可以在您的 java 代码中进行以下更改以返回具有最小值的列表:

public List<Recognition>recognizeImage(final Bitmap bitmap, final  int sensorOriwentation){

        Bitmap resized_bitmap = Bitmap.createScaledBitmap(bitmap, 224, 224, true);

        List<Recognition> recognitions = new ArrayList<>();
        inputImageBuffer = loadImage(resized_bitmap,sensorOriwentation);
        tensorClassifier.run(inputImageBuffer.getBuffer(),probabilityImageBuffer.getBuffer().rewind());

        Map<String,Float> labelledProbability = new TensorLabel(labels,
                probabilityProcessor.process(probabilityImageBuffer)).getMapWithFloatValue();
        Integer i= new Integer(0);
        String minKey = null;
        Float minValue = new Float(0.0);
        for (Map.Entry<String, Float>entry : labelledProbability.entrySet()){
            if(i==0) {
              minValue = entry.getValue();
              minKey = entry.getKey();
            }else if(minValue > entry.getValue()){
              minValue = entry.getValue();
              minKey = entry.getKey();
            }  
            i++;
            Log.d("myTag", "entry.getKey():  "+ entry.getKey() + "   " + "entry.getValue():  "+entry.getValue());

        }
        if(minKey != null){
          recognitions.add(new Recognition(minKey,minValue));
        }
        return recognitions;
    }

在这里,我正在查找具有最小值的条目,如果条目存在,则将其添加到“识别”数组列表中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 2019-04-01
    • 1970-01-01
    • 2013-08-21
    • 2012-05-27
    • 2018-05-02
    • 2014-04-03
    相关资源
    最近更新 更多