Home>Article>Java> Java actual combat gives a string and counts the number of Chinese characters or letters that appear

Java actual combat gives a string and counts the number of Chinese characters or letters that appear

无忌哥哥
无忌哥哥 Original
2018-07-20 10:56:18 2355browse

废话不多说,直接上代码

package test; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Stack; import java.util.Vector; import com.sun.org.apache.bcel.internal.generic.IRETURN; public class Test { public static void main(String[] args) { String str = "中国你好吗>国你好吗>中国国家国国国国aabb;;;"; Map map = new HashMap(); char[] ch = str.toCharArray();//将字符串转为数组 /*将字符和出现的个数存入到Map*/ for(char c:ch){ if(map.get(c)==null){ map.put(c, 1);//为空的话值为1 }else{ Integer in = map.get(c);//得到的value值至少为1 in++; map.put(c, in); } } /* 使用for-each遍历 for (Character c : map.keySet()) { System.out.println(map.get(c)); } */ //使用迭代器和entrySet遍历 Iterator> iterator=map.entrySet().iterator(); Integer count =Collections.max(map.values());//出现次数最多的值 Character character = null; //存放次数最多的key while(iterator.hasNext()) { Map.Entry entry=(Map.Entry)iterator.next(); System.out.println(entry.getKey()+" "+entry.getValue()); //得到出现次数最多的值的key if(entry.getValue().equals(count)) { character=entry.getKey(); } } System.out.println("出现次数最多的是:"+character+"出现次数为"+count); } }

此题当然可以使用keyset()遍历,但是没有EntrySet效率高

The above is the detailed content of Java actual combat gives a string and counts the number of Chinese characters or letters that appear. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn