public class Task1 {
public static void main(String[] args) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
List<String> valSetOne = new ArrayList<String>();
valSetOne.add("0.00");
valSetOne.add("0.01");
valSetOne.add("1.00");
List<String> valSetTwo = new ArrayList<String>();
valSetTwo.add("0.00");
valSetTwo.add("0.01");
List<String> valSetThree = new ArrayList<String>();
valSetThree.add("0.01");
valSetThree.add("1.00");
List<String> valSetFour = new ArrayList<String>();
valSetFour.add("0.01");
map.put("HR3-A1234", valSetOne);
map.put("HR3-A2345", valSetTwo);
map.put("HR3-A3456", valSetThree);
map.put("HR3-A4567", valSetFour);
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
String key = entry.getKey();
List<String> values = entry.getValue();
Object obj = Collections.max(values);
System.out.println("Value of " + key + " is " + obj);
}
}
}
这是我的代码,它工作得很好。对于键 HR3-A2345,我想找到最小值,而对于其他记录,我需要最大值。有人可以建议我一个解决方案吗?
如果您不想使用if...else
,那么我建议先对集合进行排序,然后再将其放入地图以首先放置所需元素。例如:
final BiFunction<List<String>, Comparator<String>, List<String>> sort = (values, comparator) -> {
values.sort(comparator);
return values;
};
Map<String, List<String>> map = new HashMap<>();
map.put("HR3-A1234", sort.apply(valSetOne(), null)); // first element is min
map.put("HR3-A2345", sort.apply(valSetTwo(), Collections.reverseOrder())); // first element is max
map.put("HR3-A3456", sort.apply(valSetThree(), null)); // first element is min
map.put("HR3-A4567", sort.apply(valSetFour(), null)); // first element is min
map.forEach((key, values) ->
System.out.println("Value of " + key + " is " + (values != null && !values.isEmpty() ? values.iterator().next() : null)));
笔记:
Strings
! 这是不一样的。在这种情况下,必须更换String
与Double
您的代码。List
您可以使用TreeSet
所需的比较器而不是使用。例如;
public static void main(String... args) {
Map<String, Set<Double>> map = new HashMap<>();
map.put("HR3-A1234", valSetOne(null)); // first element is min
map.put("HR3-A2345", valSetTwo(Collections.reverseOrder())); // first element is max
map.put("HR3-A3456", valSetThree(null)); // first element is min
map.put("HR3-A4567", valSetFour(null)); // first element is min
map.forEach((key, values) -> {
if (values != null && !values.isEmpty())
System.out.format("Value of %s is %.2f\n", key, values.iterator().next());
});
}
private static Set<Double> valSetOne(Comparator<Double> comparator) {
Set<Double> values = new TreeSet<>(comparator);
values.add(.0);
values.add(.01);
values.add(1.);
return values;
}
private static Set<Double> valSetTwo(Comparator<Double> comparator) {
Set<Double> values = new TreeSet<>(comparator);
values.add(.0);
values.add(.01);
return values;
}
private static Set<Double> valSetThree(Comparator<Double> comparator) {
Set<Double> values = new TreeSet<>(comparator);
values.add(.01);
values.add(1.);
return values;
}
private static Set<Double> valSetFour(Comparator<Double> comparator) {
Set<Double> values = new TreeSet<>(comparator);
values.add(.01);
return values;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句