2013-06-03 37 views
0

文件:列表,在Java中的HashMap的價值

Person1:AP 
Person2:AP 
Person3:KE 
Person4:KE 
Person5:UK 
Person6:AP 
Person7:UK 
Person8:AP 

我曾嘗試是:

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.HashMap; 

public class TestNull { 

    public static void main (String args[]) throws IOException { 

     BufferedReader br = new BufferedReader (new FileReader("/home/username/Desktop/test")); 
     String str; 
     HashMap<String, String> h = new HashMap<String, String>(); 

     try { 
      while ((str = br.readLine()) != null) { 
       String[] s = str.split(":"); 
       h.put(s[1],s[0]); 
      } 
      System.out.println(h); 
     } 

     catch (FileNotFoundException E) { 
      System.out.println("File Not Found"); 
     } 
     finally { 
      br.close(); 
     } 
    } 
} 

我可以用Perl中實現這一點:

use strict; 
use warnings; 

open (FILE,"test"); 

my %hash=(); 
while (my $line = <FILE>) { 

    chomp $line; 

    my ($name,$country) = split(":", $line); 

    chomp ($name,$country); 

    $hash{$country} .= "$name ";  
} 

for my $keys (keys %hash) { 

    print "$keys: $hash{$keys}\n"; 

} 

從數據文件,我找了一個徹頭徹尾把這樣的:

{AP = [person1, person 2, person6, person8], KE = [person3, person4], UK = [person5, person7]} 
+1

究竟什麼是你問? –

回答

1

以下是你所需要的 -

Map<String, List<String>> h = new HashMap<String, List<String>>(); 

併爲每行 -

String[] s = str.split(":"); //s[1] should be the key, s[0] is what should go into the list 
List<String> l = h.get(s[1]); //see if you already have a list for current key 
if(l == null) { //if not create one and put it in the map 
    l = new ArrayList<String>(); 
    h.put(s[1], l); 
} 
l.add(s[0]); //add s[0] into the list for current key 
+1

提示:OP尋找'LinkedHashMap'而不是'HashMap'。閱讀所需的輸出。 –

+0

@gthm geeky:如果應該保留命令(讀取行的順序),那麼就像Luiggi建議的那樣,用'new LinkedHashMap >()'替換'new HashMap > 。 –

+0

@LuiggiMendoza:OP的Perl示例並不表示他想要使用LinkedHashmap –

0

HashMap.put()將取代任何元素你已經插入到哈希映射。您需要先嚐試在地圖中檢索現有的鍵,並且如果它已經存在(例如,已插入之前的輸入行),則可以將當前名稱附加到其值,然後將其重新插入到地圖中。

此外,您可能要使用類似List<String>的東西來保存地圖值(名稱),而不僅僅是String。使用列表還可以節省重新插入值的必要性,因爲您可以將其應用於已經在地圖中的列表。

0

你只需要改變while和地圖類型:

Map<String, List<String>> h = new HashMap<String, List<String>>(); 

/* (...) */ 

while ((str = br.readLine()) != null) { 

    String[] s = str.split(":"); 
    list = h.get(s[1]); 
    if(list==null) { 
     list = new ArrayList<String>(); 
    } 
    list.add(s[0]); 
    h.put(s[1],list); 

} 
0

把這一地圖倒過來,你可能希望這樣的:

HashMap<String, String> h = new HashMap<String, String>(); 
... 
// your reading code 
... 
HashMap<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>(); 
... 
for(String key: h.keySet()) { 
    if(!map.hasKey(key)) { 
     map.put(key, new ArrayList<String>()); 
    } 
    map.get(key).add(h.get(key)); 
} 

和現在的「地圖」載所需的結構化數據,與「AP」指向一個ArrayList(PERSON1,人2,person6,person8)等

0

到其他的解決方案另一種方法是使用一個O f來自Guava的Multimap實現。這可能是矯枉過正添加這樣的依賴,如果這是唯一的此類地圖,你有,但是如果風與一堆這種性質的地圖,這是不錯的不要有這個樣板遍:

if (!map.containsKey(key)) { 
    map.put(key, new List<Foo>()); 
} 
map.get(key).add(value); 

當你可以這樣做:

multimap.put(key, value); 

取而代之。有很多實現取決於你想要的東西。

排序: http://guava-libraries.googlecode.com/svn/tags/release03/javadoc/com/google/common/collect/TreeMultimap.html

相同的順序,你加入他們: http://guava-libraries.googlecode.com/svn/tags/release03/javadoc/com/google/common/collect/LinkedHashMultimap.html

不要在乎訂購: http://guava-libraries.googlecode.com/svn/tags/release03/javadoc/com/google/common/collect/HashMultimap.html

https://code.google.com/p/guava-libraries/