Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashTable

 
AmitV VermaV
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I get the second maximum value from a Hashtable which contains integer value.
say
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
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afternoon.
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
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have to get your results into a List; then see if you can sort a List
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic