比较两个连续的hasmap值

时间:2017-05-30 13:53:34

标签: java collections hashmap

我需要比较两个连续值的单个hashmap,如下例所示:

关键是:自然----价值是:2

关键是:duck ----值是:3

关键是:羊----值是:3

键是:WoodTable ----值是:4

关键是:PVCdoor ----值是:4

我要求的是,我该如何比较:

  • 自然的价值与鸭子的价值

  • 绵羊的价值

  • 具有woodTable值的绵羊的价值
  • woodTable的值,其值为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;


        }

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分: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