-1

I have Declared a HashMap as

HashMap minMaxVal = new HashMap();
with K,V as Integer,Float[]

Would Like to retrieve the Min value from hashMap. Overriding the Min Function of collection is only solution for this scenario. How do I approach this.

Jeetesh Nataraj
  • 608
  • 2
  • 8
  • 20

2 Answers2

0

see this question : Get minvalue of a Map(Key,Double)

expecially see how to use a custom comparator, cause float[] is not directly (nor easily) comparable.

Community
  • 1
  • 1
Simone Gianni
  • 11,426
  • 40
  • 49
0

Your Map<Integer, Float[]> can be converted to a Set<Map.Entry<Integer, Float[]>> using Map.entrySet(). Once you have this set, you can use Collections.min() to find the minimum value. Your comparator will have to decide how two Map.Entry<Integer, Float[]> instances compare.

I could have given you an example, but you didn't say what min meant in your case.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • @ JB Nizet :: "min" here means extracting the Minimum or the lowest value for the given Collection... Thank You – Jeetesh Nataraj Aug 10 '11 at 09:52
  • Do you want the minimum key (Integer), or the minimum value (Float[]). If the latter, how do you compare Float[] instances? Is [1, 4] smaller or greater than [0, null, 12]? – JB Nizet Aug 10 '11 at 09:55
  • My scenario is this k,v [0-->(10.6,4.6)] [1-->(5.7,8.9)] I am suppose to extract the lowest the number among the values in the collection. – Jeetesh Nataraj Aug 10 '11 at 09:57
  • 1
    Then the best you can do is iterate through each value in the map (use Map.values() to get the values of the Map), and then iterate through each float of each array and keep the minimum value. It would be slightly easier if you used a Map> because then you would just have one iteration, and you could use Collections.min() to find the minimum value of each of the List. Next time you ask a question, make it much clearer. – JB Nizet Aug 10 '11 at 10:32