像这样将患者分类值存储在Hashmap中。
{
0=Patient [patientName=Robert, phoneNumber=9878594302, age=30],
1=Patient [patientName=mathew, phoneNumber=9876643278, age=56],
2=Patient [patientName=smith, phoneNumber=87, age=8334456781]
}
根据年龄,要显示广告的降序和升序 哈希图值?
怎么可能?
答案 0 :(得分:0)
是的,有可能,请参见下面的代码段。
public static void main(String[] args) {
Map<Integer, Patient> patients = new HashMap<>();
patients.put(0, new Patient("btest", 12));
patients.put(1, new Patient("atest", 11));
patients.put(2, new Patient("dtest", 10));
patients.put(3, new Patient("ctest", 13));
System.out.println("Ascending");
patients
.values()
.stream()
.sorted(Comparator.comparing(Patient::getAge))
.forEach(System.out::println);
System.out.println("Descending");
patients
.values()
.stream()
.sorted(Comparator.comparing(Patient::getAge).reversed())
.forEach(System.out::println);
}
患者实体
class Patient {
private String name;
private Integer age;
public Patient(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Patient{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}