Skip to content
Advertisement

How to filter a hashmap with specific conditions

I have the following initial hashmap:

LatS=[59, 48, 59, 12, 48]

LatD=[41, 42, 46, 42]

EW=[W, W, W, W]

NS=[N, N, N, N]

LonM=[39, 23, 30, 48]

State=[OH, SD, WA, MA]

LatM=[5, 52, 35, 16]

City=[Youngstown, Yankton, Yakima, Worcester]

LonS=[0, 23, 36, 0]

LonD=[80, 97, 120, 71]

I want to filter the Hashmap using the method query that has the following call:

HashMap<Object, List<Object>> result = df.query( map -> "Youngstown".equals(df.getFrameInfo().get("City").get(0)))

The definition of the function is:

`public HashMap<Object, List<Object>> query(Predicate<Object> cond){

    HashMap<Object, List<Object>> ref = new HashMap<>(frameInfo);

        Map<Object, List<Object>> result = ref.entrySet()
                .stream()
                .filter(cond)
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

        System.out.println("Result: " + result);`

But the most dificult part is to obtain the hashmap result in a specific way. I only want the columns of the city passed as a parameter.

For example: if I use this argument:

HashMap<Object, List<Object>> result = df.query( map -> "Youngstown".equals(df.getFrameInfo().get("City").get(0)))

the output should be:

LatS=[59]
LatD=[41]
EW=[W]
NS=[N]
LonM=[N]
State=[OH]
LatM=[5]
City=[Youngstown]
LonS=[0]
Lond=[80]

Thnx you very much!

Advertisement

Answer

Issue

First of all, call:

df.query(map -> "Youngstown".equals(df.getFrameInfo().get("City").get(0)))

does not makes sense really, as in your particular case, provided predicate evaluates always to true – which you then pass to the filter(). Note, that you are not using predicate’s map variable at all.

Possible solution

I’d suggest to add extra parameter to query method, denoting which “key” we are querying (supposing we want to query always single key).

Then, pass predicate for value, which will finds the value for in list for particular “key”.

We will get the index for found value, and then construct final Map by extracting values for all keys on that index.

public Map<Object, List<Object>> query(String keySelector, Predicate<Object> valuePredicate) {
    final List<Object> row = frameInfo.get(keySelector);
    final List<Integer> indices = getColumnIndex(row, valuePredicate);

    return getColumnMap(indices);
}

private List<Integer> getColumnIndex(List<Object> row, Predicate<Object> valuePredicate) {
    return IntStream.range(0, row.size()).filter(columnIndex -> valuePredicate.test(row.get(columnIndex))).boxed().collect(Collectors.toList());
}

private Map<Object, List<Object>> getColumnMap(List<Integer> columnIndices) {
    final Map<Object, List<Object>> result = new HashMap<>();
    for (Map.Entry<Object, List<Object>> entry : frameInfo.entrySet()) {
        for (int columnIndex : columnIndices) {
            result.putIfAbsent(entry.getKey(), new ArrayList<>());
            result.get(entry.getKey()).add(entry.getValue().get(columnIndex));
        }
    }
    return result;
}

// Example call
final Map<Object, List<Object>> result = df.query("City", "Youngstown"::equals);
System.out.println(result);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement