Matching two MultivaluedMaps and retrieving values

user2761431 :

MultivaluedMap<String, Pair<String, Float>> dataToCheck;

MultivaluedMap<String, String> inputData;

  1. I need to match the key of dataToCheck with inputData.
  2. Followed by matching the values of dataToCheck with inputData.
  3. If the values match, I retrieve the Float value. I want to exit at the first match.

Can exit with one key match.

float compareMap(MultivaluedMap<String, Pair<String, Float>> dataToCheck, MultivaluedMap<String, String> inputData) { }

EDIT:

MultivaluedMap is from Jax-Rs (https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/MultivaluedMap.html)

This is what I came up with. I know I can use filter, but I am not sure how to make it work.

    float sampleValue = new float[1];
    dataToCheck.keySet().stream().forEach(field -> {
        List<String> inputValues = inputData.get(field);
        List<Pair<String, Float>> checkValues = dataToCheck.get(field);
        if (checkValues != null && inputValues != null) {
            checkValues.stream().forEach(value -> {
                for (String inp : inputValues) {
                    if (value.getLeft().equalsIgnoreCase(inp)) {
                        sampleValue[0] = value.getRight();
                        break;
                    }
                }
            });
        }
    });
Naman :

A slightly cleaner way of performing that would be:

private static Float findFirstMatchingRight(MultivaluedMap<String, Pair<String, Float>> dataToCheck, 
                                            MultivaluedMap<String, String> inputData) {
    for (Map.Entry<String, List<Pair<String, Float>>> entry : dataToCheck.entrySet()) {
        Optional<Float> sampleValue = entry.getValue().stream()
                .filter(cv -> inputData.get(entry.getKey())
                        .stream()
                        .anyMatch(inp -> cv.getLeft().equalsIgnoreCase(inp)))
                .findFirst()
                .map(Pair::getRight);
        if (sampleValue.isPresent()) {
            return sampleValue.get();
        }
    }
    return Float.NaN; // when the condition doesn't match anywhere in the maps
}

Guess you like

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