2016-03-25 116 views
-1

這個文件是我在運行包含推文的項目時得到的結果,每條推文都有自己的ID和Id,可能會出現多次,可能不是。計算不止一次出現的推文總數

Start: 1458763849881 
712044789663924224 RT @WHUFC_News: West Ham are the only team in the top half of the Premier League without a player in the England squad. 
712044789663928320 RT @BlackPplVines: Don't say anything just RT 
712044789659787266 the 100 is wild [] 
712044789630275584 RT @SincerelyTumblr: me trying to concentrate [] 
712044789630283776 RT @Marie_aguilar98: Bless the man that ends up with me because I have a big attitude along with a big temper [] 
712044789647147009 If you could visit any place in the World, where would it be? — Hawaii and Japan [] 
712044789659787268 ほんとやめて、視聴者を殺しに來るのは [] 
712044789647052800 Who wanna be nice enough to bring me some  [[712044789663928320,"RT Don't say anything just RT ,0.0]] 
712044789634637825 Y6 drama #for #against #debate @MissT_02 @Michelle_Hill[[712044789663928320,"RT Don't say anything just RT ,0.2316541497936268]] 

我需要計算出現不止一次的推文總數? 只有一次出現的推文總數?

鳴叫這裏有ID開始7120447896當鳴叫再次出現將有相同的ID

像它具有ID 712044789663928320,並在最後一行,我寫

我如何開始再次出現主推?正如我開始覺得,使在ID循環,但我不能把它寫以及

FileReader reader =new FileReader("/home/user/results.txt"); 
    Scanner scnr = new Scanner(reader); 
HashMap< String, Integer>h=new HashMap<String, Integer>(); 
    int lineNumber = 1; 
    while(scnr.hasNextLine()){ 
     String line = scnr.nextLine(); 
     h.put(line, i); 
     lineNumber++; 
+0

請訪問[幫助],參加[導覽]並閱讀[問]。 –

+0

謝謝,但我需要知道如何開始正確思考,這是什麼問題? – user5520049

+0

問題是,這不是StackOverflow的工作方式。這不是教程或討論網站。如果你不知道從哪裏開始,還有其他類型的討論網站。 –

回答

2

用掃描儀解析由行的文件行。將ID放置在一個列表中(使用HashMap來放置ID的計數),並檢查這個List是否已經包含ID,如果沒有,將它添加到List中(值爲1),如果已經在Lis中,則計數ID再來一次。

編輯:

HashMap<String, Integer> m = new HashMap<>(); 
//... 
//for first time ID 
m.put("ID as string",1); 
//... 
//Counting on more ID 
m.replace("ID as string",m.get("ID as string")+1); 
+0

感謝您的幫助,對不起,我沒有得到。我如何設置ID的哈希映射。 – user5520049

+0

請參閱編輯答案 – Tokazio

+0

這裏我試過HashMap h = new HashMap (); \t \t掃描儀n =新掃描儀(「results.txt」); \t \t while(n.hasNextLine()){ int i = n.nextInt(); System.out.println(i); } n.close(); – user5520049

1

,您可以利用哈希映射爲相同的鳴叫的該ID將是關鍵和無的現象時有發生鳴叫將是值...閱讀文件,並將其存儲後在哈希映射中,您可以輕鬆找出爲特定的推特ID完成了多少推文。

+0

感謝您的幫助,與我的問題仍然是,但我怎麼能把這個ID的hashmap – user5520049

+0

看到基本上這是你的邏輯你將如何做,但我可以說,把用戶id作爲twitter id,因爲這將是唯一的,並做一樣。 –