zoukankan      html  css  js  c++  java
  • java基础Map遍历

    map遍历方法:

    1.通过keySet取出Map中的数据,然后for-each循环

    2.通过EntrySet取出,foreach循环

    3.KeySet取出,Iterator遍历

    4.通过EntrySet取出,Iterator遍历

     1         /**Map的四种遍历方式*/
     2         //1.直接打印:{name=null, id=null, age=0}
     3         System.out.println(map);
     4         //2.通过keySet取出Map中的数据,然后for-each循环:
     5         //key:name--value:null    key:id--value:null    key:age--value:0
     6         Set<String> keySet=map.keySet();//得到set集合
     7         for (String key:keySet){
     8             System.out.print("key:"+key+"--value:"+map.get(key)+"    ");
     9         }
    10         System.out.println();
    11         //3.通过EntrySet取出,foreach循环
    12         //key:name--value:null  key:id--value:null  key:age--value:0
    13         Set<Map.Entry<String,Object>> entrySet=map.entrySet();//得到set集合
    14         for (Map.Entry<String ,Object> entry:entrySet){
    15             System.out.print("key:"+entry.getKey()+"--value:"+entry.getValue()+"  ");
    16         }
    17         /**迭代器遍历*/
    18         //4.KeySet取出,Iterator遍历
    19         Iterator<String>iterator1=keySet.iterator();//map.keySet(),得到set集合,可以通过迭代器遍历
    20         while (iterator1.hasNext()){
    21             String key=iterator1.next();
    22             System.out.println("key:"+key+"   value"+map.get(key));
    23         }
    24 
    25         //5.通过EntrySet取出,Iterator遍历
    26         Iterator<Map.Entry<String,Object>> iterator2=entrySet.iterator();//map.entrySet(),得到set集合,得到set集合,可以通过迭代器遍历
    27         while (iterator2.hasNext()){
    28             Map.Entry<String,Object> entryIterator=iterator2.next();
    29             System.out.println("key:"+entryIterator.getKey()+"   value:"+entryIterator.getValue());
    30         }

    统计文件中关键字:

      1 package MapDemo;
      2 /**
      3 *统计文件中关键字的个数,利用Map
      4 *@author:Archer-LCY
      5 *@date:2018年3月13日下午1:12:45
      6 */
      7 
      8 import java.io.BufferedReader;
      9 import java.io.FileReader;
     10 import java.util.ArrayList;
     11 import java.util.Arrays;
     12 import java.util.HashMap;
     13 import java.util.HashSet;
     14 import java.util.List;
     15 import java.util.Map;
     16 import java.util.Set;
     17 import java.util.TreeMap;
     18 
     19 public class CountLKeyword {
     20     public static void main(String[] args) {
     21         Count_keyword();
     22     }
     23     
     24     public static void Count_keyword() {
     25         final String fileName="src/MapDemo/CountLKeyword.java";
     26         /*关键字*/
     27         final String keywords="package,inport,public,class,static,void ,final,private,new,try.catch,while,null,if,continue,return";
     28         //1、获得Java源文件中的字符串(行集合,StringBuffer)
     29         List<String> contentList=ReadSourceFile(fileName);
     30         Set<String> keywordsSet=new HashSet<>();
     31         keywordsSet.addAll(Arrays.asList(keywords.split(",")));//以“,”分割
     32         //2、遍历集合关键字,统计每个集合在字符串中出现的次数--将关键字以键-值的方式存放在Map集合中
     33         Map<String , Integer> keywordsMap=new HashMap<>();
     34         for(String line:keywordsSet) {//外循环遍历关键字集合
     35             for(String keyword:contentList) {//内循环遍历,源代码所有行
     36                 //统计每一行,keyword出现的次数(进行累加)
     37                 
     38                 int count=getKeywordcounts(line,keyword);
     39                 if(count==0) continue ;
     40                 if(keywordsMap.containsKey(keyword)) {
     41                     int oldCount =keywordsMap.get(keyword).intValue();
     42                     keywordsMap.put(keyword, new Integer(count+oldCount));
     43                 }else {
     44                     //将count放到map集合中
     45                     keywordsMap.put(keyword, count);//关键字出现一次直接添加即可
     46                 }
     47             }
     48         }
     49         
     50         //3、遍历Map集合(以关键字出现的次数来排序)
     51         for(java.util.Map.Entry<String, Integer> entry: keywordsMap.entrySet()) {
     52             System.out.println(entry.getKey()+"	"+entry.getValue());
     53         }
     54         //排序显示
     55         System.out.println("----------------------------------------------------");
     56         Map<String, Integer> treeMap=new TreeMap<>(new MyTreeComparator(keywordsMap));
     57         treeMap.putAll(keywordsMap);
     58         for(java.util.Map.Entry<String, Integer> entry: keywordsMap.entrySet()) {
     59             System.out.println(entry.getKey()+"	"+entry.getValue());
     60         }
     61     }
     62     
     63     private static int getKeywordcounts(String line ,String keyword) {
     64         if(line==null||line.length()==0)
     65             return 0;
     66         //注释跳过
     67         if(line.startsWith("/*")||line.startsWith("*")||line.startsWith("//")||line.endsWith("*/"))
     68             return 0;
     69         int count=0;
     70         String strLine=new String(line);
     71         int index=-1;
     72         while((index=strLine.indexOf(keyword))!=-1) {
     73             count++;
     74             strLine=strLine.substring(index+keyword.length()+1);
     75         }
     76         return count;
     77     }
     78 
     79     /**
     80      * 读取Java源文件,以字符串方式返回源文件所以内容
     81      * @param fileName
     82      * @return
     83      */
     84     private static List<String> ReadSourceFile(String fileName){
     85         List<String> countentList =new ArrayList<>();
     86         try (
     87                 FileReader fReader=new FileReader(fileName);
     88                 BufferedReader bReader=new BufferedReader(fReader);
     89                 ){
     90             String line=null;
     91             while((line=bReader.readLine())!=null){
     92                 if(line.trim().length()==1)
     93                     continue;
     94                 countentList.add(line);
     95             }
     96             
     97         } catch (Exception e) {
     98             e.printStackTrace();
     99         }
    100         return countentList;
    101     }
    102 }
  • 相关阅读:
    指针
    初级程序员面试不靠谱指南(七)
    初级程序员面试不靠谱指南(六)
    Hadoop 中利用 mapreduce 读写 mysql 数据
    Mapreduce的文件和hbase共同输入
    mapreduce多文件输出的两方法
    mapreduce中一个map多个输入路径
    GDB介绍
    超强的指针学习笔记
    iOS开发之Appstore篇——版本更新
  • 原文地址:https://www.cnblogs.com/archer-lcy/p/9182912.html
Copyright © 2011-2022 走看看