2016-10-18 2 views
-1

Ich habe eine Hash-Map erstellt, die meine Key-Value-Paare enthält, um die Benutzereingabe durch den Wert zu ersetzen, der dem jeweiligen Schlüssel entspricht. Für exp habe ich mehrere Strings wieErsetzen Zeichenfolge Werte mit Wert in Hash-Map in Java

String pattern = "a+b"; 
String pattern = "C__a_plus_b+d" 
String pattern = "d+C__a_plus_b+F__c_plus_d" 

und ich habe hashmap, die wie es Wert

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

vals.put("a", "123"); 
vals.put("b", "13"); 
vals.put("C__a_plus_b", "123"); 
vals.put("d", "1623"); 
vals.put("C__a_plus_b", "5"); 
vals.put("F__c_plus_d", "15"); 

enthält jetzt will ich mit dort Werte von HashMap in meinem String in String ersetzen, und ich meine wollen Ausgabe wie

String pattern = "a+b"; 
       123+13 

String pattern = "C__a_plus_b+d" 
        123+1623 
+0

'C__a_plus_b' steht sowohl' 123' als auch '5'. Was ist los??? – Bohemian

+0

Was haben Sie bisher geschrieben, Code-weise? – Bloodysock

+0

für (HashMap.Entry val: lablemap.entrySet()) \t \t \t { \t \t \t result = operation.replace (val.getKey(), val.getValue()); \t \t \t Operation = Ergebnis; \t \t \t} – Achin

Antwort

1

Mit Java 8 Streams es so etwas wie sein sollte:

String result = String.join(
    "+", 
    Arrays.asList(pattern.split("\\+")) 
     .stream() 
     .map((String s) -> vals.get(s)) 
     .collect(Collectors.toList()) 
);