File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes HashTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashTable" Watch "HashTable" New topic


AmitV VermaV

Joined: Dec 08, 2006
Posts: 2
How do I get the second maximum value from a Hashtable which contains integer value.
Hashtable Hsm = new Hashtable();
Hsm.put("1", 2);
Hsm.put("2", 3);
Hsm.put("3", 4);
Hsm.put("4", 5);
Hsm.put("5", 6);
Hsm.put("6", 7);
Hsm.put("7", 8);
Hsm.put("8", 9);
Hsm.put("9", 11);
Hsm.put("10", 10);

I want to fetch the second highest value.pls help me.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45307
Please don't use Hashtable; declare it as a Map and then use HashMap. Hashtable is generally regarded as a legacy class.

You aren't putting Integers into anything, but ints, so you are going to have to set the Map up with generic typing.
There are methods in the Map interface which allow you to get a Collection of all the values. Once you have got that, go through the Collection interface and the Collections class and see whether you can get them into any semblance of order.
Joe Ess

Joined: Oct 29, 2001
Posts: 9168

Have you made any effort to find the answer on your own? Have you looked at the API documentation for Hashtable to see what it can do? Have you looked at the rest of the Collections API to see what facilities are built-in to Java?

[How To Ask Questions On JavaRanch]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45307
You may have to get your results into a List; then see if you can sort a List
I agree. Here's the link:
subject: HashTable
It's not a secret anymore!