• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

collections help

 
Greenhorn
Posts: 14
Eclipse IDE Tomcat Server Chrome
  • 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.....
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your question is not clear to me . elaborate please
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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
Eclipse IDE Tomcat Server Chrome
  • 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 VI Editor Ubuntu
  • 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.
 
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
Eclipse IDE Tomcat Server Chrome
  • 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
 
Sheriff
Posts: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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).
 
Marshal
Posts: 64680
225
  • 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
Eclipse IDE Tomcat Server Chrome
  • 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
Boost this thread!