Using Predicate to Find a Hazelcast Key Set

I'm new to Hazelcast - evaluation and prototyping to find out if it meets the requirements for distributed memory. One of the requirements was the use of wild cards to search for keys on this card. By looking at the IMap documentation, you can use keySet (predicate predicate). But I could not figure out how to use Predicate in such a way that, given the wild card string, a keySet was returned containing all the relevant keys. An example will be helpful.

Excerpt from my code. This is the client side.

IMap<String, String> mapTest1 = client.getMap("testMap"); mapTest1.put( "testKey1", "This is a Test" ); mapTest1.put( "testKey2", "This value has a long key name" ); mapTest1.put( "key3", "Another value" ); // Need a wild card search on the map, such that all keys with "%test%" will be returned. 

Thanks.

+1
source share
1 answer

This should do the trick if I understood your request correctly:

 public class Test { public static void main(String[] args) { HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(); IMap<String, String> map = hazelcastInstance.getMap("someMap"); Collection<String> keys = map.keySet( new RegexPredicate("[az].*[a-z0-9]")); System.out.println(keys); } public static class RegexPredicate implements Predicate<String, String> { private String regex; private transient Pattern pattern; public RegexPredicate(String regex) { this.regex = regex; } @Override public boolean apply(Map.Entry<String, String> mapEntry) { if (pattern == null) { pattern = Pattern.compile(regex); } Matcher matcher = pattern.matcher(mapEntry.getKey()); return matcher.matches(); } } } 
+2
source

All Articles