简体   繁体   English

从哈希图中检索值并将其添加到字符串中

[英]Retrieving values from hashmap and adding them to a string

I am basically trying to make a string consisting of all the values in my hashmap . 我基本上是在尝试使hashmap的所有值组成一个字符串。

For example String finalOutcome = "forename=Bob&surname=Marl" and the hashmap would be: 例如, String finalOutcome = "forename=Bob&surname=Marl" ,哈希图将为:

Hashmap<String, String> maps = new Hashmap<String, String>();
maps.put("forename", forename);
maps.put("surname", surname);

My app/program keeps crashing when I am trying to add the values in my hashmap to get the finalOutcome . 当我尝试在哈希图中添加值以获取finalOutcome时,我的应用程序/程序始终崩溃。 This is my code, and also how do I do it so that & sign does not come after the last value in the hashmap?? 这是我的代码,该怎么做,以使&符号不会出现在哈希图中的最后一个值之后? I pass this hashmap into another method in another class as follows: 我将此哈希图传递给另一个类中的另一个方法,如下所示:

public addMapValues(Hashmap<String, String> maps){
    for(int i=0; i<maps.size(); i++){
        finalOutcome += maps.get(i) + "&";
    }
}

It's just that bit that is causing me a huge headache and I am sure someone here can fix this almost instantly (yes, I am not good with Java) 只是那一点让我非常头疼,我敢肯定,这里有人可以立即解决此问题(是的,我对Java不太满意)

Would greatly appreciate some help 非常感谢您的帮助

  1. Use a StringBuilder to concatenate strings in a loop. 使用StringBuilder在循环中连接字符串。
  2. Consider Guava's map joiner . 考虑番石榴的地图连接器
  3. Your code doesn't work because you're trying to lookup values by index instead of by key. 您的代码无效,因为您试图按索引而不是按键查找值。 Maps aren't indexed. 地图未编制索引。 If you want to iterate over the values, you should use Map.entrySet() . 如果要迭代这些值,则应使用Map.entrySet()

Example with Guava: 番石榴的例子:

HashMap<String, String> map = new HashMap<String, String>();
String result = Joiner.on('&').withKeyValueSeparator("=").join(map);

Example with loop: 循环示例:

HashMap<String, String> map = new HashMap<String, String>();
StringBuilder sb = new StringBuilder();
for (Entry<String, String> entry : map.entrySet()) {
    sb.append('&').append(entry.getKey()).append('=').append(entry.getValue());
}
String result = sb.length() > 0 ? sb.substring(1) : "";

Example with Java 8 streams (based on another answer that was deleted from some reason): Java 8流示例(基于由于某些原因而删除的另一个答案):

HashMap<String, String> map = new HashMap<String, String>();
String result = map.entrySet().stream()
        .map(e -> e.getKey() + "=" + e.getValue())
        .collect(Collectors.joining("&"));

Consider using a StringBuilder object 考虑使用StringBuilder对象

StringBuilder finalOutcome  = new StringBuilder  ();

for (String name: maps.values()) {
    finalOutcome.append (name).append("&");
}

return finalOutcome.toString ();

Consider using https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#values() to get the data out of the Map 考虑使用https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#values()从地图中获取数据

Also, please be aware that you are using the same key if you are looping ie forename , so if you were to add a new person, they would replace the existing person in your map. 另外,请注意,如果要循环(即forename ,则使用相同的key ,因此,如果要添加新人员,则他们将替换地图中的现有人员。

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

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