Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Lambda expression to replace chars in string using HashMap

Tags:

java

lambda

So i'm working on a project in which I have to convert a String containing greek letters in to a string containing the english representation for this letter. So a greek α would become alpha and Α would become Alpha.

I have created a HashMap which has the appropriate conversions from Unicode Character to a normal String.

I have used a simple For loop to check if a Char in the input String is a Key in the HashMap and if it is then i'll append its replacement using a StringBuilder. Here is the code I use to do this:

char[] ca = snippet.toCharArray();
        StringBuilder out = new StringBuilder();
        for (char c : ca) {
            if (GREEK_LETTER_DICT.containsKey(Character.toString(c))) {
                out.append( GREEK_LETTER_DICT.get(Character.toString(c)));
            } else {
                out.append(c);
            }
        }
    return out.toString();

This is in a public static method with String as input. The thing I want to know is if it is possible to do the same with a lambda expression? I have already found a couple of solutions replacing a Character in String but they do not use a HashMap/Dictionary.

I understand that it is completely useless to just convert this into a lambda expression for the sake of using a lambda expression, but because I have another 7 of these functions shortening my code by almost 60% I would like to see if it is at all possible to do this in "one line" of code. This way i can decrease the number of separate methods I use and get cleaner code.

So far I have found a way to convert a String to a Stream using:

String.chars()

Then converting this IntStream to a Stream<Character> with

.mapToObj(ch -> (char) ch)

and filtering to see if the Character is in the HashMap with

.filter(ch -> (GREEK_LETTER_DICT.containsKey(ch)))

The problem lies in the fact that I am not able to

  1. append the original Character to the output String if it is not in the HashMap
  2. replace the Character with the String in the HashMap

So any help on these two points is appreciated. I found out that sometimes I think the wrong way around because instead of seeing if a Character in a String is equal to a Character in a list of options, I should have checked if that list of options returns a positive index. So this (PseudeoCode):

"StringWithOptions".indexOf('characterLiteral')

instead of this:

Character.equals("One|of|these")

like image 481
Nathan van Dalen Avatar asked Jan 05 '23 18:01

Nathan van Dalen


2 Answers

How about that:

public static String replaceGreekLetters(String snippet) {
    return snippet
            .chars()
            .mapToObj(c -> (char) c)
            .map(c -> GREEK_LETTER_DICT.getOrDefault(c, c.toString()))
            .collect(Collectors.joining());
}
like image 147
xehpuk Avatar answered Jan 08 '23 07:01

xehpuk


I would do something like this:

str
    .chars()
    .forEach(
        character -> out.append(
            GREEK_LETTER_DICT.getOrDefault(Character.toString(c), c)
        )
    );
like image 27
SMA Avatar answered Jan 08 '23 09:01

SMA