简体   繁体   English

将静态HashMap中的每个值转换为String Java

[英]Converting Each Value in static HashMap to String Java

I have got some troubles converting each value in my HashMap to a String. 我在将HashMap中的每个值转换为String时遇到了一些麻烦。

    private static HashMap<String, List<Music>> musiksammlung = new 
    HashMap<String, List<Music>>(); 

This is my constructor for the HashMap. 这是我的HashMap构造函数。 The key represents the album, the value a list of tracks from this album. 键代表专辑,值代表该专辑的曲目列表。
Now I want to convert each Music object to a String without creating a new HashMap, is this possible? 现在,我想将每个Music对象转换为String而不创建新的HashMap,这可能吗? I've tried it with the Iterator scheme, for loop over the entry set and so on but nothing seems to work. 我已经尝试过使用Iterator方案,对条目集进行循环等等,但是似乎没有任何效果。

Edit:// 编辑://

My code for the convertmethod: 我的convertmethod代码:

    public HashMap<String, List<String>> generateFormatList() {
    HashMap<String, List<String>> formatList = new HashMap<String, List<String>>(); 

    for(String key : musiksammlung.keySet())
            formatList.put(key, musiksammlung.get(key).toString());  
    return musiksammlung; 

}

But this always results in an error "is not applicable for the Arguments (String, String) so I have no idea. Do I have to override toString()? 但这总是导致错误“不适用于参数(字符串,字符串),所以我不知道。我必须重写toString()吗?

You're on the right path but you need to convert the existing List<Music> to a List<String> and put the List<String> into your new HashMap . 您所走的路是正确的,但是您需要将现有的List<Music>转换为List<String>并将List<String>放入新的HashMap

You also then want to return your newly created HashMap<String, List<String>> instead of your original one. 然后,您还想返回新创建的HashMap<String, List<String>>而不是原始的HashMap<String, List<String>>

public HashMap<String, List<String>> generateFormatList() {
   HashMap<String, List<String>> formatList = new HashMap<String, List<String>>(); 

   for(String key : musiksammlung.keySet()) {
      // Value to store in map
      List<String> value = new ArrayList<String>();

      // Get the List<Music>
      List<Music> musicList = musiksammlung.get(key);
      for (Music m: musicList) {
          // Add String of each Music object to the List
          value.add(m.toString);
      }

      // Add the value to your new map
      formatList.put(key, value);  
   }
   // Return the new map
   return formatList;
}

So answer your question: 因此,请回答您的问题:

Now I want to convert each Music object to a String without creating a new HashMap, is this possible? 现在,我想将每个Music对象转换为String而不创建新的HashMap,这可能吗?

You need to create a new HashMap , because it's storing different type of value: List<Music> is different from List<String> . 您需要创建一个新的HashMap ,因为它存储了不同类型的值: List<Music>List<String>

Also as mentioned in my previous answer, make sure you override Music.toString() so that it returns a meaningful String for you instead of the one it inherits from its parent classes, which includes at least java.lang.Object 另外,正如我之前的回答中所述,请确保重写Music.toString()以便它为您返回一个有意义的String ,而不是它从其父类(至少包含java.lang.Object继承的String返回

Try to change your HashMap like this: 尝试像这样更改您的HashMap

private static HashMap<String, List<Object>> musiksammlung = new HashMap<String,List<Object>>();

So you can save any kind of objects in this HashMap . 因此,您可以在此HashMap保存任何类型的对象。 Also use instanceof to check the type of the object before using it. 在使用对象之前,还请使用instanceof检查对象的类型。

formatList wants a List<String> , but musiksammlung.get(key).toString() returns a String (not a List<String> ). formatList一个List<String> ,但是musiksammlung.get(key).toString()返回一个String (不是List<String> )。 Did you mean this? 你是这个意思吗

HashMap<String, String> formatList = new HashMap<String, String>();

Have you tried something like this: 您是否尝试过以下方法:

Iterator<String> it = musiksammlung.keySet().iterator();
while (it.hasNext()) {
    List<Music> ml = musiksammlung.get(it.next());
    for (Music m : ml)
        System.out.println(m.toString());
}

And of course you should override the Music#toString() method with something you could use. 当然,您应该使用可以使用的方法覆盖Music#toString()方法。

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

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