Solved

Searching for a substring in a Map

Posted on 2003-11-10
5
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

717 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