The moose likes Beginning Java and the fly likes collections help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "collections help" Watch "collections help" New topic

collections help

anusha sivakumar

Joined: Jan 20, 2011
Posts: 14

hi im new to java and jst practicing it.....

I have used hashmap to retrieve the data from data base....
here after retrieving them to a table i want to get only one record from that hashmap table and display below.....
please can any one help.....
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

your question is not clear to me . elaborate please
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064

anusha sivakumar wrote:
... im ... jst .....

Please UseRealWords - im, jst are not understood by all.

Also TellTheDetails

And Welcome to JavaRanch

Mohamed Sanaulla | My Blog
Christophe Verré

Joined: Nov 24, 2005
Posts: 14687

Mohamed Sanaulla wrote:Please UseRealWords - im, jst are not understood by all.

I thought he was new to Java and Jstl

[My Blog]
All roads lead to JavaRanch
anusha sivakumar

Joined: Jan 20, 2011
Posts: 14

this is my code....with this code im able to get all the 10records from database into my hash map......
now from this hashmap i want to get the 5th record
Christophe Verré

Joined: Nov 24, 2005
Posts: 14687

Why is the iterator loop inside the while loop ? Shouldn't you wait to put all records into the map before iterating through the map ?

Why do you want to get the 5th record ? I think it's important to know your exact requirement before starting suggesting anything.
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
First of all HashMap is a key-value based collection and can be accessed by using the Keys and not by index of the entry.
Now in your case the key is "id". I am not sure what this id signifies but if it is the kind of rowid which goes sequentially like 1,2,3,4,5 etc then you have a chance to get the 5th record by calling hm.get(5).
If your key which is "id" is not somehthing like rowid then you need to iterate the HashMap and reach 5th entry. This might return you different results everytime you iterate the hashmap because
1) The query you are firing may not return the results in the same order everytime
2) A simple Iterator does not guarantee iteration order.
Iterator<E> iterator() - Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

You can do something like this -
1) Change your query to add ORDER BY Clause so that you get ordered resultset.
2) Secondly while iterating through the resultset and adding it to the HashMap, maintain a counter. Use this counter as the Key in the HashMap. This will help you to get the nth record directly using key-based lookup.

anusha sivakumar

Joined: Jan 20, 2011
Posts: 14

i kept the iterator in the loop to display all the values or else it displays only the last value......
also my faculty wants any record in between 1 n 10 from the hashmap to be displayed
Rob Spoor

Joined: Oct 27, 2005
Posts: 19653

anusha sivakumar wrote:

Off-topic, but that's going to bite you in the behind sometime in the future. If somebody adds a column to your table between two other columns the indexes will change. Either specify which columns you retrieve in the select statement (select id, date, time, ... from appointments) or retrieve them using their name (rs.getString("id") etc).

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38016
anusha sivakumar wrote: . . 1 n 10 . . .
Did you read what Mohammed Sanaullah posted earlier?
anusha sivakumar

Joined: Jan 20, 2011
Posts: 14

i just now got the output.......
and i wasnt able to post correct details......because i didnt know how and as a fresher i couldnt give exact way
any ways thanks guys.....
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: collections help
Similar Threads
Passing my object to one to other activity
Locking on a static object
retriving data from panel to frame
Why HashMap.Entry is transient?
How to pick the data from the Table and keep in Hash map for the validation