用map集合巧妙解决实际问题。
2016-10-31 08:36
239 查看
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Created by ttc on 16-10-30.
*/
public class Ea
{
public static void main(String[] args)
{
System.out.println("请输入一段英文");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
String[] word = input.split(" ");
Map<String, Integer> map = new HashMap<String, Integer>();
for (String s : word)
{ //如果map中不包含该单词,则取出该单词加入到map集合中,该单词作为key,值为1
if (!map.containsKey(s))
{
map.put(s, 1);
} else //如果map当前含有该单词,则取出单词相对应的值(单词出现的个数),将其加1后,保存到集合
{
Integer count = map.get(s);
count++;
map.put(s, count);
}
}
for (Map.Entry<String, Integer> me : map.entrySet())
{
String strkey = me.getKey();
Integer intvalue = me.getValue();
System.out.println(me.getKey() + "出现了" + me.getValue() + "次");
}
}
}
import java.util.Map;
import java.util.Scanner;
/**
* Created by ttc on 16-10-30.
*/
public class Ea
{
public static void main(String[] args)
{
System.out.println("请输入一段英文");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
String[] word = input.split(" ");
Map<String, Integer> map = new HashMap<String, Integer>();
for (String s : word)
{ //如果map中不包含该单词,则取出该单词加入到map集合中,该单词作为key,值为1
if (!map.containsKey(s))
{
map.put(s, 1);
} else //如果map当前含有该单词,则取出单词相对应的值(单词出现的个数),将其加1后,保存到集合
{
Integer count = map.get(s);
count++;
map.put(s, count);
}
}
for (Map.Entry<String, Integer> me : map.entrySet())
{
String strkey = me.getKey();
Integer intvalue = me.getValue();
System.out.println(me.getKey() + "出现了" + me.getValue() + "次");
}
}
}
相关文章推荐
- 巧妙使用Map集合,解决实际工作中的问题
- MyBatis的一系列问题的处理(遍历Map集合和智能标签和属性和字段不一样的解决办法 和sql片段)(三)
- 解决map集合数据过多get操作慢的问题
- 使用MultiValueMap巧妙解决拼字符串问题
- 模板元编程 --- 用 MPL 解决实际问题
- 认清DVD盘片格式 巧妙解决DVD刻录兼容问题
- 模板元编程 --- 用 MPL 解决实际问题
- 485总线的实际问题和解决办法
- 企业网管常见问题解决集合
- IE图片下空隙问题解决方法集合
- ubuntu8.04安装配置,现有问题集合,及解决方法
- 巧妙解决7610手机出现的机卡不兼容的问题
- 模板元编程 --- 用 MPL 解决实际问题
- MVP模式 -- 解决了实际项目的重用问题
- 在使用SSIS设计脚本时候,出现的在PostExecute之外不能锁定变量集合进行读写访问的问题的解决
- 巧妙备份 解决重装Vista双系统后续问题
- 解决: 集合已修改,枚举操作可能不会执行问题
- prototype 解决实际问题的例子
- 运用Excel三大条件函数解决实际问题!