繁体   English   中英

无法从 Map.Entry 转换<String,Integer>到字符串

[英]Cannot convert from Map.Entry<String,Integer> to String

我在尝试解决它 2 小时后发帖,但我想在这里得到一些帮助。 任务如下:

创建一个不带参数并返回一个字符串的方法 getHeaviest。 调用时,该方法应返回数据库中最重的恐龙的名称。 您如何实现这一点取决于您。 如果数据库中没有恐龙,则返回一个空字符串。 不要担心恐龙有相同的重量。

   import java.util.Map;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;


public class randomdamdam {

    private Map<String, Integer> dinos;

    public randomdamdam () {
        dinos = new HashMap<>();
    }

    public int size(){
        return dinos.size();
    }

    public void addDino(String newDino, int weight){
        if (!dinos.containsKey(newDino)) {
            dinos.put(newDino, weight);
            System.out.println(newDino + " added. Weight: " + newDino + "kg");
        } else {
            System.out.println(newDino + " cannot be added. It is already in the database!");
        }
    }

    public void updateDino (String updatedDino, int newWeight){
        if (dinos.containsKey(updatedDino)){
            System.out.println(updatedDino + "updated. Weight: " +newWeight+ "kg"); 
        } else {
            String line = updatedDino + "cannot be updated. It is not in the database!";
            System.out.println(line);
        }
    }

    public void removeDino (String removedDino) {
        if (dinos.containsKey(removedDino)){
            System.out.println(removedDino + "removed"); 
    } else {
        String line2= removedDino +"cannot be removed. It is not in the database!";
        System.out.println(line2);
    }
}

    public int getWeight (String existingDinosaur) {
        if (dinos.containsKey(existingDinosaur)){
            return dinos.get(existingDinosaur);
    } else {
        String ofweight = existingDinosaur + "cannot be found in the database!";
        System.out.println(ofweight);
        return 0;
    }
    }

    public Set<String> getDinoNames(){
        Set<String> names = dinos.keySet();
        return names;
    }

    public String getHeaviest () {
        int max = Collections.max(dinos.values());

        for (Entry<String, Integer> heaviestBoi : dinos.entrySet()) {
            if (heaviestBoi.getValue() == max ) {
                String heavy = heaviestBoi.toString();

            
            return heaviestBoi;
        }}
    }

所以问题是我想从每个恐龙中取出最重的恐龙,我已经多次尝试这样做,但实际上我做不到。

我想你已经很接近了。 查看您的getHeaviest方法,在 if 语句中,您本质上必须获取“Entry”对象(这是恐龙的名称)的关键元素。 您不能简单地返回整个heaviestBoi对象,因为它属于Entry类型。

解决方案

public String getHeaviest () {
    int max = Collections.max(dinos.values());

    for (Entry<String, Integer> heaviestBoi : dinos.entrySet())
    {
        if (heaviestBoi.getValue() == max ) {                            
             return heaviestBoi.getKey();
        }
    }
}

附加评论

请注意,您在 if 语句中写了以下内容:

String heavy = heaviestBoi.toString();
return heaviestBoi;

因此,第一行实际上对返回的对象没有影响。

地图的键必须是恐龙的名字,值必须是它的键。 因此,不是将 amp 转换为 String (这是失败的),而是必须返回恐龙名称的键

代替

String heavy = heaviestBoi.toString();

String heavy = heaviestBoi.getKey();

并返回该字符串而不是地图对象

请像下面这样重构 getHeaviest() 方法。下面给出了完整的源代码,以便您更好地理解。

额外:命名类时请遵循 Java 命名约定。例如:类名的开头字母应始终为大写。

public class randomdamdam {

    private Map<String, Integer> dinos;
    
    public randomdamdam () {
        dinos = new HashMap<>();
    }

    public int size(){
        return dinos.size();
    }

    public void addDino(String newDino, int weight){
        if (!dinos.containsKey(newDino)) {
            dinos.put(newDino, weight);
            System.out.println(newDino + " added. Weight: " + newDino + "kg");
        } else {
            System.out.println(newDino + " cannot be added. It is already in the database!");
        }
    }

    public void updateDino (String updatedDino, int newWeight){
        if (dinos.containsKey(updatedDino)){
            System.out.println(updatedDino + "updated. Weight: " +newWeight+ "kg"); 
        } else {
            String line = updatedDino + "cannot be updated. It is not in the database!";
            System.out.println(line);
        }
    }

    public void removeDino (String removedDino) {
        if (dinos.containsKey(removedDino)){
            System.out.println(removedDino + "removed"); 
    } else {
        String line2= removedDino +"cannot be removed. It is not in the database!";
        System.out.println(line2);
    }
}

    public int getWeight (String existingDinosaur) {
        if (dinos.containsKey(existingDinosaur)){
            return dinos.get(existingDinosaur);
    } else {
        String ofweight = existingDinosaur + "cannot be found in the database!";
        System.out.println(ofweight);
        return 0;
    }
    }

    public Set<String> getDinoNames(){
        Set<String> names = dinos.keySet();
        return names;
    }

    public String getHeaviest () {//Here is your method which returns the heaviest dino's name
        int max = -1;
        String name= null;

        for (Map.Entry<String, Integer> heaviestBoi : dinos.entrySet()) {
            if (heaviestBoi.getValue() >max ) {
                max = heaviestBoi.getValue();
                name = heaviestBoi.getKey();
                
        }}
       return name;
       
    }
    
    public static void main(String[] args) {
        randomdamdam m1 = new randomdamdam();
        m1.addDino("Dino1", 450);
        m1.addDino("Dino2",455);
        m1.addDino("Dino3",700);
        
        System.out.println("Heaviest Dino: "+ m1.getHeaviest() );//Calling the method 
    }
}

输出:

Dino1 added. Weight: Dino1kg
Dino2 added. Weight: Dino2kg
Dino3 added. Weight: Dino3kg
Heaviest Dino: Dino3 //Heaviest one's name returned

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM