2016-03-07 45 views
0

Dead Stackoverflow,帶有Hashmap打印的矩陣表

我通過Hadoop編寫一個代碼作爲分配,以便從文本文件中讀取值的讀取矩陣表。基本上它必須讀取一個角色在某個其他角色之後出現的次數。

我有那些值的已經雖然我在Hadoop的代碼,並把它們放到一個HashMap>,並將它們傳遞到一個函數調用頻率表:

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 

沒有任何人有任何想法,我們應該做些什麼?我們完全無能爲力。

謝謝您已經

+2

'死Stackoverflow' - RIP) – Thomas

回答

0

我不知道到底你的數據應該代表什麼,但基本問題似乎是你的內部循環:

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

在這裏,您將在相同字符添加到列列表是否存在於多行的映射中,例如如果你有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   
    } 
    } 
} 

有兩點需要注意:

  • 既然你只提供包含HashMap您不能確定列的順序(與LinkedHashSet順序將是如地圖返回,但仍然沒有定義,因爲散列圖通常不定義一個訂單,如果你想要一個特定的訂單,你必須對列進行排序(然後使用一個有序集)或手動提供它們。
  • 如果您沒有頻率爲0的條目,那麼您將無法獲得全部爲零的列,如om只有0值。在這種情況下,您必須手動提供它們以獲取沒有頻率數據的列。

編輯:

爲了使示例更清楚假設以下的輸入數據(格式:行,列,頻率)

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

這將導致在具有值的列組a,b,c,e以任何順序(因爲你使用hashmaps)。

輸出看起來是這樣的(同樣的順序可以不同,由於使用包含HashMap,我只是用一個隨機的順序):

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

毫米,這使得更多的SENCE但是可能會打印雙A的問題或實際上的任何字符。基本上行只應該顯示一次字符 因此,如果「a,n,4」(低沉地表示發生4次並且「a,p,5」發生5次,則應該只發生一次而不發生兩次 所以這應該是結果: 甲 N 4 第5頁 而且不 AA N 4 第5頁 – Tvt

+0

@Tvt你認識到列值的每一行,並且僅輸出一次使用的一組'columns'有效地刪除任何重複?當然,如果你有像「a,n,4」和「n,a,4」的數據,你會得到4個條目:2行的值爲「a,0,4」和「 n,4,0「 - 說,我不確定你在哪裏得到那個'AA N 4 P 5'從。 – Thomas

+0

是的,我做,但有沒有辦法來過濾?要使它看起來像這樣:http://prnt.sc/accgqi 不知道這是否可能。 – Tvt