【问题标题】:Matrix Table with Hashmap printing带有 Hashmap 打印的矩阵表
【发布时间】:2016-06-21 04:59:59
【问题描述】:

死 Stackoverflow,

我通过 Hadoop 编写代码作为分配,以编写从文本文件中读取的值的矩阵表。基本上它必须读取一个字符在某个其他字符之后出现了多少次。

虽然我在 hadoop 中的代码,但我已经有了这些值,并将它们放入哈希图中 > 并将它们传递给一个名为频率表的函数:

 public void FrequencieTable(HashMap<Character, HashMap<Character,Integer>> charFrequentie){
    String theRowList = "";
    String theColumnList = "";
    for(Character row : charFrequentie.keySet()){
        theRowList += "\n" + row;
        for(Character column : charFrequentie.get(row).keySet()) {
            theColumnList += column.charValue() + "\t";
            theRowList += "\t" +  charFrequentie.get(row).get(column);
        }

        System.out.println("\t" + theColomList +  "\n");
        System.out.println(theRowList);
    }
}

只有这样会给出错误的输出,因为它应该只在一行和一列上显示像“H”这样的每个字符一次,如果那里没有任何数据,它应该显示 0。

基本上它给出了这个输出:

    u   s   a   o   e   m   g   d   t   n   j   t   a   n   g   t   e   m   a   t   e   e   a   o   e   u   s   g   l   j   k   


g   1   1   2   2
d   1   1
e   1   2   2   1   1
a   2   2   3
n   2   2
o   2   1

虽然它应该是这样的:(没有重复)

    u   s   a   o   e   m   g   d   t   n   j   
g   0   0   0   0   0   0   0   1   1   2   2
d   1   1   0   0   1   0   3   0   0   0   0

有人知道我们应该做什么吗?我们完全一无所知。

已经谢谢你了

【问题讨论】:

  • Dead Stackoverflow - RIP ;)

标签: java matrix hashmap duplicates


【解决方案1】:

我不知道你的数据到底应该代表什么,但基本问题似乎是你的内部循环:

for(Character column: charFrequentie.get(row).keySet()){
    theColumnList += column.charValue() + "\t";
    theRowList += "\t" +  charFrequentie.get(row).get(column);
}

如果 same 字符存在于多行的映射中,您可以将其添加到列列表中,例如如果您有 a->c 和 b->c 的频率,那么您的列列表中会出现两次 c。

除此之外,您还需要以相同的顺序遍历映射中的 所有 个字符。您目前只使用每行的值,并且由于您不知道它们在哪一列,您不能用 0 填充其他列。

要解决这个问题,您必须循环两次(否则您可能会错过顶行中的尾随零):

  • 一次获取所有列
  • 一次实际打印行

列的顺序要么必须提供/定义,要么取决于映射中的顺序。

例子:

//Step 1: collect all the columns that have values
Set<Character> columns = new LinkedHashSet<>();
for(Character row : charFrequentie.keySet()){
  //gets the mapped characters for the row and adds them in the order they are returned ignoring any duplicates 
  columns.addAll(charFrequentie.get(row).keySet());
}

//Step 2: print
for( Character col : columns ) {
  //print the columns as the first line
}

//here you iterate over the rows since you'll print line by line
for(Character row : charFrequentie.keySet()){
  //go over the columns in the same order for each row
  for( Character col : columns ) {
    //get the frequency for the column in that row, which might be null       
    Integer frequency = charFrequentie.get(row).get(col);

    //if there is no value for the column in the current row just print 0
    if( frequency == null ) {
      //print 0
    } else {
      //there is a frequency value so just print it         
    }
  }
}

关于列及其排序的两个注释:

  • 由于您只提供哈希映射,因此您无法确定列的顺序(LinkedHashSet 的顺序将由映射返回,但仍然无法定义,因为哈希映射通常不定义顺序。如果您需要特定的顺序,则必须对列进行排序(然后使用排序集)或手动提供它们。
  • 如果您没有频率 0 的条目,您将不会得到任何全为零的列,如您的示例中 om 只有 0 值。在这种情况下,您必须手动提供它们以获取没有频率数据的列。

编辑:

为了使示例更清晰,假设以下输入数据(格式:行、列、频率)

a,a,1
a,b,5
a,c,3
b,a,5
b,e,7

这将导致列集具有值 abce 的任意顺序(因为您使用哈希图)。

输出可能如下所示(由于使用哈希图,顺序可能会有所不同,我只是使用随机顺序):

  b e a c
b 0 7 5 0  
a 5 0 1 3

【讨论】:

  • 嗯,这更有意义,但实际上可能会打印双 A 或任何字符的问题。基本上该行应该只显示一次字符所以如果“a,n,4”(基本上意味着如果一个发生4次并且“a,p,5”发生5次它应该只显示一次而不是两次。所以这应该结果是:AN 4 P 5 而不是 AAN 4 P 5
  • @Tvt 您意识到每行仅打印一次列值,并且使用columns 的集合有效地删除了任何重复项?当然,如果你有像“a,n,4”和“n,a,4”这样的数据,你会得到 4 个条目:2 行,值为“a, 0, 4”和“n, 4, 0”。 - 也就是说,我不确定你从哪里得到 A A N 4 P 5
  • 是的,但有没有办法过滤这个?让它看起来像这样:prnt.sc/accgqi 不知道这是否可能。
  • @Tvt 当然这是可能的。 “过滤这个”是什么意思?如果您打算过滤列,只需手动提供它们(即选择您想要查看的列,而不是从映射中收集它们)。如果还应该过滤行,只需检查键是否在允许的键集中(根据您的示例可能是 columns),如果不是,则跳过该行。
  • 我的意思是“过滤这个”是我想检查像 A 这样的 collom 是否已经存在,如果是的话,将它应用到该 col/row 以便它是有序的。我不知道它应该如何再次检查它的位置,所以它发布在正确的行+kolom
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 2014-08-28
  • 1970-01-01
相关资源
最近更新 更多