无法从 Map.Entry<String,Integer> 转换为 String。

huangapple go评论82阅读模式
英文:

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 + "已添加。重量:" + weight + "kg");
        } else {
            System.out.println(newDino + "无法添加。已在数据库中!");
        }
    }

    public void updateDino (String updatedDino, int newWeight){
        if (dinos.containsKey(updatedDino)){
            System.out.println(updatedDino + "已更新。新重量:" + newWeight + "kg");
        } else {
            String line = updatedDino + "无法更新。不在数据库中!";
            System.out.println(line);
        }
    }

    public void removeDino (String removedDino) {
        if (dinos.containsKey(removedDino)){
            System.out.println(removedDino + "已移除");
        } else {
            String line2 = removedDino + "无法移除。不在数据库中!";
            System.out.println(line2);
        }
    }

    public int getWeight (String existingDinosaur) {
        if (dinos.containsKey(existingDinosaur)){
            return dinos.get(existingDinosaur);
        } else {
            String ofweight = existingDinosaur + "在数据库中找不到!";
            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 heavy;
            }
        }
        return ""; // 如果数据库为空
    }
}

问题是,我想从每个恐龙中获取最重的恐龙,我尝试了多次,但实际上做不到。

英文:

I'm posting after 2 hours of trying to solve it but I'd like some help here. The task is the following:

> Create a method getHeaviest which takes no parameters and returns a string. When called, the method should return the name of the heaviest dinosaur in the database. How you achieve this is up to you. If no dinosaurs are in the database, return an empty string instead. Don't worry about dinosaurs having the same weight.

   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&lt;String, Integer&gt; dinos;
public randomdamdam () {
dinos = new HashMap&lt;&gt;();
}
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 + &quot; added. Weight: &quot; + newDino + &quot;kg&quot;);
} else {
System.out.println(newDino + &quot; cannot be added. It is already in the database!&quot;);
}
}
public void updateDino (String updatedDino, int newWeight){
if (dinos.containsKey(updatedDino)){
System.out.println(updatedDino + &quot;updated. Weight: &quot; +newWeight+ &quot;kg&quot;); 
} else {
String line = updatedDino + &quot;cannot be updated. It is not in the database!&quot;;
System.out.println(line);
}
}
public void removeDino (String removedDino) {
if (dinos.containsKey(removedDino)){
System.out.println(removedDino + &quot;removed&quot;); 
} else {
String line2= removedDino +&quot;cannot be removed. It is not in the database!&quot;;
System.out.println(line2);
}
}
public int getWeight (String existingDinosaur) {
if (dinos.containsKey(existingDinosaur)){
return dinos.get(existingDinosaur);
} else {
String ofweight = existingDinosaur + &quot;cannot be found in the database!&quot;;
System.out.println(ofweight);
return 0;
}
}
public Set&lt;String&gt; getDinoNames(){
Set&lt;String&gt; names = dinos.keySet();
return names;
}
public String getHeaviest () {
int max = Collections.max(dinos.values());
for (Entry&lt;String, Integer&gt; heaviestBoi : dinos.entrySet()) {
if (heaviestBoi.getValue() == max ) {
String heavy = heaviestBoi.toString();
return heaviestBoi;
}}
}

So the thing is that I'd like to get the heaviest dino out of every dino and I've tried multiple times to do that but I actually can't.

答案1

得分: 1

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

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

英文:

I think you were already close. Looking at your getHeaviest method, within the if statement, you have essentially to get the key element of the "Entry" object (which is the name of the dino). You cannot simply return the whole heaviestBoi object as it is of type Entry.

Solution

public String getHeaviest () {
int max = Collections.max(dinos.values());
for (Entry&lt;String, Integer&gt; heaviestBoi : dinos.entrySet())
{
if (heaviestBoi.getValue() == max ) {                            
return heaviestBoi.getKey();
}
}
}

Additional Comment

Please note that you wrote the following within your if statement:

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

Hence, the first line has actually no effect in respect of the returned object.

答案2

得分: 1

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

将以下内容替换:

String heavy = heaviestBoi.toString();

使用以下内容:

String heavy = heaviestBoi.getKey();

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

英文:

Key of the map must be the name of the dinosaur and value must be its key. So instead of casting amp to String (which is failing) you must return the key which is name of the dinosaur

Replace

String heavy = heaviestBoi.toString();

with

String heavy = heaviestBoi.getKey();

and return that string instead of map object

答案3

得分: 1

请将getHeaviest()方法重构如下:

public String getHeaviestDino() {
    int maxWeight = -1;
    String heaviestDinoName = null;

    for (Map.Entry<String, Integer> entry : dinos.entrySet()) {
        if (entry.getValue() > maxWeight) {
            maxWeight = entry.getValue();
            heaviestDinoName = entry.getKey();
        }
    }

    return heaviestDinoName;
}

注意,方法名已经按照Java命名规范更改为getHeaviestDino,首字母大写。

英文:

Please refactor the getHeaviest() method like below.Complete source code is given below for your better understandings.

Extra: Please follow the Java naming conventions when you are naming classes .Ex:Starting letter of a class name should be always Capital.

public class randomdamdam {
private Map&lt;String, Integer&gt; dinos;
public randomdamdam () {
dinos = new HashMap&lt;&gt;();
}
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 + &quot; added. Weight: &quot; + newDino + &quot;kg&quot;);
} else {
System.out.println(newDino + &quot; cannot be added. It is already in the database!&quot;);
}
}
public void updateDino (String updatedDino, int newWeight){
if (dinos.containsKey(updatedDino)){
System.out.println(updatedDino + &quot;updated. Weight: &quot; +newWeight+ &quot;kg&quot;); 
} else {
String line = updatedDino + &quot;cannot be updated. It is not in the database!&quot;;
System.out.println(line);
}
}
public void removeDino (String removedDino) {
if (dinos.containsKey(removedDino)){
System.out.println(removedDino + &quot;removed&quot;); 
} else {
String line2= removedDino +&quot;cannot be removed. It is not in the database!&quot;;
System.out.println(line2);
}
}
public int getWeight (String existingDinosaur) {
if (dinos.containsKey(existingDinosaur)){
return dinos.get(existingDinosaur);
} else {
String ofweight = existingDinosaur + &quot;cannot be found in the database!&quot;;
System.out.println(ofweight);
return 0;
}
}
public Set&lt;String&gt; getDinoNames(){
Set&lt;String&gt; names = dinos.keySet();
return names;
}
public String getHeaviest () {//Here is your method which returns the heaviest dino&#39;s name
int max = -1;
String name= null;
for (Map.Entry&lt;String, Integer&gt; heaviestBoi : dinos.entrySet()) {
if (heaviestBoi.getValue() &gt;max ) {
max = heaviestBoi.getValue();
name = heaviestBoi.getKey();
}}
return name;
}
public static void main(String[] args) {
randomdamdam m1 = new randomdamdam();
m1.addDino(&quot;Dino1&quot;, 450);
m1.addDino(&quot;Dino2&quot;,455);
m1.addDino(&quot;Dino3&quot;,700);
System.out.println(&quot;Heaviest Dino: &quot;+ m1.getHeaviest() );//Calling the method 
}
}

Output:

Dino1 added. Weight: Dino1kg
Dino2 added. Weight: Dino2kg
Dino3 added. Weight: Dino3kg
Heaviest Dino: Dino3 //Heaviest one&#39;s name returned

huangapple
  • 本文由 发表于 2020年10月20日 20:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64445548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定