is there any way to tackle if the value in one-pair of hashmap matches with the key in other pair?

Itian :

Iam sorry in advance as I really don't have way to express the problem i want to ask for, but here i could brief with the help of my example code that what i want

static { 
  hashmap.put("Bus","Land_Vehicle");
  hashmap.put("Land_Vehicle","Vehicle"); 
  hashmap.put("SchoolBus","Bus");
  hashmap.put("Truck","Land_Vehicle");
  } 
  public static void checkRelationship(String key, String value) { 

      String val1=hashmap.get(key);
      String val2=hashmap.get(value);

          if(hashmap.containsKey(key) && hashmap.get(key).equals(value)==true) {

              System.out.println("values: "+val1+val2);
              System.out.println(" Something is missing in-between");


          }

If I needs to check the Relationship for ("Bus", "Vehicle") it should prompt a message that something is missing, as the Value for "Bus" and Key for the "Vehicle" are same i.e. Land_Vehicle.

Here i want to mentioned that String val1, val2 store the values "Land_Vehicle" and "Vehicle" respectively when iam checking the relationship for ("Bus", "Land_Vehicle"); but i dont understand how to check the relationship for ("Bus", "Vehicle").

Ruslan :

If you want to check the relationship between Bus and Vehicle try this:

public static void checkRelationship(String key, String value) {
    String val1=hashmap.get(key);  // get "Land_Vehicle"

    // get value by "Land_Vehicle" and compare with "Vehicle"
    if (value.equals(hashmap.get(val1))) {    
        System.out.println("Something is missing in-between");
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=150364&siteId=1