java - HashMap 条目不会进入列

标签 java swing hashmap jtable

package einlesen;

/**
 * @author a
 *
 */

import java.io.*;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class Einlesen {

    /**
     * @param args
     * @throws IOException
     */
    static List<String> word = new ArrayList<String>();
    static Map<String, Integer> wordsInTheMiddle = new HashMap<>();
    @SuppressWarnings({ "resource" })
    public static void main(String[] args) throws IOException {

        Scanner scan = new Scanner(System.in);
        String antwort;

        System.out.println("Welches Dokument wollen Sie? Geben Sie dabei den Path an, bitte.");
        antwort = ("Downloads/lol.txt"); // scan.nextLine();

        String path = System.getProperty("user.home");
        // System.out.println(path);
        File file = Paths.get(path, antwort).toFile();

        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        Scanner sc = new Scanner(br);
        //List<String> word = new ArrayList<String>();

        while (sc.hasNext()) {
            String wort = sc.next();
            // Remove quotes
            if (wort.startsWith("\"")) {
                wort = wort.substring(1);
            }
            if (wort.endsWith("\"")) {
                wort = wort.substring(0, wort.length() - 1);
            }
            word.add(wort);
        }

        br.close();

        int chunkStartIndex = 0;
        Map<String, Integer> wordsInTheMiddle = new HashMap<>();
        while (word.size() - chunkStartIndex > 0) {

            int chunkEndIndex = chunkStartIndex + 2000;
            if (chunkEndIndex > word.size()) {
                chunkEndIndex = word.size();
            }
            List<String> chunkOfWords = word.subList(chunkStartIndex, chunkEndIndex);

            for (int i = 0; i < chunkOfWords.size(); i++) {

                String word1 = chunkOfWords.get(i);

                if (word1.matches("[A-Z][a-z][a-z]\\w+")) {

                    wordsInTheMiddle.putIfAbsent(word1, 0);
                    int oldCount = wordsInTheMiddle.get(word1);
                    wordsInTheMiddle.put(word1, oldCount + 1);

                }
            }

            // do not process the last word! Would cause an index out of bounds exception.
            for (int i = 0; i < chunkOfWords.size() - 1; i++) {

                String word1 = chunkOfWords.get(i);

                if (word1.matches("\\w*(\\.|\\?|!)$")) {

                    // Word is at end of sentence
                    String nextWord = chunkOfWords.get(i + 1);

                    if (wordsInTheMiddle.getOrDefault(nextWord, 0) < 2) {

                        // sort out words that appear at the beginning of a sentence and appear less
                        // than 2 times in the text
                        wordsInTheMiddle.remove(nextWord);

                    }

                }

            }

            // remove blacklisted words
            String[] blacklist = { "This", "When", "Night", "Most", "Stone", "There", "Bonfire", "Tuesday", "Their",
                    "They", "Professor", "Famous", "About", "Madam", "Nearly", "Aunt", "What", "Uncle", "Mommy",
                    "Scars", "Scotch", "Every", "That" };
            for (String listedWord : blacklist) {

                wordsInTheMiddle.remove(listedWord);

            }

            System.out.println("Mitte: " + wordsInTheMiddle);

            chunkStartIndex = chunkEndIndex;

        }


        JTable t = new JTable(toTableModel(wordsInTheMiddle));

        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(t.getModel());
        t.setRowSorter(sorter);

        List<RowSorter.SortKey> sortKeys = new ArrayList<>(25);
        sortKeys.add(new RowSorter.SortKey(1, SortOrder.DESCENDING));
        sorter.setSortKeys(sortKeys);

        JPanel p = new JPanel();
        p.add(t);
        JFrame f = new JFrame();
        f.add(p);
        f.setSize(700, 600);
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setTitle(antwort);

    }


    public static TableModel toTableModel(Map<?, ?> map) {
        DefaultTableModel model = new DefaultTableModel(new Object[] { "Key", "Value" }, 0);
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            model.addRow(new Object[] { entry.getKey(), entry.getValue() });

            for (int a = word.size()/2000;model.getColumnCount() - 2 <= a;)
            {
                model.addColumn(new Object[] { wordsInTheMiddle});

            }

        }

        return model;

    }

}

