List<HashMap<String,Double>> 排序
时间: 2023-07-09 19:30:55 浏览: 49
要对 List<HashMap<String, Double>> 进行排序,可以使用 Java 8 的 Stream API 和 Lambda 表达式来实现。以下是一个示例代码,假设我们要按照 HashMap 中的 Double 值进行排序:
```
listOfMaps.stream()
.sorted((map1, map2) -> {
Double val1 = map1.get("key");
Double val2 = map2.get("key");
return val2.compareTo(val1);
})
.collect(Collectors.toList());
```
这段代码首先将 List 转换为 Stream,然后使用 sorted 方法对 Stream 进行排序。在 sorted 方法中,我们使用 Lambda 表达式来定义比较器,即根据每个 HashMap 中的 "key" 对应的 Double 值进行比较。最后,使用 collect 方法将排序后的结果转换为 List。
相关问题
List<Map<String, Object>>
This is a data structure in Java that represents a list of maps, where each map contains key-value pairs. The key is a string, and the value can be any object.
For example, let's say we have a list of employees, and each employee has a name, age, and salary. We can represent this data using a List<Map<String, Object>> as follows:
List<Map<String, Object>> employees = new ArrayList<>();
Map<String, Object> employee1 = new HashMap<>();
employee1.put("name", "John");
employee1.put("age", 30);
employee1.put("salary", 50000.0);
employees.add(employee1);
Map<String, Object> employee2 = new HashMap<>();
employee2.put("name", "Mary");
employee2.put("age", 25);
employee2.put("salary", 60000.0);
employees.add(employee2);
In this example, we have a list of two maps, where each map represents an employee. The keys in each map are "name", "age", and "salary", and the values can be any object (in this case, a String, an Integer, and a Double, respectively).
We can then access the data using various methods, such as iterating over the list and retrieving the values for each key, or using streams to filter and manipulate the data.
创建一个List<Map<String, String>>对象,其中包含3个Map对象,每个Map对象包含3对键值对,键为"name"、"age"和"gender",值为不同的字符串。 编写一个方法,该方法接受一个List<Map<String, String>>作为参数,并检查其中是否存在名称为"Tom"的人员,如果存在,则返回该人员的年龄和性别,如果不存在,则返回空字符串。 编写一个方法,该方法接受一个List<Map<String, String>>作为参数,并返回其中所有人员的年龄之和。 编写一个方法,该方法接受一个List<Map<String, String>>作为参数,并返回其中所有人员的平均年龄。 编写一个方法,该方法接受一个List<Map<String, String>>作为参数,并按照人员年龄进行排序,返回排序后的List对象。
请参考下面的Java代码实现:
```java
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Map<String, String>> people = new ArrayList<>();
Map<String, String> person1 = new HashMap<>();
person1.put("name", "Tom");
person1.put("age", "20");
person1.put("gender", "male");
Map<String, String> person2 = new HashMap<>();
person2.put("name", "Jerry");
person2.put("age", "25");
person2.put("gender", "female");
Map<String, String> person3 = new HashMap<>();
person3.put("name", "John");
person3.put("age", "30");
person3.put("gender", "male");
people.add(person1);
people.add(person2);
people.add(person3);
// 检查是否存在名称为"Tom"的人员,如果存在,则返回该人员的年龄和性别,如果不存在,则返回空字符串
String[] result1 = findAgeAndGenderByName(people, "Tom");
System.out.println(Arrays.toString(result1));
// 返回其中所有人员的年龄之和
int result2 = sumAge(people);
System.out.println(result2);
// 返回其中所有人员的平均年龄
double result3 = averageAge(people);
System.out.println(result3);
// 按照人员年龄进行排序,返回排序后的List对象
List<Map<String, String>> result4 = sortByAge(people);
System.out.println(result4);
}
public static String[] findAgeAndGenderByName(List<Map<String, String>> people, String name) {
for (Map<String, String> person : people) {
if (person.get("name").equals(name)) {
String age = person.get("age");
String gender = person.get("gender");
return new String[]{age, gender};
}
}
return new String[]{};
}
public static int sumAge(List<Map<String, String>> people) {
int sum = 0;
for (Map<String, String> person : people) {
String age = person.get("age");
sum += Integer.parseInt(age);
}
return sum;
}
public static double averageAge(List<Map<String, String>> people) {
int sum = sumAge(people);
double average = (double) sum / people.size();
return average;
}
public static List<Map<String, String>> sortByAge(List<Map<String, String>> people) {
Collections.sort(people, new Comparator<Map<String, String>>() {
@Override
public int compare(Map<String, String> o1, Map<String, String> o2) {
String age1 = o1.get("age");
String age2 = o2.get("age");
return Integer.parseInt(age1) - Integer.parseInt(age2);
}
});
return people;
}
}
```
输出结果为:
```
[20, male]
75
25.0
[{name=Tom, age=20, gender=male}, {name=Jerry, age=25, gender=female}, {name=John, age=30, gender=male}]
```