Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tcelik
Last active April 15, 2019 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcelik/3b97b297b14126ecca8d5117043bd5d3 to your computer and use it in GitHub Desktop.
Save tcelik/3b97b297b14126ecca8d5117043bd5d3 to your computer and use it in GitHub Desktop.
Mülakat sorusu olarak soruldu. Geleneksel Yöntem, Collection Yöntem, Lambda Yöntemi.
class StringUtil {
// "baba" a:2, b:2 tane var gibi mesela.
public static HashMap<Character, Integer> countOccurencesWithHashMapWay(String str)
{
char [] strArray = str.toCharArray();
HashMap<Character, Integer> hm = new HashMap<>();
for (char ch : strArray)
if (hm.containsKey(ch))
hm.put(ch, hm.get(ch) + 1);
else
hm.put(ch, 1);
return hm;
}
}
@tcelik
Copy link
Author

tcelik commented Apr 15, 2019

public static HashMap<Character, Integer> countOccurencesWithHashMapWayLambda(String str)
{

    HashMap<Character, Integer> hm = new HashMap<>();

    str.chars().mapToObj(val -> (char) val).collect(Collectors.toList())
            .stream().forEach(character -> {

                if (hm.containsKey(character))
                    hm.put(character, hm.get(character) + 1);
                else
                    hm.put(character, 1);

    });

    return hm;

}

@tcelik
Copy link
Author

tcelik commented Apr 15, 2019

 public static int [] countOccurencesWithFrequencyArrayTradionatalWay(String str)
{
        int [] result = new int['z' + 1];

        char [] chars = str.toCharArray();

        for (char ch : chars)
            result[ch - 'A']++;

        return result;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment