Counting occurrences of a key in a Map in Java

后端 未结 5 1760
别那么骄傲
别那么骄傲 2020-12-17 02:37

I\'m writing a project that captures Java keywords from a .java file and keeps track of the occurrences with a map. I\'ve used a similar method in the past successfully, but

5条回答
  •  庸人自扰
    2020-12-17 03:29

    Map map = new HashMap();
    Set keywordSet = new HashSet(Arrays.asList(keywords));
    Scanner input = new Scanner(file);
    
    while (input.hasNext()){
         String key = input.next();
         if (key.length() > 0)
            if (keywordSet.contains(key)){
               Integer counter = map.get(key);
    
               if (counter == null)
                   map.put(key, 1);
               else
                   map.put(key, count + 1);
             }
    }
    

提交回复
热议问题