在HashMap中添加整数/打印字符串(相同的HashMap)

时间:2014-12-19 06:19:04

标签: java string hashmap

我对HashMap没有太多经验,但我被指示使用它。我需要在Integers内添加HashMap,并打印出同一Strings中存在的HashMapHashMap被声明为

HashMap<String, Integer> newmap = new HashMap<String, Integer>();

我想将我将放入的所有整数(从文件读取)中加起来,例如,如果我有100和50,我希望输出说150 total points。此外,地图还存储了人物的名称,例如Carl和Sophia,所以我希望输出说出类似Characters: Carl, Sophia的内容。

我的问题就像我说的那样,我不知道我对HashMap的经验如何。无论如何,非常感谢您的回复,请询问您是否需要澄清!的谢谢!

此外,这个问题是有道理的,这个程序的目标是“拯救”角色/对象,有点像视频游戏。最后,它应该显示所有被拯救的角色或物品以及获得的总分数。

编辑Hero.java

import java.util.*;
import java.io.*;
import java.awt.*;

public abstract class Hero extends Character
{
   private String heroname1;
   public Hero() {
          heroname1 = "Rick Grimes"; //the default player name
   }
   HashMap<String, Integer> newMap = new HashMap<String, Integer>();

   public Hero(String newhero) {
          if (newhero.length() > 0) {
               heroname1 = newhero;
          } else { heroname1 = "Rick Grimes"; } //defaulted as protagonist
   }

   public String getHeroName() {
          return heroname1; //return the name
   }

   public String rescue() { //class to rescue people or things
          String out = "".toString();
          for(String _key : newMap.keySet()) {
              out += heroname1 + " rescued " + _key + " " + newMap.get(_key) +!
          }
          return out;
   }

   public String toString() { //print
      return heroname1;

   }
   public String view() {
          Set<String> names = newMap.keySet();
          StringBuilder sb = new StringBuilder("Characters rescued: ");
          int i = 0;
          for (String name : names) {
              if (i == 0) {
                  sb.append(name);
                  i++;
              } else {
                  sb.append(", ");
                  sb.append(name);
              }
          }
          String printNames = sb.toString();
          Collection<Integer> points = newMap.values();
          StringBuilder sp = new StringBuilder("Total points: ");
          int sum = 0;
          for (Integer value : points) {
               sum += value;
               sp.append(value);
          }
          String printPoints = sp.toString();
          return printNames + " " + printPoints ;
   }
}

当我的司机打电话时 的输出

Rick Grimes
Rick Grimes rescued Carl 100pts!
Rick Grimes rescued Carl 100pts!Rick Grimes rescued Michone 50pts!
Characters rescued: Carl, Michone Total points: 10050

DISERED OUTPUT

Rick Grimes
Rick Grimes rescued Carl 100pts!
Rick Grimes rescued Michone 50pts!
Characters rescued: Carl, Michone Total points: 150

3 个答案:

答案 0 :(得分:2)

为了澄清,这不是HashMap而是特定于任何java Map。要查找所有值的总和,以下代码段将非常方便。请注意Map.class具有values()方法,该方法返回包含地图所有值的Collection

Collection<Integer> values = myMap.values();
int sum = 0;
for (Integer value : values) {
    sum += value;
}

要以所需格式连接密钥,请找到以下代码。 Map.class返回使用keySet()方法设置的地图的所有键。

Set<String> keys = myMap.keySet();
StringBuilder sb = new StringBuilder("Characters: ");
int i = 0;
for (String key : keys) {
    if (i == 0) {
        sb.append(key);
        i++;
    } else {
        sb.append(", ");
        sb.append(key);
    }
 }
 String consolidatedKey = sb.toString();

除此之外,你可以使用entrySet()使用相同的循环。此方法返回一组Map.Entry<String, Integer>,然后您可以getKey()getValue()

答案 1 :(得分:0)

看起来你想要HashMap中存在的键和值。您应该查看entrySet()方法。您将能够遍历Map并获得键和值(得分和字符)。

答案 2 :(得分:0)

请尝试以下代码:

public static String view(Map newMap) {

     Set<Map.Entry<String,Integer>> entry=newMap.entrySet();
     StringBuilder sb1 = new StringBuilder("Characters rescued: ");
     int total=0;
     for(Entry<String,Integer> s:entry)
     {
         sb1.append(s.getKey() + ",");
         total=total+s.getValue();
     }

     sb1.deleteCharAt(sb1.toString().length()-1);

     return sb1.toString() + " " + total ;
}