Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Fetch multiple keys at one go

 
electron dew
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you have so far?

Hunter
 
electron dew
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry if I misunderstood that.

I have a Hashmap , and wondering how to read multiple keys ...
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might have about 30k entries tops, not sure about performance degradation for numbers of this order.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"electron dew", please check your private messages for an important administrative matter. Thanks.
 
Ronald de Gier
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SortedMap allows you to get a head map, tail map and sub map, but only from a specific element to another specific element.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic