【问题标题】:Find animals with the highest weight without sorted() (map<String, Animal> (Where Animal - class))在没有 sorted() 的情况下查找权重最高的动物 (map<String, Animal> (Where Animal - class))
【发布时间】:2021-05-03 19:51:03
【问题描述】:

我的任务有问题。我必须在 map 中找到重量最高但没有 sorted() 的动物。问题是我无法获得值 body_wt。 我试图通过 double maxWeight = (Collections.max(map.)); where map Map 但没有任何效果

类动物

    package com.company;

public class Animal {
    public double body_wt;
    public double brain_wt;
    public double non_dreaming;
    public double dreaming;
    public double total_sleep;
    public double life_span;
    public double gestation;
    public int predation;
    public int exposure;
    public int danger;

    //getters and setters
    @Override
    public String toString()
    {

        return "AnimalCharacteristics{bodyWt="+getValue(body_wt)+", brainWt="+getValue(brain_wt)+", nonDreaming=" +
                getValue(non_dreaming)+ ", dreaming="+ getValue(dreaming)+ ", totalSleep="+getValue(total_sleep)+
                ", lifeSpan="+getValue(life_span)+", gestation="+ getValue(gestation)+", predation="+predation+", exposure="+exposure+", danger="+danger+"}";
    }

}

我从 txt 文件中读取动物

public static Map<String, Animal> readAnimals(){
    Map<String, Animal> animals = new HashMap <>();
    Scanner devScanner = null;
    try {
        devScanner = new Scanner(new File("mammals.txt"));

        String species = "q";
        devScanner.nextLine();
        while (devScanner.hasNext()) {
            Animal animal = new Animal();
            String nextLine = devScanner.nextLine();
            String[] anData = nextLine.split(";");
            for (int i = 0; i < anData.length; i++) {
                if (anData[i].isEmpty()) continue;
                switch(i){
                    case 0:  species = anData[0];continue;
                    case 1: if(anData[1].equals("NA")){animal.setBody_wt(0);} else{ animal.setBody_wt(Double.parseDouble(anData[1]));} continue;
                    case 2: if(anData[2].equals("NA")){animal.setBrain_wt(0);} else{ animal.setBrain_wt(Double.parseDouble(anData[2]));} continue;
                    case 3: if(anData[3].equals("NA")){animal.setNon_dreaming(0);} else{ animal.setNon_dreaming(Double.parseDouble(anData[3]));} continue;
                    case 4: if(anData[4].equals("NA")){animal.setDreaming(0);} else{ animal.setDreaming(Double.parseDouble(anData[4]));} continue;
                    case 5: if(anData[5].equals("NA")){animal.setTotal_sleep(0);} else{ animal.setTotal_sleep(Double.parseDouble(anData[5]));} continue;
                    case 6: if(anData[6].equals("NA")){animal.setLife_span(0);} else{ animal.setLife_span(Double.parseDouble(anData[6]));} continue;
                    case 7: if(anData[7].equals("NA")){animal.setGestation(0);} else{ animal.setGestation(Double.parseDouble(anData[7]));} continue;
                    case 8: if(anData[8].equals("NA")){animal.setPredation(0);} else { animal.setPredation(Integer.parseInt(anData[8]));} continue;
                    case 9: if(anData[9].equals("NA")){animal.setExposure(0);} else { animal.setExposure(Integer.parseInt(anData[9]));} continue;
                    case 10: if(anData[10].equals("NA")){animal.setDanger(0);} else { animal.setDanger(Integer.parseInt(anData[10]));} break;
                }
            }
            animals.put(species, animal);
        }



    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return animals;
}

【问题讨论】:

  • animalMap.values().stream().max(Comparator.comparingInt(Animal::getWeight)) 怎么样。
  • @sprinter 谢谢

标签: java dictionary


【解决方案1】:

如果我理解正确,您可以创建一个接收动物[] 的静态方法。如果 arr[i].getWeight() > currentMax -> currentMax = arr[i].getWeight(),则有一个 currentMax 值,初始化为 Double.MIN_VALUE 和数组中的每个索引。然后在最后返回最大 currentMax。如果您想要在 currentMax 更新时存储 i 的索引的位置,请添加一个索引值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多