2017-05-30 71 views
-1

我需要一個單一的散列映射兩個兩個連續值像在下面的例子進行比較:比較兩個連續hasmap值

鍵:自然----值爲:2

關鍵是:鴨----值爲:3

關鍵是:羊----值是:3

關鍵是:WoodTable ----值爲:4

關鍵是:PVCdoor - ---價值是:4

我問什麼,是我可以比較:

  • 自然鴨

  • 羊的價值鴨的值的值值

  • 羊價值與木材價值表
  • 木質價值表PVCdoor

....等

我試過,但我不能得到我需要的結果。如果你有任何想法,我需要你的幫助; 這是我使用的函數,但結果根本不是我需要的輸出。

謝謝

public Map<String, Integer> setCoefffils(Map<String, Integer> map){ 

     Map.Entry<String,Integer> entry=map.entrySet().iterator().next(); 



    this.listCoeffConceptfilsfinal.put(entry.getKey(), coeffFils); 

     Set<Entry<String, Integer>> setHm = map.entrySet(); 

     java.util.Iterator<Entry<String, Integer>> it = setHm.iterator(); 


       Entry<String, Integer> e = it.next(); 

       for(Entry<String, Integer> ee : setHm){ 
       // Entry<String, Integer> eeee = it.next(); 
        // for(Entry<String, Integer> eeee : setHm){ 
        System.out.println("key current is: "+ee.getKey() + " ---- Value is: " + ee.getValue()); 
        System.out.println("key following is: "+e.getKey() + " ---- Value is: " + e.getValue()); 
       if(ee.getValue().equals(e.getValue())) 
        System.out.println(""+ee.getValue() + " et " + e.getValue()+" sont égaux "); 
       else 
        System.out.println(" ne sont pas égaux "); 

      // } 



return this.listCoeffConceptfilsfinal; 


     } 
+1

你要什麼的結果是這種元件的比較之後? – SomeDude

回答

1

一種解決方案是將所有的密鑰存儲在列表中,那麼其他後訪問它們之一。

public static void foo(Map<String, Integer> map) { 
    Set<String> keySet = map.keySet(); 
    String lastKey = null; 
    for (String key : keySet) { 
     if (null == lastKey) { 
      lastKey = key; 
      continue; 
     } 
     if (map.get(key).equals(map.get(lastKey))) { 
      System.out.println("Les valeurs associées aux clés " + lastKey + " et " + key + " sont égales."); 
     } else { 
      System.out.println("Les valeurs associées aux clés " + lastKey + " et " + key + " sont différentes."); 
     } 
     lastKey = key; 
    } 
} 

但請注意:地圖並不總是保證按鍵處於插入順序。因此,你的比較可能是錯誤的。如果你想保留插入順序,你必須使用LinkedHashMap。

0

使用按鍵順序

CODE:

import java.util.TreeMap; 
import java.util.Map; 
import java.util.Iterator; 
/** 
* Write a description of class sumAndMax here. 
* 
* @author (your name) 
* @version (a version number or a date) 
*/ 
public class compareMap 
{ 

    public static void main(String[] args){ 
     Map<String, Integer> map = initializeMap(); 
     compareMaps(map); 
    } 

    private static void compareMaps(
     Map<String, Integer> map) 
     { 
      Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator(); 
      String leftKey = ""; 
      String rightKey = ""; 
      int leftValue = -1; 
      int rightValue = -1; 

      while (it.hasNext()) { 

       // Get values 
       Map.Entry<String, Integer> pair = it.next(); 
       leftKey = rightKey; 
       leftValue = rightValue; 
       rightKey = pair.getKey(); 
       rightValue = pair.getValue(); 

       if(!leftKey.equals("")){  
        // Compare keys 
        System.out.println("Comparing key "+leftKey+" with key "+rightKey); 
        System.out.println("Result: "+leftKey.equals(rightKey)); 

        // Compare values 
        System.out.println("Comparing value "+leftValue+" with value "+rightValue); 
        System.out.println("Result: "+(leftValue==rightValue)); 
       } 

      } 

     } 

    private static Map<String, Integer> initializeMap(){ 
     // Use Tree map for have a key ordered map !!! 
     Map<String, Integer> map = new TreeMap<>(); 
     map.put("keyA",3); 
     map.put("keyB",4); 
     map.put("keyC",8); 
     map.put("keyD",8); 
     map.put("keyE",89); 
     map.put("keyF",4); 
     map.put("keyG",4); 
     return map; 
    } 
} 

結果:

Comparing key keyA with key keyB 
Result: false 
Comparing value 3 with value 4 
Result: false 
Comparing key keyB with key keyC 
Result: false 
Comparing value 4 with value 8 
Result: false 
Comparing key keyC with key keyD 
Result: false 
Comparing value 8 with value 8 
Result: true 
Comparing key keyD with key keyE 
Result: false 
Comparing value 8 with value 89 
Result: false 
Comparing key keyE with key keyF 
Result: false 
Comparing value 89 with value 4 
Result: false 
Comparing key keyF with key keyG 
Result: false 
Comparing value 4 with value 4 
Result: true