2017-05-30 41 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