aspose file tools*
The moose likes Java in General and the fly likes Fetch multiple keys at one go Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Fetch multiple keys at one go " Watch "Fetch multiple keys at one go " New topic
Author

Fetch multiple keys at one go

electron dew
Greenhorn

Joined: Aug 05, 2010
Posts: 5

I have a Map ( Key - Value) , and my requirement is to fetch values for multiple key entries in one shot , is that at all possible or should I not define a Map in the first place and go for something more appropriate...

Say , 1 - One , 2- Two .... 100 - Hundred

I need to lookup values for 1, 7, 10 and 12 and get values One , Seven , Ten and Twelve in one shot..

Best,
e
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

What do you have so far?

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
electron dew
Greenhorn

Joined: Aug 05, 2010
Posts: 5
I am sorry if I misunderstood that.

I have a Hashmap , and wondering how to read multiple keys ...
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I understand what you are asking. I am merely asking if you made an attempt at this yet? and if so can I see what you have tried?

Hunter
electron dew
Greenhorn

Joined: Aug 05, 2010
Posts: 5
I considered writing a method that takes in a list parameter with all required keys , which will iterate over the list, fetch the value for each key and add it to a list and return it . but thats not going to help as my concern is to reduce the number of lookups in the first place...
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

That seems like a good idea, lookups in a map are generally pretty fast so I don't really think that is going to be an issue unless you are handling millions of pieces of data. If you don't want to use a list you could always make a function that only returns one value per call of the function. The list idea will also work however.

Example:


Hunter
electron dew
Greenhorn

Joined: Aug 05, 2010
Posts: 5
But is there an alternative collection which can do multiple key lookups in one go , something similar to say a Oracle IN query , if I am allowed to equate a normal key lookup to a Where Clause in SQL, if you will.
electron dew
Greenhorn

Joined: Aug 05, 2010
Posts: 5
I might have about 30k entries tops, not sure about performance degradation for numbers of this order.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I'm not sure about the oracle/or fetching multiple keys other than the way I posted, one of the more experienced programmers will post in a bit I'm sure. There is a way to return all of the values of the list in one go, I'm not sure if that is what you are looking for.

java.util.HashMap has a values() method that returns all of the values from the map in a list.

Hunter
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

But an "in" clause still has to compare each value, you just don't see it doing it. Anything that did the same thing with a map would still have to check for multiple key entries.

Have you actually *seen* a performance issue? Are you trying to *retrieve* 30k items using this method, or does the map *have* 30k items?

How to go about this most efficiently may well depend on the criteria you're using to decide which objects to retrieve, so it's tough to give a general-purpose answer. But anything grabbing elements from a standard map implementation is going to have to check to see if each element is in there.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61054
    
  66

"electron dew", please check your private messages for an important administrative matter. Thanks.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ronald de Gier
Greenhorn

Joined: Sep 09, 2009
Posts: 9
With a hashmap you're bound to get key/value one at a time.

I'd understand it if you needed 4 values from a sorted set, you'd want to iterate through it once and store the 4 values to return.

With a hashmap there's no guarantee on the order of elements. So they aren't linked in that sense. Having found one element does not mean finding the next is faster if you start looking from there.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

SortedMap allows you to get a head map, tail map and sub map, but only from a specific element to another specific element.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Consider Paul's rocket mass heater.
 
subject: Fetch multiple keys at one go