sokeefe
sokeefe

Reputation: 637

How to turn a hashmap value into a single string

I'm currently having some trouble pulling a single string from the values in a hash map.

Currently, I have a hash map that is setup as follows:

Map<Character, Character> keyMapping = new HashMap<Character, Character>();
       for(int i = 0; i < rotor1.length(); i++) {
           keyMapping.put(thirdKeyMapping.get(rotor1.charAt(i)), rotor2.charAt(i));
       }

And when you print keyMapping it renders an array as shown below:

println(keyMapping.values().toString());
//[F, S, Y, Q, N, I, D, X, B, E, H, Z, C, T, J, O, W, M, V, A, L, K, U, P, R, G]

How can I change this print line so that the value is a single string with only the letters? For example:

//FSYQNIDXBEHZCTJOWMVALKUPRG

Hopefully this is clear, but please let me know if there is any more information that I can provide to help answer this question.

Upvotes: 2

Views: 3875

Answers (6)

adonese
adonese

Reputation: 171

I personally prefer to use the typing system. So for complex json objects, e.g:

{"result": "some_data", "extras": [{"key": "value"}]}

I would usually do it as this:

class Result implements Serializable {

private String result;
private List<Extras> extras;
// others
}

class Extras implements Serializable {
private String key;
// other fields and logic

}

That way i could do something like Result.extras.get(1).getKey() and so on.

Upvotes: 0

iameven
iameven

Reputation: 348

I use the map entrySet

public class MapToString {
     public static String valueToString(Map<Character,Character> map) {
           StringBuilder stringBuilder = new StringBuilder();
           for (Map.Entry<Character,Character> entry:map.entrySet()) {
               stringBuilder.append(entry.getValue());
           }
           return stringBuilder.toString();
     }
     public static void main(String[] args) {
           Map<Character,Character> res = new HashMap<>();
           res.put('A','A');
           res.put('B','B');
           System.out.println(valueToString(res));
     }
}

Upvotes: 1

Bahramdun Adil
Bahramdun Adil

Reputation: 6079

You simply can do it by first get the strings of value and then replace all the non-alphabet to "" and that is OK.

Map<Character, Character> keyMapping = new HashMap<Character, Character>();
keyMapping.put('a', 'c');
keyMapping.put('b', 'c');
keyMapping.put('c', 'c');
keyMapping.put('d', 'c');
keyMapping.put('e', 'c');

String result = keyMapping.values().toString().replaceAll("[^\p{Alpha}]", "");

Result:

result = ccccc

Upvotes: 1

intboolstring
intboolstring

Reputation: 7100

Here is how I would do it using the actual collection that keyMapping.values() returns.

Map<Character, Character> keyMapping = new HashMap<Character, Character>();
       for(int i = 0; i < rotor1.length(); i++) {
           keyMapping.put(thirdKeyMapping.get(rotor1.charAt(i)), rotor2.charAt(i));
       }
// ^^Your code^^

Iterator<Character> iter = keyMapping.values().iterator();
StringBuilder sb = new StringBuilder();
if (iter.hasNext()) {
    sb.append(iter.next());
    while (iter.hasNext()) {
        sb.append(iter.next());
    }
}

See more on this method here.

Upvotes: 0

Makoto
Makoto

Reputation: 106390

If you're using Java 8, then you can use reduction to accomplish what you're after, with the String identity being the empty string "".

Note that, since you have a collection of Characters, you have to have an intermediate step to convert them over to Strings first.

String reduced = keyMapping.values()
                           .stream()
                           .map(Object::toString)
                           .reduce("", String::concat);

You can then print out reduced and it will be the string you expect.

Upvotes: 3

Kason
Kason

Reputation: 797

Use the string method - Replace the ", " to "" .

Upvotes: -3

Related Questions