Solved

Searching for a substring in a Map

Posted on 2003-11-10
5
199 Views
Last Modified: 2010-03-31
I have a list of 3000 items.
Each item has key and value.

I need to find all the occurrence in the list whose value matches a substring.

Example:
My input is "home" and my list is:

Key              Value
001              home gardening
002              stay at home
003              office supplies
004              working from home - guide
005              the homeland office

The output should return the following items:
001, 002, 004 and 005.

I can use a vector and using a loop, perform a string.indexOf validation for each item in the list. Is there a more efficient way of doing this.
0
Comment
Question by:bennies
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9714551
>>Is there a more efficient way of doing this.

No - in a word.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9714586
...unless of course you can mark the items in some way on insertion, and then maintain a separate index.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9715772
I'd go along with CEHJs comments here.

There are always *different* ways of doing things, but that all depends on what you mean by efficiency.  Remember, efficiency is always a trade-off between space and speed.

The way you're doing it at the moment is probably about the best you could do in terms of space, implementing a separate index might make the code faster, but increases the space it takes.

It all depends on which (space or speed) you want to sacrifice ;-)

(Note.  Sometimes it is possible to create a solution that is more efficient in both space and speed.  This usually leads to unreadable/unmaintainable code ;-))
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now