Solved

Searching for a substring in a Map

Posted on 2003-11-10
5
202 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 20
groupSum5 challenge 5 84
stackato and cloud 4 85
more than one jdk and one jre 1 41
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

777 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