Java Hashmap条目不';不要进入纵队
我得到了前两列的正确条目,但在其余的列中,我不知何故无法输入值。希望您能帮助我。您的程序中有两个问题:Java Hashmap条目不';不要进入纵队,java,swing,hashmap,jtable,Java,Swing,Hashmap,Jtable,我得到了前两列的正确条目,但在其余的列中,我不知何故无法输入值。希望您能帮助我。您的程序中有两个问题: 你似乎不知道每个2000字的chunck中有多少个名字。您可以通过添加例如列表或其他数据类型来解决此问题。在我的示例中,列表中的每个元素都会跟踪特定chunck中的名称及其计数 您的tableToModel()确实使用.addColumn()为2000个单词中的每一个添加了新的列。但是,当使用.addRow(Object[])添加行时,对象数组仅包含两个元素。您需要以某种方式将特定于chunc
列表
或其他数据类型来解决此问题。在我的示例中,列表中的每个元素都会跟踪特定chunck中的名称及其计数tableToModel()
确实使用.addColumn()
为2000个单词中的每一个添加了新的列。但是,当使用.addRow(Object[])
添加行时,对象数组仅包含两个元素。您需要以某种方式将特定于chunck的计数包装到该对象数组中。此外,首先使用.addColumn()
创建列,然后立即添加新行可能是合理的name | total count | 1.Part | 2.Part...
---------------------------------------
name | namecounttotal| count1.Part| count2.Part...
JTable
,然后使用:
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);
}
您的程序中有两个问题:
列表
或其他数据类型来解决此问题。在我的示例中,列表中的每个元素都会跟踪特定chunck中的名称及其计数tableToModel()
确实使用.addColumn()
为2000个单词中的每一个添加了新的列。但是,当使用.addRow(Object[])
添加行时,对象数组仅包含两个元素。您需要以某种方式将特定于chunck的计数包装到该对象数组中。此外,首先使用.addColumn()
创建列,然后立即添加新行可能是合理的name | total count | 1.Part | 2.Part...
---------------------------------------
name | namecounttotal| count1.Part| count2.Part...
JTable
,然后使用:
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);
}
多亏了Am9417,我成功了!如果有人感兴趣,结果如下:
JTable t = new JTable(toTableModel(wordsInTheMiddle, wordsPerChunck));
package-einlesen;
/**
*@作者angeliqueschulberger
*
*/
导入java.io.*;
导入java.nio.file.path;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Scanner;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JTable;
导入javax.swing.RowSorter;
导入javax.swing.SortOrder;
导入javax.swing.WindowConstants;
导入javax.swing.table.DefaultTableModel;
导入javax.swing.table.TableModel;
导入javax.swing.table.TableRowSorter;
公共类Einlesen{
/**
*@param args
*@抛出异常
*/
静态列表字=新的ArrayList();
静态映射字sinthemiddle=newhashmap();
@抑制警告({“资源”})
公共静态void main(字符串[]args)引发IOException{
扫描仪扫描=新扫描仪(System.in);
串珠蕨;
System.out.println(“Welches Dokument wollen Sie?Geben Sie dabei den Path an,bite.”);
antwort=(“Downloads/lol.txt”);//scan.nextLine();
字符串路径=System.getProperty(“user.home”);
//System.out.println(路径);
File File=path.get(path,antwort.toFile();
FileReader fr=新的FileReader(文件);
BufferedReader br=新的BufferedReader(fr);
扫描仪sc=新扫描仪(br);
//List word=new ArrayList();
while(sc.hasNext()){
字符串wort=sc.next();
//删除引号
if(麦汁以“\”开头){
麦汁=麦汁子串(1);
}
if(麦汁尾端(“\”){
麦汁=麦汁子串(0,麦汁长度()-1);
}
添加(麦汁);
}
br.close();
int chunkStartIndex=0;
Map wordsInTheMiddle=newhashmap();
List wordsperchunk=new ArrayList();
int chunckNumber=0;
while(word.size()-chunkStartIndex>0){
int chunkEndIndex=chunkStartIndex+2000;
if(chunkEndIndex>word.size()){
chunkEndIndex=word.size();
}
List chunkOfWord=word.subList(chunkStartIndex,chunkEndIndex);
Map countinchuck=new HashMap();
添加(countinchuck);
for(int i=0;i package einlesen;
/**
* @author angeliqueschulberger
*
*/
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.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.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<>();
List<Map<String,Integer>> wordsPerChunck = new ArrayList<>();
int chunckNumber = 0;
while (word.size() - chunkStartIndex > 0) {
int chunkEndIndex = chunkStartIndex + 2000;
if (chunkEndIndex > word.size()) {
chunkEndIndex = word.size();
}
List<String> chunkOfWords = word.subList(chunkStartIndex, chunkEndIndex);
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);
int oldCount = wordsInTheMiddle.get(word1);
wordsInTheMiddle.put(word1, oldCount + 1);
countInChunck.putIfAbsent(word1, 0);
// Increase the count in this chunck
countInChunck.put(word1, countInChunck.get(word1) + 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, wordsPerChunck));
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, List<Map<String, Integer>> 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" });
}
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);
}
return model;
}
}