2017-02-20 50 views
1

我有散列表,它的關鍵字是String,值存儲爲List。比較存儲爲列表結構的Hashmap值

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

因爲我在同一個鍵下有多個值。 因此,例如我有4個鍵和值是:

[1681,3523]

[276,489]

[1527,2865]

[300]

另外我還有另一個treeList包含這些值作爲排序。我想要做的是(和問);

Iterator<Integer> itr=myTreeSet.iterator(); 
    while(itr.hasNext()) 
    { 
     int check = itr.next(); 
     for(Object value : map2.entrySet()) 
     { 
      //System.out.println("Value is :" + value); 

     } 
    } 

我要檢查ITR所示的代碼與我的Hashmap和的entrySet如果等號返回的HashMap key.Above代碼的entrySet返回作爲數組。簡單地說,如何檢查它屬於hashmap的entrySet並屬於哪個鍵。

我正在MAP1和MAP2作爲參數

static void game(Map map1, Map map2, Hero h, HashSet< Integer> hash) 

我對不起你們。我不清楚,我認爲你很困惑。我第一次在主FUNC定義該地圖:

Map<String, Enemy> enemymap = new HashMap<>(); 
Map<String,List<Integer>> posmap = new HashMap<>(); 

而且我填寫他們主要FUNC爲好。填補他們後,我把它們發送到我的函數遊戲中。 posmap是map2。所以我指出posmap。抱歉混淆。 功能的完整代碼。

static void game(Map map1, Map map2, Hero h, HashSet< Integer> hash) 
{ 
    TreeSet<Integer> myTreeSet = new TreeSet<>(); // For sorting min to max-for alignment 
    myTreeSet.addAll(myHashset); 
    System.out.println(myTreeSet); 
    String start = "Micheal started travel with" + " " + h.getHealth() + " " + "HP!"; 
    Iterator<Integer> itr=myTreeSet.iterator(); 
    while(itr.hasNext()) 
    { 
     int check = itr.next(); 
     /* for(Map.Entry<String, List<Integer>> entry : map2.entrySet()) 
      { 
       if(entry.getValue() != null && entry.getValue().contains(check)) 
       System.out.println("The key " + entry.getKey() + "contains the treeset value " + check); 

      } -/ 
    } 

} 

從主FUNC發送如:

game(enemymap, posmap , hero, myHashset); 
+0

我不確定我是否理解這個問題,因爲它太馬虎。 'posmap'應該和'map2'一樣嗎?但是,如果你想測試一個整數是否在地圖中的某個值列表中,你需要查看所有的值並使用類似'List.contains'的東西。並且不要說'對象值',比如'Map.Entry >值'。否則,您將無法檢索這些值。 – ajb

+0

我認爲你的澄清使事情不太清楚。此外,在參數列表中使用原始類型(如'Map',而不是'Map >')將使編寫代碼難以做正確的事情,因爲編譯器不知道您的密鑰是字符串,你的值是列表。 – ajb

回答

1

我會推薦以下方法:

Iterator<Integer> itr=myTreeSet.iterator(); 
while(itr.hasNext()) 
{ 
    int check = itr.next(); 
    for(Map.Entry<String, List<Integer>> entry : map2.entrySet()) 
    { 
     if(entry.getValue() != null && entry.getValue().contains(check)) 
     System.out.println("The key " + entry.getKey() + "contains the treeset value " + check); 

    } 
} 
+0

謝謝@ajb。欣賞你的投入。只是修復它。 – VHS

+0

它說map2.entrySet:類型不匹配:不能從元素類型對象轉換爲Map.Entry – Lucas

+0

順便說一下,我正在將map2作爲參數。 – Lucas

1

,因爲你不記得你存儲在HashMap中的INT時使用的hashkey ,你需要檢查,如果int是在任何散列值。

for(Map.Entry<String,List<Integer>>entry: map2.values()) { 
    if(entry.getValue().contains(check)) return entry.getKey(); 
} 

這樣的事情。

2

其他解決方案打印或返回一個密鑰。根據你原來的短文,我以爲你想要一套,所以這就是我所做的。

編輯過@ AJB的建議是:

Set<Integer> myTreeSet = new TreeSet<>(); 
Map<String,List<Integer>> posmap = new HashMap<>(); 
Iterator<Integer> itr = myTreeSet.iterator(); 

//The set of keys containing values in any list of posmap 
Set<String> matchedKeys = new HashSet<>(); 

//Iterate through the TreeSet Integers 
itr.forEachRemaining(itrVal -> { 
    //Stream each entry of the posmap 
    posmap.entrySet().stream() 
     //Remove the entries without the itrVal in the entry's list 
     .filter(entry -> entry.getValue().contains(itrVal)) 
     //Add each key with a match to the set 
     .forEach(matchedEntry -> matchedKeys.add(matchedEntry.getKey())); 
}); 

return matchedKeys; 

,如果你有Apache的百科全書在您的處置,這可能是一個不錯的辦法:

Set<Integer> myTreeSet = new TreeSet<>(); 
Map<String,List<Integer>> posmap = new HashMap<>(); 
Iterator<Integer> itr = myTreeSet.iterator(); 

Set<String> matchedKeys = new HashSet<>(); 

List<Integer> treeSetValues = IteratorUtils.toList(itr); 

treeSetValues.stream().map(val -> { 
    return posmap.entrySet().stream() 
     .filter(entry -> entry.getValue().contains(itrVal)) 
     .collect(Collectors.toSet()); 
}); 

return matchedKeys; 

如果您不能使用IteratorUtils.toList() ,你可以使用Guava和Lists.newArrayList()。

+0

當我發佈這個解決方案時,我認爲正在搜索posmap的值來自於treelist。但是,你的問題現在突然出現了map2? –

+0

我認爲使用'Map.Entry'更有效率。你的方法檢索所有的鍵,然後對每個鍵進行查找。當您從地圖中檢索所有按鍵時,這些值應該可以隨時使用,就在按鍵旁邊。但是你忽略了價值觀,然後再次查找它們,這是做了雙重工作。使用'entrySet()'可以避免冗餘工作。你的代碼對我來說看起來很好 - 這只是對如何改進的建議。 – ajb

+0

不, posmap是實際值。 map2是參數name.Answer馬特評論。 – Lucas