【问题标题】:Cannot convert from Map.Entry<String,Integer> to String无法从 Map.Entry<String,Integer> 转换为 String
【发布时间】:2020-10-20 12:50:07
【问题描述】:

我在尝试解决该问题 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;
        }}
    }

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

【问题讨论】:

    标签: java hashmap set return


    【解决方案1】:

    请像下面这样重构 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
    
    

    【讨论】:

      【解决方案2】:

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

      替换

      String heavy = heaviestBoi.toString();
      

      String heavy = heaviestBoi.getKey();
      

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

      【讨论】:

        【解决方案3】:

        我想你已经很接近了。查看您的 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;
        

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

        【讨论】:

          猜你喜欢
          • 2019-05-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-06
          • 2015-07-31
          • 2022-01-27
          • 1970-01-01
          • 2013-12-27
          相关资源
          最近更新 更多