YelCav
YelCav

Reputation: 51

How to search for multiple keys in Hash Table

I'm trying to perform more complex search in hash table, in the example below I can find if the hash table contains list of values, but only if it contains all the list (AND).

I wish to perform query like: “(key1=value or key2=value) and key3!=value”

So I'm looking for a module that can do it.

  //  The query parameter class
  public class QueryParameter {
      public String attribute;
      public String value;  
  }

//  Load the hash table
Hashtable<String, String> singleRow = new Hashtable<>();
for (int count = 0; count < someValue; count++) {   
    logRow.put(getKey(count), getValue(count));
}

//  Query the hash table
public void queryTable(QueryParameter... parameters) {

    Boolean rowMatches = true;
    for (QueryParameter parameter : parameters) {
        if (null != logRow.get(parameter.attribute)) {
            rowMatches = rowMatches && logRow.get(parameter.attribute).equals(parameter.value);
        }
    }
}

Upvotes: 0

Views: 387

Answers (1)

Cristian Sevescu
Cristian Sevescu

Reputation: 1489

With Java 8, you can use the stream API. Yo will work with entrySet from the map as Map itself is not a Collection.

It is described here how you can do something like:

    Map<Integer, String> monthsWithLengthFour = 
            MONTHS.entrySet()
            .stream()
            .filter(p -> p.getValue().length() == 4)
            .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));

You also have there the options for non Java 8.

Upvotes: 1

Related Questions