java - Java 中计算单词对的数据结构类型是什么?

标签 java counting cpu-word

我正在尝试计算文本文件中的单词对。我的目标是将字符串中的每个单词映射到其后面的单词,然后计算重复的键/值对。我不关心订单。我的代码当前使用 HashMap 来存储每个单词对,但使用 HashMap 我丢失了重复的条目。如果我的文本文件包含:“FIRST SECOND THIRD FIRST SECOND”,我将得到输出:FIRST [SECOND] SECOND[] THIRD [FIRST]。因此,如果我有重复的键,则以下字符串值将覆盖以前的值。布兰登·林(Brandon Ling)早些时候在之前的一篇文章中帮助了我,但我并不清楚他的目标。我现在终于意识到 HashMap 可能不起作用。
任何帮助将不胜感激。

 import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
 import java.io.FileNotFoundException;
 import java.util.Iterator;
 import java.util.Scanner;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.ArrayList;
 import java.util.Set;
 import java.util.TreeMap;



 public class Assignment1
 {
     // returns an InputStream that gets data from the named file
     private static InputStream getFileInputStream(String fileName)
     {
     InputStream inputStream;

     try {
         inputStream = new FileInputStream(new File(fileName));
     }
     catch (FileNotFoundException e) {       // no file with this name exists
         System.err.println(e.getMessage());
         inputStream = null;
     }
     return inputStream;
     }

    // @SuppressWarnings("unchecked")
     public static void main(String[] args)
     {


     InputStream in = System.in;

         in = getFileInputStream(args[0]);
         System.out.println("number of words is" + in);


     if (in != null) 
     {

         // Using a Scanner object to read one word at a time from the input   stream.

         @SuppressWarnings("resource")
         Scanner sc = new Scanner(in);   
         String word;

         System.out.println("CS261 - Assignment 1 -AdamDavis%n%n");
         System.out.println("");
         System.out.println("");

         // Continue getting words until we reach the end of input 
         List<String> inputWords = new ArrayList<String>();
         HashMap<String, List<String>> wordPairs = new HashMap<String,     List<String>>();

         while (sc.hasNext()) 
         {  
         word = sc.next();       
         if (!word.equals(null)) 
         {

             inputWords.add(word);

             System.out.println("");
             System.out.println("");
        }
       }

         Iterator<String> it = inputWords.iterator();
         boolean firstWord = true;
         String currentWord = null;
         String previousWord = null;


         while(it.hasNext())
             {
                 currentWord = it.next();
                wordPairs.put(currentWord, new ArrayList<String>());
                 if(firstWord == true)
                 {
                    //System.out.println("this is result inside if first ==   null:" + wordPairs.containsKey(currentWord));
                     firstWord = false;
                  }
                 else
                 {
                 // System.out.println("this is result inside else:" + currentWord);
                   wordPairs.get(previousWord).add(currentWord);
                  //System.out.println("this is result inside else:" +  wordPairs.containsKey(previousWord));

                 }

                     previousWord = currentWord;

                  }


             {
                 Entry<String, List<String>> Pairs = iter.next();
                 System.out.println("this is the key in pairs: " +Pairs.getKey());

                  Pairs.getValue();
                  System.out.println("this is the key in pairs: " +Pairs.getValue());

                  int count = 0;
                  if(iter.hasNext())
                  {

                      count ++;

             }

        Set<Entry<String, List<String>>> s = wordPairs.entrySet();
        Iterator<Entry<String, List<String>>> itr=s.iterator();

     while(itr.hasNext())
    {
        Entry<String, List<String>> Pairs = itr.next();
        System.out.println(Pairs.getKey()+"\t"+Pairs.getValue());
    }
}


}
}

最佳答案

您可以使用apache commons org.apache.commons.collections.map.MultiKeyMap,它允许您存储多个键,然后只需添加整数作为值来维护计数器。

    MultiKeyMap map = new MultiKeyMap();
    Integer counter = new Integer(1);
    map.put("String1","String2",counter);
    Integer value = (Integer)map.get("String1", "String2");

或者您可以为 map 创建组合键。单词1+单词2。然后使用整数继续

    Map<String,Integer> map = new HashMap<>();

    String key = "word1" + "|" + "word2";

    Integer value = new Integer(1);

    map.put(key,value);
    Integer cntr = map.get(key);

关于java - Java 中计算单词对的数据结构类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641336/

相关文章:

java - 使用函数接口(interface)抽象公共(public)代码的问题

java - Tomcat7(本地主机 :8080): 404 error

javascript - 计算 ul 中 li 的数量并分配类别

php - 从 mysql 数据库中发出一个多词的排序

ide - 在 WebStorm 中指定单词分隔符

java - 如何在 Tomcat 6 上手动部署 Web 服务?

java - 使用自定义面板的 JOptionPane.showOptionDialog 无法正常工作

python - 如何计算Python 3中字典中每组值的数量?

Python语法/理解

python - 如何创建由a和b组成的具有一定长度的单词?