巧妙使用Map集合,解决实际工作中的问题

来源:互联网 发布:js限制ip访问代码 编辑:程序博客网 时间:2024/04/28 23:25
import java.util.HashMap;import java.util.Map;import java.util.Scanner;/** * Created by ttc on 16-10-28. */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() + "");        }    }}
0 0
原创粉丝点击