目的:了解Map的containsKey的是用以及Map的遍历:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
* 从一数组中找出出现最多的字符 引发对Map的总结 1.containsKey(Object key)方法的使用 2.Map的遍历操作
*/
public class MapTest {
public static void findSameNum(String[] arr) {
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < arr.length; i++) {
/*
* map.containsKey(Object findKey)
* 方法介绍: 如果此映射包含指定键的映射关系,则返回 true。
* 说明:map已经包含了findKey的映射关系 则返回true 否则返回false
*/
if (map.containsKey(arr[i])) {
int tempCount = map.get(arr[i]);
map.put(arr[i], ++tempCount);
} else {
map.put(arr[i], 1);
}
}
/*
* Map的遍历操作 map.entrySet().iterator(); map.getKey() map.getValue()
*/
Iterator<Entry<String, Integer>> it = map.entrySet().iterator();
int count = 0; // 全局记录某数字出现的最多的次数
String maxCountWord = arr[0]; // 默认出现最多的字符是第一个
while (it.hasNext()) {
Entry<String, Integer> en = it.next();
int tempCount = en.getValue();
if (tempCount > count) {
count = tempCount;
maxCountWord = en.getKey();
}
}
System.out.println("最终胜出:" + maxCountWord + "出现" + count + "次!");
}
public static void main(String[] args) {
String[] arr = { "hello", "world", "a", "k", "a", "bf", "aa", "z", "a",
"1", "c" };
MapTest.findSameNum(arr);
}
}
参考:http://www.iteye.com/topic/777508
分享到:
相关推荐
int[] arr = { 1, 2, 2, 3, 3, 4, 4, 4, 1, 5, 6, 6, 6 }; 两种不同方式(使用map和list两种方式)获得一下输出结果 数组和list 可以使用相同的方法,自己测试可以 控制台输出 ...数组中唯一的元素是:5
array_chunk -- 将一个数组分割成多个 array_combine -- 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 array_count_values -- 统计数组中所有的值出现的次数 array_diff_assoc -- 带索引检查...
小程序使用高德地图 map 导航路线规划 (amap-wx.js 从相关下载页面下载的 zip 文件 使用场景:uniapp和小程序使用高德地图(map)可以引用这个文件,getRegeo(获取当前地址),getDrivingRoute(驾车从开始位置到...
ibatis标签提示工具 sql-map-2.dtd
source-map-loader, 从模块中提取sourceMappingURL注释并将它的提供给 web service 加载器从现有源文件( 从他们的sourceMappingURL ) 中提取源映射。安装npm i -D source-map-loader用法文档:使用加载程序工具 web ...
sql-map-config-2.dtd
利用Map特性,计算字符串内重复出现字符的个数(Java版含数组的),之前的测试代码和之后修改的部分代码我没删除,全部上传
选择各种各样的例子,以充分了解GO地图功能,并建立每一个演示场景在您的智能手机上只要几次点击就可以使用真正的GPS位置.若要商用,请前往Unity官方资源商店重新购资源以获得合法使用权:...
《Ranking and Semi-supervised Classification on Large Scale Graphs Using Map-Reduce》原文及译文
JDSU推出的集光功能测试为一体的多功能测试平台,MAP-200。用户可以直接将对应的MAP光开关模块,MAP光衰减器模块,MAP光功率计模
unity AR游戏地图系统GO Map - 3D Map for AR Gaming 3.2 Requires Unity 2017.2.1 or higher. GO Map is a dynamic map SDK for making location based games with unity 3D. Highly customizable with the ...
.arch._amap-wx.js
赠送jar包:mapstruct-1.1.0.Final.jar; 赠送原API文档:mapstruct-1.1.0.Final-javadoc.jar; 赠送源代码:mapstruct-1.1.0.Final-sources.jar; 赠送Maven依赖信息文件:mapstruct-1.1.0.Final.pom; 包含翻译后...
ibatis的dtd约束,版本2,xml文件中标签提示,网址:http://ibatis.apache.org/dtd/sql-map-2.dtd
Hadoop-MindMap-思维导图-读书笔记
前端开源库-map-omap-o,数组对象的类似map的函数。
赠送jar包:mapstruct-1.3.1.Final.jar; 赠送原API文档:mapstruct-1.3.1.Final-javadoc.jar; 赠送源代码:mapstruct-1.3.1.Final-sources.jar; 赠送Maven依赖信息文件:mapstruct-1.3.1.Final.pom; 包含翻译后...
一个字符串包含了空个等标点符号,计算出现次数最多的字母和该字母出现次数 开发提示:可以使用Map,key是字母,value是该字母的次数