将静态HashMap中的每个值转换为字符串Java
我在将HashMap中的每个值转换为字符串时遇到了一些问题将静态HashMap中的每个值转换为字符串Java,java,string,hashmap,Java,String,Hashmap,我在将HashMap中的每个值转换为字符串时遇到了一些问题 private static HashMap<String, List<Music>> musiksammlung = new HashMap<String, List<Music>>(); private static HashMap musiksammlung=new HashMap(); 这是HashMap的构造函数。该键表示唱片集,该值表示该唱片集的曲目列表
private static HashMap<String, List<Music>> musiksammlung = new
HashMap<String, List<Music>>();
private static HashMap musiksammlung=new
HashMap();
这是HashMap的构造函数。该键表示唱片集,该值表示该唱片集的曲目列表。现在我想将每个音乐对象转换为字符串,而不创建新的HashMap,是这样吗 可能的 我曾尝试过迭代器方案、条目集上的for循环等等,但似乎没有任何效果 编辑:// 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;
}
public HashMap generateFormatList(){
HashMap formatList=新建HashMap();
for(字符串键:musiksammlung.keySet())
put(key,musiksammlung.get(key.toString());
返回musiksammlung;
}
但是这总是会导致一个错误“不适用于参数(字符串,字符串),所以我不知道。我是否必须重写toString()?您的路径是正确的,但是您需要将现有的
列表
转换为列表
,并将列表
放入新的哈希映射
然后,您还需要返回新创建的HashMap
,而不是原始的HashMap
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;
}
public HashMap generateFormatList(){
HashMap formatList=新建HashMap();
for(字符串键:musiksammlung.keySet()){
//要存储在地图中的值
列表值=新的ArrayList();
//获取列表
List musicList=musiksammlung.get(键);
音乐(m:musicList){
//将每个音乐对象的字符串添加到列表中
增值(m.toString);
}
//将值添加到新地图
formatList.put(键、值);
}
//返回新地图
返回格式列表;
}
因此,请回答您的问题:
现在,我想将每个音乐对象转换为字符串,而不创建
新的HashMap,这可能吗
您需要创建一个新的HashMap
,因为它存储不同类型的值:List
与List
不同
正如我在上一个答案中提到的,请确保重写
Music.toString()
,以便它为您返回有意义的字符串,而不是它从父类继承的字符串,父类至少包含java.lang.Object
尝试如下更改HashMap
:
private static HashMap<String, List<Object>> musiksammlung = new HashMap<String,List<Object>>();
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());
}
private static HashMap musiksammlung=new HashMap();
因此,您可以在此HashMap
中保存任何类型的对象。在使用对象之前,还可以使用instanceof
检查对象的类型。formatList
需要一个列表
,但是musiksammlung.get(key)。toString()
返回一个字符串(而不是列表
)。您的意思是这样吗
HashMap<String, String> formatList = new HashMap<String, String>();
HashMap formatList=newhashmap();
您是否尝试过类似的方法:
private static HashMap<String, List<Object>> musiksammlung = new HashMap<String,List<Object>>();
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());
}
Iterator it=musiksammlung.keySet().Iterator();
while(it.hasNext()){
List ml=musiksammlung.get(it.next());
音乐(m:ml)
System.out.println(m.toString());
}
当然,你应该覆盖音乐#toString()方法。你应该把你尝试过的和没有尝试过的代码发布出来work@teekay很高兴我们都能提供帮助。由于您是新用户,我想让您知道,您应该标记一个正确的答案,以便它可以帮助有类似问题的用户,并帮助保持StackOverflow的质量。