在javaandroid中从for循环/arraylist返回minvalue和key对

drkbr07n  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(348)

我有一个返回entry.getkey()和entry.getvalue()的函数。当我返回函数时,我将得到如下的响应

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

healthy  0.345
unhealthy  0.0
healthy  0.543

从这个列表中,我想要的是具有最小条目的一对。getvalue()。
示例:上面的列表应该只返回0.0
我试图按最小集合返回,但它与int类型和list类型冲突。。。如何返回具有最小entry.getvalue()的对?

注:请参阅日志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;
    }
fruv7luv

fruv7luv1#

您可以在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;
    }

在这里,我将查找具有最小值的条目,如果条目存在,则将其添加到“recognitions”arraylist中。

相关问题