Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Max,Min

 
Graham Thorpe
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Can any body tell me how i can find out max and min value using the following format

{ArrCol1=1, ArrCol2=2}
{ArrCol1=2, ArrCol2=4}
{ArrCol1=3, ArrCol2=6}
{ArrCol1=4, ArrCol2=8}
{ArrCol1=5, ArrCol2=10}

The above values are Hashmap

So i want to find out ArrCol1 as Min/Max values and ArrCol2 as Min/Max Values

Like

Min of ArrCol1=1
Max of ArrCol1=5

Min of ArrCol2=2
Max of ArrCol2=10


Thanks
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this some kind of homework assignment? The answer to the question could depend on it. Assuming it is, describe finding the minimum algorithmically. Maybe that will help you see a solution.
 
Graham Thorpe
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its not assignment i want to find out min and max for this Col1 ,Col2 individually hashmap result {{Col1=41, Col2=45},{Col1=56, Col2=12},{Col1=10, Col2=11}}

Using Collections.min(collection)
Collections.max(collection)

Minimum of Col1=10
Maximum of Col1=56

and same time Minimum of Col2=11
Maximum of Col2=45
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the code you have written so far.
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the documentation for the map interface, if you had you would realise that.
1 HashMap is an implementation of Map i.e an object that maps keys to values
2 A map cannot contain duplicate keys
3 each key can map to at most one value.
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you seem to already know it. You just need to realize that Set implements Collection, and that there are HashMap methods for getting the set of keys and the collection of values. Just make sure that your keys and values are Comparable or that you have a Comparator!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic