03javase之map案例

1

分析以下需求,并用代码实现:
(1)统计每个单词出现的次数
(2)有如下字符串"If you want to change your fate I think you must come to the ujiuye to learn java"(用空格间隔)
(3)打印格式:
to=3
think=1
you=2

@Test
    public void test1(){
//        (1)统计每个单词出现的次数
//                (2)有如下字符串"If you want to change your fate I think you must come to the ujiuye to learn java"(用空格间隔)
//                (3)打印格式:
//        to=3
//        think=1
//        you=2
        String str="If you want to change your fate I think you must come to the ujiuye to learn java";
        String[] split = str.split("[ ]");
        HashMap<String, Integer> map = new HashMap<>();
        for(String s:split){
            if(map.containsKey(s)){
                Integer count = map.get(s);
                map.put(s,count+1);
            }else {
                map.put(s,1);
            }
        }
        System.out.println(map);
    }

2

案例2:已知 List list = new ArrayList();
list .add(“张三丰,北京”);
list .add(“李四丰,上海”);
list .add(“王二小,北京”);
list .add(“小明,河北”);
list .add(“小毛,北京”);
list .add(“王五,北京”);
要求:求出每个地区有多少人,都是谁?
例如: 北京 4人 张三丰 王二小 小毛 王五

package demo2;

import java.util.*;

/**
 * @creat 2020-09-02-18:23
 */
public class demo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list .add("张三丰,北京");
        list .add("李四丰,上海");
        list .add("王二小,北京");
        list .add("小明,河北");
        list .add("小毛,北京");
        list .add("王五,北京");
        HashMap<String, String> map = new HashMap<>();
        for(String s:list){
            String[] split = s.split(",");
            map.put(split[0],split[1]);
        }
        System.out.println(map);
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        HashMap<String, Integer> map1 = new HashMap<>();
        for (Map.Entry<String,String> s:entrySet) {
            String key = s.getKey();//姓名
            String value1 = s.getValue();//地区
            boolean b = map1.containsKey(value1);
            if(b){
                Integer count = map1.get(value1);
                map1.put(value1,count+1);
            }else{
                map1.put(value1,1);
            }
        }
        System.out.println(map1);
        Set<Map.Entry<String, Integer>> entrySet1 = map1.entrySet();
        for (Map.Entry<String,Integer> s:entrySet1){
            String key = s.getKey();
            Integer value = s.getValue();
            System.out.println(key+"---------"+value);
            ArrayList<String> keyByValue = getKeyByValue(map, key);
            System.out.println(keyByValue);

        }

    }
    public static ArrayList<String>  getKeyByValue(Map<String,String> map, String value){
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        ArrayList<String> list = new ArrayList<>();
        for (Map.Entry<String,String> s:entrySet){
            String key = s.getKey();
            String value1 = s.getValue();

            if(value1.equals(value)){
                list.add(key);
            }

        }
        return list;
    }

}

3

做一个方法 方法有两个参数 参数1 Map<String,String> 参数2:String value
方法完成的功能是 根据value获取key
public static 返回值类型 getKeyByValue(Map<String,String>,String value){
}
返回值类型自身思考用什么类型???
getKeyByValue(map,“111”)
例如:传入的map中的数据为:
map.put(“aaa”,“111”);
map.put(“bbb”,“111”);
map.put(“ccc”,“111”);
map.put(“ddd”,“222”);
方法就应该获得 “aaa” “bbb” “ccc”

@Test
public void test3(){
    Map<String, String> map = new HashMap<>();
    map.put("aaa","111");
    map.put("bbb","111");
    map.put("ccc","111");
    map.put("ddd","222");
    ArrayList<String> keyByValue = getKeyByValue(map, "111");
    System.out.println(keyByValue);
}
//根据value获取key
public ArrayList<String>  getKeyByValue(Map<String,String> map, String value){
    Set<Map.Entry<String, String>> entrySet = map.entrySet();
    ArrayList<String> list = new ArrayList<>();
    for (Map.Entry<String,String> s:entrySet){
        String key = s.getKey();
        String value1 = s.getValue();

        if(value1.equals(value)){
            list.add(key);
        }

    }
    return list;
}

4

键盘录入一个字符串,统计每个字符出现的次数
例如,录入aaaabbccddd!@#@#KaTeX parse error: Expected 'EOF', got '#' at position 2: @#̲%cc66ff
打印出来:a有4个,b有2个,c有4个,d有3个,!有1个,@有3个,$有2个,%有1个,6有2个,f有2个

public class demo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入字符串:");
        String str = scanner.next();
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i=0;i<str.length();i++){
            char c = str.charAt(i);
            if(!map.containsKey(c)){
                map.put(c,1);
            }else{
                Integer count = map.get(c);
                map.put(c,count+1);
            }
        }
        Set<Map.Entry<Character, Integer>> entries = map.entrySet();
        for (Map.Entry<Character, Integer> m:entries){
            Character key = m.getKey();
            Integer value = m.getValue();
            System.out.println(key+"----"+value);
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值