这是我的程序代码,它将文本分为 2000 个单词的不同部分,并计算不同部分中名称的出现次数。这是在 HashMap 中完成的。因此,我需要该 HashMap 的一个表,在其中我可以看到名称、总计数和每个部分的计数。第一列是名称,第二列是总数,其余是不同的部分。

像这样:

name | total count | 1.Part | 2.Part...
---------------------------------------
name | namecounttotal| count1.Part| count2.Part...

我在前两列中输入了正确的条目,但在其余列中我无法输入值。我希望你能帮助我。

最佳答案

您的程序有两个问题:

  1. 您似乎没有记录每 2,000 个单词 block 中有多少个名字。您可以通过添加例如来解决这个问题List<Map<String,Integer>>或其他数据类型。在我的示例中,列表中的每个元素都会跟踪该特定 block 中的名称及其计数。
  2. 您的tableToModel()确实为每个 2,000 个单词 block 添加新列 .addColumn() 。但是,当您添加带有 .addRow(Object[]) 的行时,对象数组仅包含两个元素。您需要以某种方式将特定于 block 的计数包装到该对象数组中。此外,首先使用 .addColumn() 创建列可能是合理的。然后立即添加新行。

为了解决这些问题,我会采取以下措施:

  1. 将数据添加到特定于 block 的计数器并跟踪 block 的数量:

    List<Map<String,Integer>> wordsPerChunck = new ArrayList<>();
    
    while (word.size() - chunkStartIndex > 0) {
    
        // Initialize a chunck-specific word counter
        Map<String, Integer> countInChunck = new HashMap<>();
        wordsPerChunck.add(countInChunck);
        ...
        for (int i = 0; i < chunkOfWords.size(); i++) {
    
            String word1 = chunkOfWords.get(i);
    
            if (word1.matches("[A-Z][a-z][a-z]\\w+")) {
    
                wordsInTheMiddle.putIfAbsent(word1, 0);
                wordsInTheMiddle.put(word1, oldCount + 1);
                countInChunck.putIfAbsent(word1, 0);
                // Increase the count in this chunck
                countInChunck.put(word1, countInChunck.get(word1) + 1);
    
            }
        }
    
  2. 修改toTableModel一点:将该列表作为第二个参数。首先创建列,然后创建大小正确的 Object保存名称、总计数和特定于 block 的计数的数组:

    public static TableModel toTableModel(Map<?, ?> map, List<?> list) {
        DefaultTableModel model = new DefaultTableModel(new Object[] { "Key", "Value" }, 0);
        for (Map.Entry<?, ?> entry : map.entrySet()) {
    
            for (int a = word.size()/2000; model.getColumnCount() - 2 <= a;) {
                model.addColumn(new Object[] { "partial" });
            }
    
            // Create the object that holds all the columns
            Object[] temp = new Object[2+list.size()];
            temp[0] = entry.getKey();
            temp[1] = entry.getValue();
    
            int index = 2;
            for (Object o : list) {
    
                Map<?, ?> m = (Map<?, ?>) o;
                // Get the chunck-specific count with the correct key (the name)
                temp[index] = m.get(temp[0]);
                index++;
            }
            model.addRow(temp);
        }
    

我没有在这里实现您所做的检查。但如果你解决了这些问题,你应该能够让它工作。

创建JTable然后:

JTable t = new JTable(toTableModel(wordsInTheMiddle, wordsPerChunck));

关于java - HashMap 条目不会进入列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51395031/

相关文章:

java - 如果使用 jar,则从 jar 中解压文件,然后将提取的文件复制到目录中

java - 与 ASCII 不同的编码,即使对于字母也是如此

java - 从 NetBeans GUI Builder 在 JPanel 中显示图像

Java:字符串的字符(以字节为单位)的值是常量吗?

java - 如何处理没有任何元素的浏览器通知弹出窗口?

java - 从 HashMap<String, ArrayList<String>> 获取值

java - HashMap 中可以存储的键(对象)数量的理论限制?

c++ - 我如何为 tr1::unordered_map 定义一个不绑定(bind)模板参数的宏/typedef/etc?

java - Jython 中的方法可以返回 Java 对象吗?

java - 监听 JTable 中 JCheckBox 的变化