zoukankan      html  css  js  c++  java
  • Map的遍历方法及字符计数

    一、Map遍历的4种方法

    public static void main(String[] args) {


      Map<String, String> map = new HashMap<String, String>();
      map.put("1", "value1");
      map.put("2", "value2");
      map.put("3", "value3");
      
      //第一种:普遍使用,二次取值
      System.out.println("通过Map.keySet遍历key和value:");
      for (String key : map.keySet()) {
       System.out.println("key= "+ key + " and value= " + map.get(key));
      }
      
      //第二种
      System.out.println("通过Map.entrySet使用iterator遍历key和value:");
      Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
      while (it.hasNext()) {
       Map.Entry<String, String> entry = it.next();
       System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
      }
      
      //第三种:推荐,尤其是容量大时
      System.out.println("通过Map.entrySet遍历key和value");
      for (Map.Entry<String, String> entry : map.entrySet()) {
       System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
      }

      //第四种
      System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
      for (String v : map.values()) {
       System.out.println("value= " + v);
      }
     }

    二、统计字符串中各个字符的个数

    public static void main(String[] args) {
            StringBuilder str=new StringBuilder("aaaaaabbbajjsjjjjj");
            Map<Object,Integer> map=new HashMap<Object,Integer>();
            for(int i=0;i<str.length();i++){
                char t=str.charAt(i);
                int count=0;
                if(map.get(t)==null){
                    
                    for(int j=i+1;j<str.length();j++){
                        if(t==str.charAt(j)){
                            count++;
                        }
                    }
                    map.put(t, ++count);
                }
                
            
            }
            for (Object key : map.keySet()) {
                   System.out.println("key= "+ key + " and value= " + map.get(key));
            }
        }

     三、输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 

    public static   void main(String args[]){  
            int num=0,chartra=0,blak=0,other=0;  
            Scanner  s=new Scanner(System.in);  
            System.out.println("请输出字符创");  
            String   result=s.nextLine();  
            char x[]=result.toCharArray();  
            for(int i=0;i<x.length;i++){  
               if(Character.isDigit(x[i])){  
                   num++;  
               }else if(Character.isLetter(x[i])){  
                   chartra++;  
               }else if(Character.isSpace(x[i])){  
                   blak++;  
               }  
               else{  
                   other++;  
               }  
        System.out.println(x.length);  
            }  
            System.out.println("数字的个数是"+num);  
            System.out.println("字符的个数是"+chartra);  
            System.out.println("空值的个数是"+blak);  
            System.out.println("其他的个数是"+other);  
        }  
    }  
     
     
    转自:http://www.cnblogs.com/kristain/articles/2033566.html
  • 相关阅读:
    Cryptography中的对称密钥加解密:fernet算法探究
    HTTPS的工作原理
    最近要写的博客
    浅谈路由器软硬件架构
    组管理、权限管理、定时任务调度、磁盘分区
    matplotlib数据可视化
    tensorflow实现简单的卷积神经网络
    tensorflow实现简单的感知机
    tensorflow实现简单的自编码器
    区域生长算法(手动选取种子点)MATLAB
  • 原文地址:https://www.cnblogs.com/wuxinyiwu/p/7576115.html
Copyright © 2011-2022 走看看