• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

collections help

 
anusha sivakumar
Greenhorn
Posts: 14
Chrome Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your question is not clear to me . elaborate please
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anusha sivakumar wrote:
... im ... jst .....


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

Also TellTheDetails

And Welcome to JavaRanch
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:Please UseRealWords - im, jst are not understood by all.

I thought he was new to Java and Jstl
 
anusha sivakumar
Greenhorn
Posts: 14
Chrome Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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é
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
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
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.

Regds,
Amit
 
anusha sivakumar
Greenhorn
Posts: 14
Chrome Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49380
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anusha sivakumar wrote: . . 1 n 10 . . .
Did you read what Mohammed Sanaullah posted earlier?
 
anusha sivakumar
Greenhorn
Posts: 14
Chrome Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic