Get the App
SLTechnology News&Howtos  ›  Development  › 

How does java get different characters and their numbers in a string

Shulou Source: shulou.com Published: 2022-06-01 05:14:46 11月12日 Update

Editor to share with you how java gets different characters and their numbers in a string. I hope you will get something after reading this article. Let's discuss it together.

How to get different characters and their numbers in a string

The problem can be broken down into two steps: the first step is to find out the different characters, and the second step is to count the number of them. It's a bit of nonsense, isn't it? Let me give you an answer first.

Public class DistinctCharsCount {public static void main (String [] args) {printDistinctCharsWithCount ("itwanger"); printDistinctCharsWithCount ("chenmowanger");} private static void printDistinctCharsWithCount (String input) {Map charsWithCountMap = new LinkedHashMap (); for (char c: input.toCharArray ()) {Integer oldValue = charsWithCountMap.get (c) Int newValue = (oldValue = = null)? 1: Integer.sum (oldValue, 1); charsWithCountMap.put (c, newValue);} System.out.println (charsWithCountMap);}}

The result of the program output is:

1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1

Let's talk about my train of thought:

1) declare a LinkedHashMap, you can also use HashMap, but the former can maintain the order of the split strings, and the result looks clearer at a glance.

Why use Map? Because the key of Map does not allow repetition, it is just enough to accumulate the number of repeated characters.

2) split the string into characters and traverse it.

3) if key is null, it means that the number of characters should be + 1; otherwise, just + 1 on the previous value, and then re-put to the Map, thus overwriting the previous number of characters.

The train of thought is very clear, is it correct? I can't help giving myself a round of applause.

Well, after JDK 8, Map added a powerful method, merge (), to assign values to multiple keys at once:

Private static void printDistinctCharsWithCountMerge (String input) {Map charsWithCountMap = new LinkedHashMap (); for (char c: input.toCharArray ()) {charsWithCountMap.merge (c, 1, Integer::sum);} System.out.println (charsWithCountMap);}

Is it very good? One line of code is done. The first parameter is the key, the second parameter is the value, and the third parameter is a BiFunction, which means that if the key already exists, the new value is recalculated based on the BiFunction.

If the character appears for the first time, the value is assigned to 1; otherwise, the previous value sum 1 is set.

After reading this article, I believe you have a certain understanding of "how java gets different characters and their numbers in strings". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Characters quantity strings different parameters awesome ideas articles results no clear at a glance one-time one-line three two code multiple finished nonsense meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia vpn MariaDB Apple