• Post Reply Bookmark Topic Watch Topic
  • New Topic

Map 'Like Statement'  RSS feed

 
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know in a Map I can store values in a Key value kind of way
for instance suppose there is a key called name and it has a Value george
like
tempMap.get("name") would return George

is there a function in Collections That I would say tempMap.get("n*"); and it would return an array List of Values that there keys start with n
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There aren't any built in methods that do that but you could build one that uses a regex to provide that functionality
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"n*" will not match what you really want using regexp. "n.*" will.

You may use org.apache.oro.text.GlobCompiler to match such patterns.
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way to match against a regex is to loop through the whole Map, as suggested. However if you're always just interested in finding keys that start with a certain pattern, (i.e. if the * is always at the end, never beginning or middle of the expression) then you may get more efficient results from a TreeMap:

or more generally

where str is "n" in your example, and end is a string just outside the range you're looking for.
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if it is what you want, but there is a neat little api that lets you query java objects called josql.

If you want to query Object[][] arrays with a sql like syntax (including regular expression sytax with 'like') then I have ArraySQL as part of my formatteddataset api below.
 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All for the suggestion and may I add the solution i worked on

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!