HashMap Get if the key exists from possible candidates in Java

1

I have the following variable to save properties with its clave:valor

Map<String, String> properties = new LinkedHashMap<>();

properties.put("lat","48.756189");
properties.put("longitude","94.218750");
properties.put("name","Gengis Kan");

I would like to obtain with a function if the key exists from some candidates for example latitude|lat|y if it finds one in order returns true

To separate the candidates I have

String candidatesStr = "latitude|lat|y"
String[] candidates = candidatesStr.split("|");   

I need to obtain the existing keys in properties and compare with some candidate if concide returns true

As a complement it would be to obtain from the candidates the name of the first one that has been found and then be able to obtain its value properties.get(candidato_encontrado)

    
asked by Webserveis 30.11.2017 в 19:19
source

3 answers

1

Itera on the hasmamp of the following

function boolean verifyCandidates(String[] candidates) {
    for(Map.Entry<String, String> prop: properties.entrySet()) {
        String key = prop.getKey();
        if(Arrays.asList(candidates).contains(key)){ 
           return true;
        }
    }
   return false;
}

If you want to get the value you are iterating from instead of returning a boolean it returns a string with the name

function String verifyCandidates(String[] candidates) {
    for(Map.Entry<String, String> prop: properties.entrySet()) {
      String key = prop.getKey();
      if(Arrays.asList(candidates).contains(key)) {
         return key;
      }
    }
    return "";
}

Or you can return the value once

function String verifyCandidates(String[] candidates) {
     for(Map.Entry<String, String> prop: properties.entrySet()) {
        String key = prop.getKey();
        if(Arrays.asList(candidates).contains(key)) {
           return prop.getValue();
        }
     }
     return "";
}
    
answered by 30.11.2017 / 19:40
source
1

Firstly, the correct way to separate the elements of a String using a pipe and generate an array is using .split("\|") , like this:

String candidatesStr = "latitude|lat|name";
String[] candidates = candidatesStr.split("\|");   

This way you could check if any key exists:

Map<String, String> properties = new LinkedHashMap<>();
        properties.put("lat","48.756189");
        properties.put("longitude","94.218750");
        properties.put("name","Gengis Kan");

        String candidatesStr = "latitude|lat|name";
        String[] candidates = candidatesStr.split("\|");

        for(Map.Entry<String, String> prop: properties.entrySet()) {
            String key = prop.getKey();
            if(Arrays.asList(candidates).contains(key)) {
                Log.i(TAG, "Se encuentra: " + key);
            }else{
                Log.i(TAG, "NO se encuentra: " + key);
            }
        }

If you want a method to know if any of the candidates is found, it would be:

public boolean checkCandidatos(String[] candidates, Map<String, String> properties) {
    for(Map.Entry<String, String> prop: properties.entrySet()) {
        String key = prop.getKey();
        if(Arrays.asList(candidates).contains(key)) {
            return true;
        }
    }
    return false;
}

If you want a method to get the name of the first candidate that is on the String

public String checkCandidato(String[] candidates, Map<String, String> properties) {
    for(Map.Entry<String, String> prop: properties.entrySet()) {
        String key = prop.getKey();
        if(Arrays.asList(candidates).contains(key)) {
            return key;
        }
    }
    return "";
}
    
answered by 30.11.2017 в 22:30
1

I add other solutions that I have been finding

To check if a candidate is available

public boolean hasProperty(String[] candidates) {
    for (String candidate : candidates) {
        if (properties.containsKey(candidate)) {
            return true;
        }
    }
    return false;
}

To obtain the value of a candidate if it exists

public String getProperty(String[] candidates) {
    for (String candidate : candidates) {
        if (properties.containsKey(candidate)) {
            return properties.get(candidate);
        }
    }
    return null;
}

To obtain the candidate

public String getCandidate(String[] candidates) {
    for (String candidate : candidates) {
        if (properties.containsKey(candidate)) {
            return candidate;
        }
    }
    return null;
}
    
answered by 30.11.2017 в 23:17