hashmap find by like

hi,

i would like to use searh in hashmap (or similar with it) with like operator ('%') like in sql.
how can i do?

imagine i have a map like that;
1000, a1
1001, a2
2000, b1
2001, b2

what i need it when i run like this map.get(10%), it will give an array of object that contains a1, a2

thanks in advance.


 
komputerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

schybertCommented:
The only way I can think of of the top of my head is to implement your own Map class. Possibliy just extending HashMap. You then need to override the get method so that it searches the key set for keys that match your wildcard pattern. Let me know if you want details...
0
komputerAuthor Commented:
i need a solution, an open-source lib or source code.
0
schybertCommented:
I don't know of any open-source lib for this and some code snippets is what I can give you here. Take it or leave it ;-)

On second thought, I'd rather use a Utility class for this than subclassing HashMap. Most projects have some sort of CollectionUtil that I'm sure you can put it in. This example is by no means optimized for big amounts of data and the example uses regular expression matching instead of '%' syntax (you'll have to write the matches method yourself for that):


public static List wildcardGet(String key, Map map) {
	List retList = new ArrayList();
 
	String sKey = (String) key;
	Set keys = map.keySet();
	for (Iterator iter = keys.iterator(); iter.hasNext();) {
		Object o = (Object) iter.next();
		if (o instanceof String) {
			if (matches(key, (String) o)) {
				retList.add(map.get(o));
			}
		}
	}
 
	return retList;
}
 
private static boolean matches(String matchString, String target) {
	return target.matches(matchString);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

schybertCommented:
Ooops, the cast to sKey is unnecessary of course.

Usage is:

List l = CollectionUtil.wildcardGet("10\\w*", myMap);
0
cmalakarCommented:
What kind of objects are a1, a2
0
komputerAuthor Commented:
alhough it not what i look for (since it iterates all object and this will cause performance problems), i will accept, cause i have not found a geniric solution.

thanks guys.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.