Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report