答案 0 :(得分:0)
也许您可以使用Comparator并创建升序值数组列表
public static ArrayList<ObjectA> getObjectAList() {
ArrayList<ObjectA> objectAList = new ArrayList<>();
Collections.sort(objectAList, ObjectA.objectAValue);
return objectAList;
}
public class ObjectA{
/* Comparator for sorting the list by value */
public static Comparator<ObjectA> objectAValue = new Comparator<ObjectA>() {
@Override
public int compare(ObjectA object1, ObjectA object2) {
float value1 = object1.getValue();
float value2 = object2.getValue();
/* For ascending order */
return Float.floatToIntBits(value1)-Float.floatToIntBits(value2);
}
};
}
答案 1 :(得分:0)
您可以执行以下操作:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
List<Map<String, Float>> elements = new ArrayList<Map<String, Float>>();
Map<String, Float> element = new HashMap<String, Float>();
element.put("a", (float) 1.22);
element.put("b", (float) 1.33);
element.put("c", (float) 1.52);
element.put("d", (float) 1.352);
element.put("e", (float) 1.452);
element.put("f", (float) 1.42);
Map<String, Float> element1 = new HashMap<String, Float>();
element1.put("a", (float) 1.22);
element1.put("b", (float) 1.32);
element1.put("c", (float) 1.52);
element1.put("d", (float) 1.32);
element1.put("e", (float) 1.5552);
element1.put("f", (float) 1.42);
Map<String, Float> element2 = new HashMap<String, Float>();
element2.put("a", (float) 1.22);
element2.put("b", (float) 1.242);
element2.put("c", (float) 1.552);
element2.put("d", (float) 1.32);
element2.put("e", (float) 1.542);
element2.put("f", (float) 1.42);
elements.add(element);
elements.add(element1);
elements.add(element2);
System.out.println("Initial Mappings are: " + elements);
String[] keys = { "a", "b", "c", "d", "e", "f" };
List<Map<String, Float>> resultList = new ArrayList<Map<String, Float>>();
Map<String, Float> resultMap;
float min, max;
for (int j = 0; j < keys.length; j++) {
resultMap = new LinkedHashMap<String, Float>();
max = elements.get(0).get(keys[j]);
min = elements.get(0).get(keys[j]);
for (int i = 1; i < elements.size(); i++) {
if (max < elements.get(i).get(keys[j])) {
max = elements.get(i).get(keys[j]);
}
if (min > elements.get(i).get(keys[j])) {
min = elements.get(i).get(keys[j]);
}
}
resultMap.put(keys[j] + "->min", min);
resultMap.put("max", max);
resultList.add(resultMap);
}
System.out.println("Result: " + resultList);
}
}
输出:
Initial Mappings are: [{a=1.22, b=1.33, c=1.52, d=1.352, e=1.452, f=1.42}, {a=1.22, b=1.32, c=1.52, d=1.32, e=1.5552, f=1.42}, {a=1.22, b=1.242, c=1.552, d=1.32, e=1.542, f=1.42}]
Result: [{a->min=1.22, max=1.22}, {b->min=1.242, max=1.33}, {c->min=1.52, max=1.552}, {d->min=1.32, max=1.352}, {e->min=1.452, max=1.5552}, {f->min=1.42, max=1.42}]